farizrahman4u/recurrentshop

LSTM inside RecurrentModel

cpernoud opened this issue · 2 comments

Thanks for this great package! I've looked over your documentation and it certainly seems like this should work and I could use some help. I'm probably missing that I need to control the timestep in some way. I saw the nested code, but I think I'm getting tripped up on the Functional version. Here's the toy code:

import recurrentshop
import numpy
import keras.layers
from keras.layers import Dense, Input, LSTM
from keras.models import Model

prior_out = Input(batch_shape=(1,1,5),name='prior_out')
main_input = Input(batch_shape=(1,1,5),name='main_input')
added_layer = keras.layers.add([prior_out,main_input])
# hidden = LSTM(10,stateful=True,return_sequences=False,name='hidden')(added_layer)
hidden = recurrentshop.cells.LSTMCell(10)(added_layer)
out = Dense(5,name='out')(hidden)
rnn = recurrentshop.RecurrentModel(input=main_input, initial_states=prior_out, output=out, final_states=out)
rnn_input = Input(batch_shape=(1,1,5),name='rnn_input')
rnn_output = rnn(rnn_input)
model = Model(inputs=rnn_input,outputs=rnn_output)
model.compile(optimizer='adam',loss='mae')
print model.predict(numpy.random.random((3,1,5)),batch_size=1)

And I get this error:
ValueError: Operands could not be broadcast together with shapes (0,) (10,)

Thanks again for the help!

import recurrentshop
import numpy
import keras.layers
from keras.layers import Dense, Input, LSTM
from keras.models import Model

prior_out = Input(batch_shape=(1,5),name='prior_out')
main_input = Input(batch_shape=(1,5),name='main_input')
h_tm1 = Input(batch_shape=(1, 10))
c_tm1 = Input(batch_shape=(1, 10))
added_layer = keras.layers.add([prior_out,main_input])
# hidden = LSTM(10,stateful=True,return_sequences=False,name='hidden')(added_layer)
hidden, h, c = recurrentshop.cells.LSTMCell(10)([added_layer, h_tm1, c_tm1])
out = Dense(5,name='out')(hidden)
rnn = recurrentshop.RecurrentModel(input=main_input, initial_states=[prior_out, h_tm1, c_tm1], output=out, final_states=[out, h, c])
rnn_input = Input(batch_shape=(1,1,5),name='rnn_input')
rnn_output = rnn(rnn_input)
model = Model(inputs=rnn_input,outputs=rnn_output)
model.compile(optimizer='adam',loss='mae')
print model.predict(numpy.random.random((3,1,5)),batch_size=1)

Thanks @farizrahman4u , and there it was right on the front page, I just wasn't getting what it was doing.