AttributeError: ‘Tensor’ object has no attribute 'assign’解决办法
问题描述
a = tf.ones(shape=[1,2])
tf.assign(a,-1)
会报错:
AttributeError: 'Tensor' object has no attribute 'assign'
- 原因是Constant类型的Tensor不能assign,只有Variable才能调用assign方法
解决办法:
比如我现在有个3维的Tensor,叫ee,我想要更改它第二个维度的某列的值,可以这样:
bb
= tf
.ones_like
(ee
)*some_value
left
= aa
[:, :index
,:]
right
= aa
[:, index
+1:,:]
new_ee
= tf
.concat
(values
=[left
, bb
, right
], axis
=1)
其中,index是你想要替换掉的那一列的索引,some_value是你想要填充进去的值,left和right分别是两边保留的值。然后concat一起就行了。 同样的道理,如果你想替换掉的不是一列,而是某个值,那你直接定位那个值的索引,然后把它替换掉,就行了,这里就不举例了。