Python 属性错误:';张量';对象没有属性'_在"图形"模式下';
我有一个错误:“Tensor”对象没有属性“\u在图模式中”。我已经调试了代码,我认为它在这个GradientTape函数中,但我不知道为什么。如果有人知道,请帮帮我!:) 其中grad_模型接受特定层的输入和输出。此外,opt_img和*images*是张量Python 属性错误:';张量';对象没有属性'_在"图形"模式下';,python,python-3.x,tensorflow2.0,attributeerror,Python,Python 3.x,Tensorflow2.0,Attributeerror,我有一个错误:“Tensor”对象没有属性“\u在图模式中”。我已经调试了代码,我认为它在这个GradientTape函数中,但我不知道为什么。如果有人知道,请帮帮我!:) 其中grad_模型接受特定层的输入和输出。此外,opt_img和*images*是张量 grads = tape.gradient(loss, [conv_inverted_outputs, conv_content_outputs]) processed_grads = [g for g in gra
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)
我得到了这个错误
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'
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.7/runpy.py”,第193行,在“运行”模块中作为“主”
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
文件“/usr/local/lib/python3.7/runpy.py”,第85行,在运行代码中
exec(代码、运行\全局)
文件“/home/helena/.vscode/extensions/ms python.python-2020.2.64397/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/____.py”,中第45行
cli.main()
文件“/home/helena/.vscode/extensions/ms python.python-2020.2.64397/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/。/ptvsd/server/cli.py”,主文件第361行
运行()
文件“/home/helena/.vscode/extensions/ms python.python-2020.2.64397/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/。/ptvsd/server/cli.py”,第203行,在run_文件中
runpy.run\u路径(options.target,run\u name=“\uuuuuu main\uuuuu”)
文件“/usr/local/lib/python3.7/runpy.py”,第263行,在运行路径中
pkg_name=pkg_name,script_name=fname)
文件“/usr/local/lib/python3.7/runpy.py”,运行模块代码第96行
模块名称、模块规格、组件名称、脚本名称)
文件“/usr/local/lib/python3.7/runpy.py”,第85行,在运行代码中
exec(代码、运行\全局)
文件“/home/helena/Documents/LAR_Celesc/LAR computer vision/objdetapi/test_inversed_image.py”,第20行,在
数据、模型、类别索引=选项卡、类别索引、图层名称=“block5\u conv3”
文件“/home/helena/Documents/LAR_Celesc/LAR computer vision/objdet api/tf_explain/core/inversed_image.py”,解释中第54行
图像、模型、类索引、层名称
文件“/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site packages/tensorflow_core/python/eager/def_function.py”,第568行,在调用中__
结果=自身调用(*args,**kwds)
文件“/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site packages/tensorflow_core/python/eager/def_function.py”,第615行,在调用中
self.\u初始化(参数、KWD、添加初始值设定项到=初始值设定项)
文件“/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site packages/tensorflow_core/python/eager/def_function.py”,第497行,在
*args,**科威特第纳尔)
文件“/home/helena/Documents/LAR_-Celesc/larenv/lib/python3.7/site-packages/tensorflow\u-core/python/eager/function.py”,第2389行,位于“获取具体的”函数“内部垃圾”收集中
图函数,自我,可能定义函数(args,kwargs)
文件“/home/helena/Documents/LAR_-Celesc/larenv/lib/python3.7/site-packages/tensorflow_-core/python/eager/function.py”,第2703行,在定义函数中
graph\u function=self.\u create\u graph\u function(args,kwargs)
文件“/home/helena/Documents/LAR_-Celesc/larenv/lib/python3.7/site-packages/tensorflow_-core/python/eager/function.py”,第2593行,在创建图函数中
按值捕获=自身。_按值捕获),
文件“/home/helena/Documents/LAR_-Celesc/larenv/lib/python3.7/site-packages/tensorflow_-core/python/framework/func_-graph.py”,第978行,在函数图中
func_outputs=python_func(*func_args,**func_kwargs)
文件“/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site packages/tensorflow_core/python/eager/def_function.py”,第439行,包装为
返回弱_-wrapped_-fn()
文件“/home/helena/Documents/LAR_-Celesc/larenv/lib/python3.7/site-packages/tensorflow_-core/python/framework/func_-graph.py”,第968行,在包装器中
将e.ag\u错误\u元数据引发到\u异常(e)
AttributeError:在转换的代码中:
/home/helena/Documents/LAR_Celesc/LAR computer vision/objdetapi/tf_explain/core/inversed_image.py:125获取优化图像*
选择应用梯度(梯度和变量)
/home/helena/Documents/LAR_-Celesc/larenv/lib/python3.7/site packages/tensorflow\u core/python/keras/optimizer\u v2/optimizer\u v2.py:434应用梯度
自创建插槽(变量列表)
/home/helena/Documents/LAR_-Celesc/larenv/lib/python3.7/site-packages/tensorflow\u-core/python/keras/optimizer\u v2/gradient\u-dence.py:100个创建槽
自我添加(变量,“动量”)
/home/helena/Documents/LAR_-Celesc/larenv/lib/python3.7/site packages/tensorflow\u core/python/keras/optimizer\u v2/optimizer\u v2.py:574 add\u slot
var\u key=\u var\u key(var)
/home/helena/Documents/LAR_Celesc/larenv/lib/python3.7/site packages/tensorflow\u core/python/keras/optimizer\u v2/optimizer\u v2.py:1065\u var\u key
如果变量处于图形模式:
AttributeError:“Tensor”对象在图形模式下没有属性“\u”
''我刚刚遇到了类似的问题(相同的跟踪)并找到了解决方案,希望对您的情况也有帮助 检查:
conv_inverted_outputs, conv_content_outputs
属于tf.Variable类型,而不是tf.Tensor类型
如果其中任何一个是tf.张量,你就会得到这个错误
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'
我看不到足够的代码来提供精确的修复,但在我的例子中,我是通过以下方式生成权重的:
weights = tf.random.normal((784, 10))
改为:
weights = tf.Variable(tf.random.normal((784, 10)))
解决了这个问题。我刚刚遇到了类似的问题(相同的跟踪),并找到了一个解决方案,希望对您的情况也有帮助 检查:
conv_inverted_outputs, conv_content_outputs
属于tf.Variable类型,而不是tf.Tensor类型
如果其中任何一个是tf.张量,你就会得到这个错误
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'
我看不到足够的代码来提供精确的修复,但在我的例子中,我是通过以下方式生成权重的:
weights = tf.random.normal((784, 10))
改为:
weights = tf.Variable(tf.random.normal((784, 10)))
已修复此问题。Hi@Stanislav,谢谢!我替换了列表:conv_inversed_outputs,conv_content_outputs,用于tape.gradient内的可训练变量。以下是完整的代码:。代码运行了,但没有正常运行。我想这是在应用梯度,有什么想法吗?嗨@Stanislav,谢谢!我替换了列表:conv_inversed_outputs,conv_content_outputs,用于tape.gradient内的可训练变量。以下是完整的代码:。代码运行时,