【问题标题】:Tuple index out of range with LSTM Neural network. Python, Keras with TensorflowLSTM 神经网络的元组索引超出范围。 Python、Keras 和 Tensorflow
【发布时间】:2017-04-20 10:36:48
【问题描述】:

目前正在使用带有 Tensorflow 后端的 Keras 解决问题。

我正在尝试使用以下代码创建一个神经网络(最小示例,不包括我的真实数据),尽管它给出了错误:

“元组索引超出范围”输入形状的大小在以下行:

model.add(TimeDistributed(LSTM(32,return_sequences=True),input_shape=trainData.shape[1:]))

错误似乎在第 964 行的recurrent.py 文件中:

self.input_dim = input_shape[2]

它试图访问 input_shape[2] 的位置。我将形状传递给只有两个数字(数据时间序列的长度和通道数)。我传递的形状是(100000,2)。

我认为这条线试图访问我没有传递给它的东西的索引。

所以我的问题是我的神经网络配置的输入形状应该使用什么?

我使用的是 Keras 2.0.3 版和 Tensorflow 1.0.1 版。

编辑:recurrent.py 是 Keras 提供的文件(我认为)。我不想开始编辑它,以防我真的破坏了一些东西。

# import the necessary packages
from sklearn.cross_validation import train_test_split
from keras.models import Sequential
from keras.layers import Activation
from keras.optimizers import SGD
from keras.layers import LSTM
from keras.layers.wrappers import TimeDistributed
from keras.utils import np_utils
import numpy as np
numClasses = 10
time_points = 100000
num_chans = 2

iq = np.empty((time_points,0,1), int)# creates empty numpy array.
labels = np.empty([0,1])

raw_data = np.random.rand(500,time_points,num_chans)
labels = np.random.randint(numClasses, size=(500, 1))
one_hot_labels = np_utils.to_categorical(labels, num_classes=None)

print(one_hot_labels.shape)

# partition the data into training and testing splits, using 75%
# of the data for training and the remaining 25% for testing
print("[INFO] constructing training/testing split...")
(trainData, testData, trainLabels, testLabels) = train_test_split(
raw_data, one_hot_labels, test_size=0.25, random_state=42)

trainLabels = trainLabels.reshape(375,10,1)
testLabels = testLabels.reshape(125,10,1)

print(trainData.shape)
print(testData.shape)
print(trainLabels.shape)
print(testLabels.shape)
print(len(trainData.shape))

# define the architecture of the network
model = Sequential()
# Long short term memory experiment
model.add(TimeDistributed(LSTM(32, return_sequences=True),input_shape=trainData.shape[1:]))
model.add(TimeDistributed(LSTM(10, return_sequences=True)))
model.add(Activation("softmax"))

print(trainData.shape)
print(trainLabels.shape)

## train the model using SGD
print("[INFO] compiling model...")
sgd = SGD(lr=0.01)
model.compile(loss="sparse_categorical_crossentropy", optimizer=sgd, metrics=["accuracy"])
model.fit(trainData, trainLabels)

## show the accuracy on the testing set
print("[INFO] evaluating on testing set...")
(loss, accuracy) = model.evaluate(testData, testLabels, batch_size=128, verbose=1)
print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss, accuracy * 100))

【问题讨论】:

  • 使用时间分布式 LSTM 的目的是什么? LSTM 最初采用序列,所以您正在输入序列序列?不知道我明白了...
  • 我的实际数据中长度为100000的维度是时间。因此,我在两个通道中有 100000 个时间采样点。因此大小为 100000×2。所以我认为在这种情况下 timedistributed 是必要的?
  • print(trainData.shape) print(trainLabels.shape) 这个输出什么?
  • 打印:(375,100000,2,1) (125,100000,2,1)

标签: python tensorflow neural-network keras recurrent-neural-network


【解决方案1】:

尝试使用input_shape=trainData.shape[0:],因为在 python 中,元组索引将从 0 开始。

【讨论】:

    【解决方案2】:

    在 python(和大多数编程语言)中访问元组或数组时,计数从 0 开始,而不是 1。

    因此,尝试访问input_shape[2]实际上是input_shape中的第三项。

    【讨论】:

    • 是的,我知道。鉴于我的输入 (100000,2),我希望 input_shape[0]=100000 和 input_shape[1] = 2。所以 input_shape[2] 超出范围。因此错误。那么我应该将什么传递给函数以使我的神经网络配置正确?
    • input_shape=trainData.shape[1:] 可能是问题所在。此语法使 trainData.shape 缩短了一个元素。
    • 这会返回一个不同的错误:“检查输入时出错:预期 time_distributed_1_input 有 4 个维度,但得到了形状为 (375, 100000, 2) 的数组”。所以我的问题仍然存在,我应该将什么 input_shape 传递给 LSTM 以使其正确?
    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 2020-06-08
    • 2020-02-25
    • 2016-07-21
    • 2016-04-04
    • 2021-10-18
    • 2019-06-23
    • 2013-12-16
    相关资源
    最近更新 更多