Closed yanndupis closed 4 years ago
@yanndupis
I have tried on colab with TF version 2.0 and i am not seeing any error message. Please, find the gist here.Thanks!
Hello @ravikyram , thank you very much for your quick response.
I should have been more precise. You will get the error, if you load the model in a different script (where the model hasn't been explicitly defined) then start training it. In the gist, the entire code is in the same Colab. You should be able to reproduce if you have two different Colabs.
In the first Colab you run:
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Embedding
from tensorflow.keras.layers import LSTM
model = Sequential()
model.add(Embedding(20000, 128))
model.add(LSTM(128))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
tf.keras.models.save_model(model,
'imdb_model',
include_optimizer=True,
save_format='tf')
and in a different Colab you run:
import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing import sequence
(x_train, y_train), _ = imdb.load_data(num_words=20000)
x_train = sequence.pad_sequences(x_train, maxlen=80)
model = tf.keras.models.load_model('imdb_model')
model.fit(x_train, y_train, epochs=1)
Let me know if you have any question. Thank you!
I have tried on jupyter notebook with TF version 2.0 and was able to reproduce the issue.Please, find the files in attachment.Thanks! error.tar.gz
Hi @yanndupis, @ravikyram , It looks like we have the same problem, with a similar error, when saving a model, and then loading it again, e.g. for retrain.
We save the model using tf.saved_model.save() and not Keras.
We are running on Ubuntu 16.04, Python 3.6, TF2.0 We have just tried and the problem still exists with TF2.1.0-rc1
Is there any update on this ???
Hitting the same error with a bidirectional GRU layer
hitting a similar error with an LSTM model
Hitting similar issue with simple LSTM.
@yanndupis I cannot reproduce the issue with recent tf-nightly
. Can you please check the gist here. I ran the first cell to save the model, then restarted colab's runtime, then ran second cell to load the model and execute model.fit
.
Please close the issue if this was resolved for you. Thanks!
@yanndupis Closing this as it was resolved in tf-nightly
. Please feel free to reopen if this was not resolved for you. Thanks!
Error still present in tf-nightly
. Ran the example given by @jvishnuvardhan in two separate colab runtimes (saving in one, and loading in the other) and the error is still present:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:757 train_step
self.trainable_variables)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:2722 _minimize
gradients = tape.gradient(loss, trainable_variables)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/backprop.py:1073 gradient
unconnected_gradients=unconnected_gradients)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/imperative_grad.py:77 imperative_grad
compat.as_str(unconnected_gradients.value))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:797 _backward_function
return self._rewrite_forward_and_call_backward(call_op, *args)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:712 _rewrite_forward_and_call_backward
forward_function, backwards_function = self.forward_backward(len(doutputs))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:621 forward_backward
forward, backward = self._construct_forward_backward(num_doutputs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:669 _construct_forward_backward
func_graph=backwards_graph)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py:986 func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:659 _backprop_function
src_graph=self._func_graph)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gradients_util.py:669 _GradientsHelper
lambda: grad_fn(op, *out_grads))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gradients_util.py:336 _MaybeCompile
return grad_fn() # Exit early
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gradients_util.py:669 <lambda>
lambda: grad_fn(op, *out_grads))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:712 _rewrite_forward_and_call_backward
forward_function, backwards_function = self.forward_backward(len(doutputs))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:621 forward_backward
forward, backward = self._construct_forward_backward(num_doutputs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:669 _construct_forward_backward
func_graph=backwards_graph)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py:986 func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:659 _backprop_function
src_graph=self._func_graph)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gradients_util.py:669 _GradientsHelper
lambda: grad_fn(op, *out_grads))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gradients_util.py:336 _MaybeCompile
return grad_fn() # Exit early
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gradients_util.py:669 <lambda>
lambda: grad_fn(op, *out_grads))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:712 _rewrite_forward_and_call_backward
forward_function, backwards_function = self.forward_backward(len(doutputs))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:621 forward_backward
forward, backward = self._construct_forward_backward(num_doutputs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:669 _construct_forward_backward
func_graph=backwards_graph)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py:986 func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py:659 _backprop_function
src_graph=self._func_graph)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gradients_util.py:623 _GradientsHelper
(op.name, op.type))
LookupError: No gradient defined for operation 'while' (op type: StatelessWhile)```
@Gentatsu I ran the first part and then restarted the runtime
and then ran the second part (loading). I don't see any issue. Please note that I ran both the code in one colab but two runtimes.
As this is an old issue, Can you please open a new issue with standalone code to reproduce the issue. Thanks!
Facing the same error with tensorflow 2.3.1 and python 3.8. The model includes an LSTM layer. And I want to load the model and retrain it. I have been using the same script. I use tf.save_model.save and tf.save_model.load.
System information
Describe the current behavior When training a Sequential model (Embedding + LSTM + Dense layer) restored from SavedModel with load_model(
model = tf.keras.models.load_model('imodel_saved')
, I am getting the following error :LookupError: No gradient defined for operation 'while' (op type: While)
.I am able to train the same model, if the model was previously saved in
h5
format (tf.keras.models.save_model(model, 'imdb_model', include_optimizer=True, save_format='h5
). However it should work with `save_format='tf`` as well. It works for other models (e.g. convolutional model) but not for this specific model:Describe the expected behavior We should be able to train the model when restored from SavedModel.
Code to reproduce the issue
First run this script to define and save the Sequential model:
Then run the following script to restore the model and train it on the IMDB dataset:
Other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.