farizrahman4u / recurrentshop

Framework for building complex recurrent neural networks with Keras
MIT License
765 stars 218 forks source link

Error running the teacher forcing experiment from documentation #77

Open afourast opened 7 years ago

afourast commented 7 years ago

When running the teacher force example from documentation with tf backend I get

tensorflow.python.framework.errors_impl.InvalidArgumentError: The node 'recurrent_sequential_1/while_1/Variable_1/Assign' has inputs from different frames. The input 'recurrent_sequential_1/while_1/Const_1' is in frame 'recurrent_sequential_1/while_1/recurrent_sequential_1/while_1/'. The input 'recurrent_sequential_1/while_1/Variable_1' is in frame ''.

from keras.layers import Activation, Input
from keras.models import  Model
from recurrentshop import RecurrentSequential, LSTMCell
import numpy as np

rnn = RecurrentSequential(readout='add', teacher_force=True, return_sequences=True)
rnn.add(LSTMCell(10, input_dim=10))
rnn.add(LSTMCell(10))
rnn.add(LSTMCell(10))
rnn.add(Activation('softmax'))

x = Input((7, 10))
y_true = Input((7, 10))  # This is where you feed the ground truth values

y = rnn(x, ground_truth=y_true)

model = Model([x, y_true], y)

model.compile(loss='categorical_crossentropy', optimizer='sgd')

# Training

X_true = np.random.random((32, 7, 10))
Y_true = np.random.random((32, 7, 10))

model.fit([X_true, Y_true], Y_true)  # Note that Y_true is part of both input and output

# Prediction

X = np.random.random((32, 7, 10))

model.predict(X)  # >> Error! the graph still has an input for ground truth..

zeros = np.zeros((32, 7, 10)) # Need not be zeros.. any array of same shape would do

model.predict([X, zeros])

tf version = 1.2.0 keras version = 2.0.5

asturkmani commented 7 years ago

I get the same error - have you managed to resolve it?

Cyberdog52 commented 6 years ago

I realize that this thread is quite old, but since I recently stumbled on recurrentshop, I also experienced this bug. As it turns out, the example from the documentation didn't work even when it was pushed on Github in April. Here is a quick fix, that will allow you to run the example:

Simply change the backend of keras to theano. Either do this by following the instructions here (which didn't work for me) or run your code with KERAS_BACKEND=theano python your_teacher_enforcing_file.py . Hope this helps!

Lamyaaa commented 6 years ago

Agree with @Cyberdog52 , I have the similar Error when I try to run example from documentation, with readout=True and teacher_force=True:

ValueError: Initializer for variable recurrent_sequential_1/while/Variable/ is from inside a control-flow construct, such as a loop or conditional. When creating a variable inside a loop or conditional, use a lambda as the initializer.

And finally I change the backend to theano, and the problem gone.

keras version = 2.0.8 tf version = 1.3.0 theano version = 0.9.0