目前正在使用 Keras 和 Tensorflow 后端解决问题。
我正在尝试使用下面的代码创建一个神经网络(最小示例,不包括我的真实数据),尽管它给出了错误:
以下行中 input_shape 的大小“元组索引超出范围”:
model.add(TimeDistributed(LSTM(32,return_sequences=True),input_shape=trainData.shape[1:]))
错误似乎出现在 recurrent.py 文件的第 964 行:
self.input_dim = input_shape[2]
尝试访问 input_shape[2] 的位置。我将形状传递为只有两个数字(数据时间序列的长度和 channel 数)。我传递的形状是(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))
最佳答案
尝试使用input_shape=trainData.shape[0:]
,因为在Python中元组索引将从0开始。
关于python - 元组索引超出 LSTM 神经网络的范围。 Python、Keras 和 Tensorflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43517152/