tensorflow: AttributeError: 'Tensor' object has no attribute '_in_graph_mode'
I am having an error: ‘Tensor’ object has no attribute ‘_in_graph_mode’. I’ve debugged the code, and I think it’s in this GradientTape function, but I don’t know why. If anyone knows, please help me! 😃
System information
- TensorFlow version: 2.0 - ‘2.2.0-dev20200407’
- OS Platform and Distribution: Linux Mint
- Python version: Python 3.7.4
Describe the current behavior I am trying to minimize a function using opt = tf.keras.optimizers.Adam() and I am getting a TypeError when I apply opt.apply_gradients.
Standalone code to reproduce the issue
def explain(
self,
validation_data,
model,
class_index,
layer_name=None,
colormap=cv2.COLORMAP_VIRIDIS,
image_weight=0.7,
_grid=True
):
# Returns: numpy.ndarray: Grid of all the inverted image or 4D array (batch_size, height, width, channels)
tf.executing_eagerly()
images, _ = validation_data
if layer_name is None:
layer_name = self.infer_target_layer(model)
inverted_image = InvertedImage.get_optimize_image(
images, model, class_index, layer_name
)
if _grid:
return grid_display(inverted_image)
else:
return inverted_image
@staticmethod
def infer_target_layer(model):
# Returns: str: Name of the target layer
for layer in reversed(model.layers):
# Select closest 4D layer to the end of the network.
if len(layer.output_shape) == 4 and layer.name.count('conv') > 0:
return layer.name
raise ValueError(
"Model does not seem to contain 4D layer. Inverted image cannot be applied."
)
@tf.function
def get_optimize_image(images, model, class_index, layer_name):
grad_model = tf.keras.models.Model(
[model.inputs], [model.get_layer(layer_name).output]
)
opt = tf.keras.optimizers.SGD(learning_rate=1-4, momentum=0.9)
dtype = model.get_layer(layer_name).output.dtype
tensor_image = tf.convert_to_tensor(images)
opt_img = tf.Variable(1e-1 * tf.random.normal((tensor_image.shape[0], tensor_image.shape[1], tensor_image.shape[2], tensor_image.shape[3])), trainable=True)
steps = 50
for i in range(steps):
with tf.GradientTape() as tape:
inverted_feature = tf.cast(opt_img, dtype)
content_feature = tf.cast(images, dtype)
conv_inverted_outputs = grad_model(inverted_feature)
conv_content_outputs = grad_model(content_feature)
loss = InvertedImage.get_loss(conv_content_outputs, conv_inverted_outputs, content_feature, inverted_feature)
#print("Initial loss: {:.3f}".format(loss))
grad = tape.gradient(loss, [conv_inverted_outputs, conv_content_outputs])
print(grad)
processed_grads = [g for g in grad]
opt.apply_gradients(zip(processed_grads, [conv_inverted_outputs, conv_content_outputs]))
return opt_img
Loss function
def get_loss(conv_content_outputs, conv_inverted_outputs, content_feature, inverted_feature):
euclidian = tf.norm(conv_content_outputs - conv_inverted_outputs, ord='euclidean') / tf.norm(conv_content_outputs, ord='euclidean')
reg_alpha = 1e-7 * tf.math.reduce_sum(tf.norm(inverted_feature, ord=6))
total_variation = 1e-8 * tf.math.reduce_sum(tf.image.total_variation(content_feature+inverted_feature))
return euclidian + reg_alpha + total_variation
Traceback
Traceback (most recent call last):
File "/usr/local/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/local/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/helena/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/__main__.py", line 45, in <module>
cli.main()
File "/home/helena/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/../ptvsd/server/cli.py", line 361, in main
run()
File "/home/helena/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/../ptvsd/server/cli.py", line 203, in run_file
runpy.run_path(options.target, run_name="__main__")
File "/usr/local/lib/python3.7/runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "/usr/local/lib/python3.7/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/usr/local/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/helena/Documents/LAR_Celesc/lar-computer-vision/objdet-api/test_inverted_image.py", line 20, in <module>
data, model, class_index=tabby_cat_class_index, layer_name="block5_conv3"
File "/home/helena/Documents/LAR_Celesc/lar-computer-vision/objdet-api/tf_explain/core/inverted_image.py", line 54, in explain
images, model, class_index, layer_name
File "/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py", line 568, in __call__
result = self._call(*args, **kwds)
File "/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py", line 615, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py", line 497, in _initialize
*args, **kwds))
File "/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2389, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2703, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2593, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py", line 978, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py", line 439, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py", line 968, in wrapper
raise e.ag_error_metadata.to_exception(e)
AttributeError: in converted code:
/home/helena/Documents/LAR_Celesc/lar-computer-vision/objdet-api/tf_explain/core/inverted_image.py:125 get_optimize_image *
opt.apply_gradients(grads_and_vars)
/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/keras/optimizer_v2/optimizer_v2.py:434 apply_gradients
self._create_slots(var_list)
/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/keras/optimizer_v2/gradient_descent.py:100 _create_slots
self.add_slot(var, "momentum")
/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/keras/optimizer_v2/optimizer_v2.py:574 add_slot
var_key = _var_key(var)
/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site-packages/tensorflow_core/python/keras/optimizer_v2/optimizer_v2.py:1065 _var_key
if var._in_graph_mode:
AttributeError: 'Tensor' object has no attribute '_in_graph_mode'
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 16 (7 by maintainers)
The error message did not get saved in the colab - could you copy it here? At a glance, I recommend creating the model and the optimizer outside the tf.function. You will definitely get errors because tf.function doesn’t let you create variables inside it (and whenever you create a new model object, it creates a new set of variables).
@helenabdr yes, and pass it as argument to the function. Although for a simple, stateless optimizer like SGD it might not be necessary. But definitely do that with the model.
This is a well-documented limitation where you’re not allowed to unconditionally create new keras models (including new variables) inside a tf.function.
@helenabdr, Please provide the complete standalone code to reproduce the issue. Thanks