假设我有一个使用tf.Variable
,[[10,9],[9,8]]
的二维张量。所以我可以用索引来改变它的值。在这种情况下,我想将10更改为8,然后我使用assign函数编写了这段代码。
changeAbleTensor = tf.Variable([[10,9],[9,8]]);
changeAbleTensor[0][0].assign(10)
上面的代码,给我一个错误
AttributeError:"tensorflow.python.framework.ops.EagleTensor"对象没有属性"assign">
但是,当我将代码从changeAbleTensor[0][0].assign(10)
更改为changeAbleTensor[0,0].assign(10)
时,10成功更改为8。
我想知道这种行为,因为当我检查使用[0][0]
和[0,0]
的变量的类型时,它显示了相同的类型。
type(changeAbleTensor[0][0]), type(changeAbleTensor[0,0])
# result
# (tensorflow.python.framework.ops.EagerTensor,
# tensorflow.python.framework.ops.EagerTensor)
你能告诉我为什么我们[0][0]
会出错,说EagleTensor没有赋值属性,而另一方面[0,0]
有赋值属性,尽管两者都有相同的类型。
感谢
只是一个假设
对tf.Variable
应用切片操作将返回继承tf.Variable
的方法assign
的Tensor
。问题是changeAbleTensor[0][0]
首先对tf.Variable
进行切片,然后对tf.Tensor
进行切片,因此您似乎丢失了tf.Variable
的上下文。例如,当切片一次时,两个选项都具有相同的方法,包括assign
:
import tensorflow as tf
changeAbleTensor = tf.Variable([[1,9],[9,8]]);
print(dir(changeAbleTensor[0]))
print(dir(changeAbleTensor[0,0]))
['OVERLOADABLE_OPERATORS', '_USE_EQUALITY', '__abs__', '__add__', '__and__', '__array__', '__array_priority__', '__bool__', '__class__', '__complex__', '__copy__', '__deepcopy__', '__delattr__', '__dict__', '__dir__', '__div__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__iter__', '__le__', '__len__', '__long__', '__lt__', '__matmul__', '__mod__', '__module__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__or__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmatmul__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__tf_tracing_type__', '__truediv__', '__weakref__', '__xor__', '_add_consumer', '_as_node_def_input', '_as_tf_output', '_c_api_shape', '_copy', '_copy_nograd', '_copy_to_device', '_create_with_tf_output', '_datatype_enum', '_disallow_bool_casting', '_disallow_in_graph_mode', '_disallow_iteration', '_disallow_when_autograph_disabled', '_disallow_when_autograph_enabled', '_disallow_when_autograph_unavailable', '_handle_data', '_id', '_matmul', '_num_elements', '_numpy', '_numpy_internal', '_numpy_style_getitem', '_override_operator', '_prefer_custom_summarizer', '_rank', '_shape', '_shape_as_list', '_shape_tuple', '_summarize_value', '_tensor_shape', '_tf_api_names', '_tf_api_names_v1', '_with_index_add', '_with_index_max', '_with_index_min', '_with_index_update', 'assign', 'backing_device', 'consumers', 'cpu', 'device', 'dtype', 'eval', 'experimental_ref', 'get_shape', 'gpu', 'graph', 'is_packed', 'name', 'ndim', 'numpy', 'op', 'ref', 'set_shape', 'shape', 'value_index']
['OVERLOADABLE_OPERATORS', '_USE_EQUALITY', '__abs__', '__add__', '__and__', '__array__', '__array_priority__', '__bool__', '__class__', '__complex__', '__copy__', '__deepcopy__', '__delattr__', '__dict__', '__dir__', '__div__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__iter__', '__le__', '__len__', '__long__', '__lt__', '__matmul__', '__mod__', '__module__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__or__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmatmul__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__tf_tracing_type__', '__truediv__', '__weakref__', '__xor__', '_add_consumer', '_as_node_def_input', '_as_tf_output', '_c_api_shape', '_copy', '_copy_nograd', '_copy_to_device', '_create_with_tf_output', '_datatype_enum', '_disallow_bool_casting', '_disallow_in_graph_mode', '_disallow_iteration', '_disallow_when_autograph_disabled', '_disallow_when_autograph_enabled', '_disallow_when_autograph_unavailable', '_handle_data', '_id', '_matmul', '_num_elements', '_numpy', '_numpy_internal', '_numpy_style_getitem', '_override_operator', '_prefer_custom_summarizer', '_rank', '_shape', '_shape_as_list', '_shape_tuple', '_summarize_value', '_tensor_shape', '_tf_api_names', '_tf_api_names_v1', '_with_index_add', '_with_index_max', '_with_index_min', '_with_index_update', 'assign', 'backing_device', 'consumers', 'cpu', 'device', 'dtype', 'eval', 'experimental_ref', 'get_shape', 'gpu', 'graph', 'is_packed', 'name', 'ndim', 'numpy', 'op', 'ref', 'set_shape', 'shape', 'value_index']
而:
print(dir(changeAbleTensor[0][0]) == dir(changeAbleTensor[0,0]))
返回False
,因为assign
方法已不存在。