python - 元组索引超出 LSTM 神经网络的范围。 Python、Keras 和 Tensorflow

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

目前正在使用 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/

相关文章:

python - 我的分类器损失很大,准确率始终为 0

machine-learning - 本教程的神经网络隐藏层输入大小

neural-network - 如何在 Keras 中添加正交正则化?

neural-network - CNN前馈或反向传播模型

python - 使用 Hadoop Streaming 和 Python 读取/写入包含 Thrift 记录的序列文件

python - 检测具有 Youtube 播放限制的视频

python - 打印随机 unicode 字符(不使用 exec)

python - 根据条件获取同一 pandas 数据框中同一列的两个值之间的差异

android - 如何将 optimized_graph.lite 文件转换为 .tflite 文件?

python - `control_flow_ops.with_dependencies` 对 tensorflow 意味着什么?