Reputation: 2557
I've built a very simple TensorFlow Keras model with a single dense layer. It works perfectly fine outside a GradientTape
block, but inside a GradientTape
block it raises LookupError: No gradient defined for operation 'IteratorGetNext' (op type: IteratorGetNext)
Code to reproduce:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import tensorflow as tf
import numpy as np
print(tf.__version__)
model = Sequential()
model.add(Dense(1, input_shape=(16,)))
fake_data = np.random.random((1, 16))
print(model.predict(fake_data).shape) # works
with tf.GradientTape() as tape:
print(model.predict(fake_data).shape) # LookupError: No gradient defined for operation 'IteratorGetNext' (op type: IteratorGetNext)
This appears to work in TensorFlow 2.0.0, however it fails in TensorFlow 2.1.0 and 2.2.0
Here is a notebook that replicates the issue.
Upvotes: 12
Views: 6448
Reputation: 22031
try to redefine the predict operation in GradientTape in this way
with tf.GradientTape() as tape:
print(model(fake_data).shape)
Upvotes: 15