Reputation: 63
Currently working on a problem using Keras with a Tensorflow backend.
I am trying to create a neural network with the code below (minimum example, doesn't include my real data), although it gives the error:
"tuple index out of range" for the size of input_shape on the following line:
model.add(TimeDistributed(LSTM(32,return_sequences=True),input_shape=trainData.shape[1:]))
The error seems to be within the recurrent.py file on line 964:
self.input_dim = input_shape[2]
Where it's trying to access input_shape[2]. I am passing the shape to be only two numbers (the length of the time series of the data and number of channels). The shape I am passing is (100000,2).
I think this line is trying to access an index of something I haven't passed to it.
So my question is what should I use for my input shape to my neural network configuration?
I am using Keras version 2.0.3 and Tensorflow version 1.0.1.
EDIT: recurrent.py is a file supplied with Keras (I think). I don't want to start editing it in case I really break something.
# 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))
Upvotes: 2
Views: 7531
Reputation: 15
try this method, it worked for me.
model.add(LSTM(units=100,return_sequences=True,input_shape=(np.shape(x_train_data)[1],1)))
Upvotes: 1
Reputation: 21
Try using input_shape=trainData.shape[0:]
, since in python tuple index would start with 0.
Upvotes: 2
Reputation: 432
When accessing tuples, or arrays in python (and most programming languages) the count starts from 0, not 1.
Therefore, trying to access input_shape[2]
is actually the third item in input_shape
.
Upvotes: 0