I'm new to Keras and having some trouble with shapes, specially when it comes to RNNs and LSTMs.
I'm running this code:
model.add(SimpleRNN(init='uniform',output_dim=1,input_dim=len(pred_frame.columns)))
model.compile(loss="mse", optimizer="sgd")
model.fit(X=predictor_train, y=target_train, batch_size=len(pred_frame.index),show_accuracy=True)
The variable predictor_train is a numpy array with 119 inner arrays, each one having 80 different items.
I'm having this error:
TypeError: ('Bad input argument to theano function with name "/Library/Python/2.7/site-packages/keras/backend/theano_backend.py:362" at index 0(0-based)', 'Wrong number of dimensions: expected 3, got 2 with shape (119, 80).')
So far what I found out is that an RNN receives 3D tensor with shape of (batch_size, timesteps, dimension) and when you set input_shape the batch_size is usually omitted, and you should just provide a tuple of (timesteps, dimension). But how exactly do I write that in my code??
timesteps usually indicate the length of your sequence (each sample should have same length, or you should do some paddings of your input to have the same length)
dimension usually indicate the number of units in a specific layer. for e.g. if your word embedding is 100 dimension, then you just set this param as 100.
Your code is good. The problem is with your data.
Thanks @farizrahman4u ! I was focusing too much in the code itself.
Thanks for the new clarifications @ymcui !
@ymcui @farizrahman4u, my input (predictor train) has the following characteristics:
type: 'numpy.ndarray'
shape: (119,80)
dtype: float64
I made this array out of a pandas dataframe where 80 was the number of rows (each row meaning one date) and 119 was the number of columns (each column meaning one feature). As far as I understood I should adapt that so that the shape has 3 dimensions (nb_samples, timesteps, input_dim), is that right?
If yes, input_dim should be the number of features and timesteps (in this case 119) the number of samples in each features (in other words the lenght of the index of the original predictor dataframe, in this case 80)? If that's right, what should nb_samples stand for?
Do you want to classify each (119,1) vector, or? I think the nb_samples is the number of your total samples, the timestep is the length of your each sample.You can have a look at this issue #2045
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.
Most helpful comment
timestepsusually indicate the length of your sequence (each sample should have same length, or you should do some paddings of your input to have the same length)dimensionusually indicate the number of units in a specific layer. for e.g. if your word embedding is 100 dimension, then you just set this param as 100.