简体   繁体   English

AttributeError:“张量”object 没有属性“_in_graph_mode”

[英]AttributeError: 'Tensor' object has no attribute '_in_graph_mode'

I am having an error: 'Tensor' object has no attribute '_in_graph_mode'.我有一个错误:“张量”object 没有属性“_in_graph_mode”。 I've debugged the code, and I think it's in this GradientTape function, but I don't know why.我已经调试过代码,我认为它在这个GradientTape function 中,但我不知道为什么。 If anyone knows, please help me: :)如果有人知道,请帮助我::)

for i in range(50):
     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)

Where grad_model takes the input and output of a specific layer.其中grad_model采用特定层的输入和 output。 Furthermore, opt_img and * images* are tensors此外, opt_img和 * images* 是张量

     grads = tape.gradient(loss, [conv_inverted_outputs, conv_content_outputs])

     processed_grads = [g for g in grads]

     grads_and_vars = zip(processed_grads, [conv_inverted_outputs, conv_content_outputs])
     opt.apply_gradients(grads_and_vars)

And i get this error.我得到这个错误。

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'

''' '''

I just run into similar problem (same trace) and found a fix, hope it helps in your case too.我刚刚遇到了类似的问题(相同的跟踪)并找到了解决方法,希望它对您的情况也有帮助。

Check that:检查:

conv_inverted_outputs, conv_content_outputs

are of type tf.Variable and not tf.Tensor.是 tf.Variable 类型,而不是 tf.Tensor。

If any of them is tf.Tensor, you will get this error.如果其中任何一个是 tf.Tensor,您将收到此错误。

I don't see enough of your code to give exact fix, but in my case I was generating weights in the following way:我看不到足够的代码来给出确切的修复,但就我而言,我是通过以下方式生成权重:

weights = tf.random.normal((784, 10))

changing to:更改为:

weights = tf.Variable(tf.random.normal((784, 10)))

fixed the issue.解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'tensorflow.python.framework.ops.EagerTensor' object 没有属性 '_in_graph_mode' - 'tensorflow.python.framework.ops.EagerTensor' object has no attribute '_in_graph_mode' AttributeError:&#39;Tensor&#39;对象没有属性&#39;append&#39; - AttributeError: 'Tensor' object has no attribute 'append' TensorFlow:AttributeError:&#39;Tensor&#39;对象没有属性&#39;shape&#39; - TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape' Medpy AttributeError: &#39;Tensor&#39; 对象没有属性 &#39;astype&#39; - Medpy AttributeError: 'Tensor' object has no attribute 'astype' AttributeError:&#39;Tensor&#39;对象没有属性&#39;compile&#39; - AttributeError: 'Tensor' object has no attribute 'compile' AttributeError:“张量”object 没有属性“to_sparse” - AttributeError: 'Tensor' object has no attribute 'to_sparse' AttributeError:“ Tensor”对象没有属性“ exp” - AttributeError: 'Tensor' object has no attribute 'exp' AttributeError: &#39;Tensor&#39; 对象没有属性 &#39;read&#39; - AttributeError: 'Tensor' object has no attribute 'read' AttributeError: &#39;Tensor&#39; 对象没有属性 &#39;numpy&#39; - AttributeError: 'Tensor' object has no attribute 'numpy' AttributeError:&#39;Tensor&#39;对象没有属性&#39;reshape&#39; - AttributeError: 'Tensor' object has no attribute 'reshape'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM