I'm trying to implement a stateful RNN, however it keeps asking me for a "complete input_shape (including batch size)". So I tried different things for the input_shape
and input_batch_size
arguments, neither of which seems to work.
Code:
model=Sequential()
model.add(SimpleRNN(init='uniform',
output_dim=80,
input_dim=len(pred_frame.columns),
stateful=True,
batch_input_shape=(len(pred_frame.index),len(pred_frame.columns)),
input_shape=(len(pred_frame.index),len(pred_frame.columns))))
model.add(Dense(output_dim=200,input_dim=len(pred_frame.columns),init="glorot_uniform"))
model.add(Dense(output_dim=1))
model.compile(loss="mse", class_mode='scalar', optimizer="sgd")
model.fit(X=predictor_train, y=target_train,
batch_size=len(pred_frame.index),show_accuracy=True)
Traceback:
File "/Users/file.py", line 1483, in Pred
model.add(SimpleRNN(init='uniform',output_dim=80,input_dim=len(pred_frame.columns),stateful=True,batch_input_shape=(len(pred_frame.index),len(pred_frame.columns)),input_shape=(len(pred_frame.index),len(pred_frame.columns))))
File "/Library/Python/2.7/site-packages/keras/layers/recurrent.py", line 194, in __init__
super(SimpleRNN, self).__init__(**kwargs)
File "/Library/Python/2.7/site-packages/keras/layers/recurrent.py", line 97, in __init__
super(Recurrent, self).__init__(**kwargs)
File "/Library/Python/2.7/site-packages/keras/layers/core.py", line 43, in __init__
self.set_input_shape((None,) + tuple(kwargs['input_shape']))
File "/Library/Python/2.7/site-packages/keras/layers/core.py", line 141, in set_input_shape
self.build()
File "/Library/Python/2.7/site-packages/keras/layers/recurrent.py", line 199, in build
self.reset_states()
File "/Library/Python/2.7/site-packages/keras/layers/recurrent.py", line 221, in reset_states
'(including batch size).')
Exception: If a RNN is stateful, a complete input_shape must be provided (including batch size).
You need to provide only the batch_input_shape= parameter, and not the input_shape parameter. Also, to avoid input shape errors, make sure the training data size is a multiple of batch_size. And finally, if you are using validation splits, you have to be sure that both splits are also multiples of the batch_size.
# ensure data size is a multiple of batch_size
data_size=data_size-data_size%batch_size
# ensure validation splits are multiples of batch_size
increment=float(batch_size)/len(data_size)
val_split=float(int(val_split/(increment))) * increment
In your definition of SimpleRNN
, remove input_dim
and input_shape
, set:
batch_input_shape = (Number_Of_sequences, Size_Of_Each_Sequence,
Shape_Of_Element_In_Each_Sequence)
batch_input_shape
should be a tuple of length at least 3.
If you passes your sequences one by one, set:
Number_Of_sequences = 1
If the size of your sequences is not fixed, set:
Size_Of_Each_Sequence = None
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.