Keras: Bug : Specifying initial state for Recurrent layers

Created on 4 Apr 2017  Â·  22Comments  Â·  Source: keras-team/keras

There are 2 issues:

rnn = SimpleRNN(10)  # Layer not built yet. So state_spec is None
x = Input((7, 5))
h_tm1 = Input((10,))
y = rnn(x, initial_state=h_tm1)  # >> Error

Another issue is at : https://github.com/fchollet/keras/blob/master/keras/layers/recurrent.py#L236

if hasattr(initial_state, '_keras_history')

Doesn't take into account when initial_state is a list. (Such as in LSTM, which has multiple states).

Most helpful comment

@xn8812 Great! Now you have hit a real bug. Will fix soon.

All 22 comments

ping @Joshua-Chin

This will be fixed by #5795. We are waiting for someone to review it.

Fixed.

I encountered an error when try to input the initial_state to the ConvLSTM2D layer. The error is

TypeError: ('Keyword argument not understood:', 'initial_state')

Any idea? Thanks! @farizrahman4u @Joshua-Chin

To be more clear, let me post the code here.

lstm_input = Input(shape=(None,) + (32, 32, 3)) 
initial_state_input = Input(shape=(32, 32, 3)) 
x = ConvLSTM2D(128, (3,3), initial_state=initial_state_input, name='lstm')(lstm_input)

It gives me an error message: TypeError: ('Keyword argument not understood:', 'initial_state')

Probably "initial_state" has not been implemented for ConvLSTM2D layer? If so, what else method I could use to set the hidden state of the ConvLSTM2D layer?

Thanks!

I don't believe we are supposed to have support for this in ConvLSTM2D yet,
but since ConvLSTM2D inherits from Recurrent where it is implemented,
getting it to work should be fairly simple. Please feel free to open a PR
for this.

On 9 May 2017 at 10:08, xn8812 notifications@github.com wrote:

To be more clear, let me post the code here.

lstm_input = Input(shape=(None,) + (32, 32, 3))
initial_state_input = Input(shape=(32, 32, 3))
x = ConvLSTM2D(128, (3,3), initial_state=initial_state_input, name='lstm')(lstm_input)

It gives me an error message: TypeError: ('Keyword argument not
understood:', 'initial_state')

Probably "initial_state" has not been implemented for ConvLSTM2D layer? If
so, what else method I could use to set the hidden state of the ConvLSTM2D
layer?

Thanks!

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/fchollet/keras/issues/6142#issuecomment-300235056,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AArWb-D04ZkHojHkeSbDuB7OWTVV2jyzks5r4J2lgaJpZM4MyunI
.

Update Keras.

I just updated Keras but still got the same error. Have you fixed the problem for ConvLSTM2D? @farizrahman4u

ConvLSTM2D uses Recurrent's __call__ and call by inheritance, so you are not supposed to get that error on latest version of Keras. Are you able to specify initial state for LSTM?

Wait a sec, this seems to be a silly user error..you are supposed to pass the initial state to __call__ not __init__.

x = ConvLSTM2D(128, (3,3), name='lstm')(lstm_input, initial_state=initial_state_input)

If it is in fact meant to be supported by ConvLSTM2D, it should have a unit test for it, which I don't believe it does. Could someone please add one?

I use "sudo pip install keras --upgrade" to update and I am able to run your code "y = rnn(x, initial_state=h_tm1)" without errors. Please correct me if I did it wrong.

I also tried to correct the silly user error. Now the error is "TypeError: unsupported operand type(s) for +: 'InputSpec' and 'list'"

@xn8812 Great! Now you have hit a real bug. Will fix soon.

6564

Found another bug. When I try the following code:

lstm = ConvLSTM2D(128, (3,3))
x = Input(shape=(None,32, 32, 3))
h_tm1 = Input(shape=(32, 32, 3))
y = lstm(x, initial_state=h_tm1)

the error message is "ValueError: Layer has 2 states but was passed 1 initial states."

When I try the following code:

lstm = ConvLSTM2D(128, (3,3))
x = Input(shape=(None,32, 32, 3))
h_tm1 = Input(shape=(32, 32, 3))
y = lstm(x, initial_state=[h_tm1, h_tm1])  

the error message is "ValueError: Layer conv_lst_m2d_1 expects 2 inputs, but it received 3 input tensors. Input received: [/input_1, /input_2, /input_2]"

@farizrahman4u

6581

Got another bug when trying the following code:

rnn = SimpleRNN(10, stateful=True)
x = Input(batch_input=(1,7,5))
y = rnn(x)
model = Model(x, y)
h_tm = np.zeros((10))
model.reset_states(states=h_tm)

The error message is "reset_states() got an unexpected keyword argument 'states'". The error happens no matter the recurrent layer is stateful or not. When fixing the bug, please also consider the ConvLSTM2D layer.

Thanks! @farizrahman4u

I found this on the Keras documentation page:

You can specify the initial state of RNN layers numerically by calling reset_states with the keyword argument states. The value of states should be a numpy array or list of numpy arrays representing the initial state of the RNN layer.

Hi,

I think I have some misunderstanding of resetting the initial states of recurrent layers. Now I know that I should use layer.reset_states(states=h_tm1) in order to set the initial states numerically. I have tested the SimpleRNN layer and it works. However, it still has some bug for the ConvLSTM2D layer.

lstm = ConvLSTM2D(512,(3,3), stateful=True)
x = Input(batch_shape=(1,1,32,32,3))
y = lstm(x)
model = Model(x,y)
h = np.zeros((1,1,32,32,3))
model.layers[1].reset_states(states=[h,h])

Running the above code will cause an error: "reset_states() got an unexpected keyword argument 'states'". Could someone fix it? Really appreciate it!

@farizrahman4u @Joshua-Chin @fchollet

LSTM(rnnConfig['LSTM_units'],initial_state =image_model)(model_2)
can any help me out to know whether the use of initial_state in LSTM is correct or not.
If it is wrong way the how can I initialize my initial_state

Was this page helpful?
0 / 5 - 0 ratings

Related issues

harishkrishnav picture harishkrishnav  Â·  3Comments

farizrahman4u picture farizrahman4u  Â·  3Comments

nryant picture nryant  Â·  3Comments

KeironO picture KeironO  Â·  3Comments

vinayakumarr picture vinayakumarr  Â·  3Comments