我想在TensorFlow中增加张量的特定值.我try 使用的代码是:
outs[h, i:i+self.cnv, j:j+self.cnv] += (self.b*counts)/sums
我不确定如何做到这一点,张量的形状确实匹配,我使用的是TF版本2.9.1.错误消息为:
TypeError: 'Tensor' object does not support item assignment
我想在TensorFlow中增加张量的特定值.我try 使用的代码是:
outs[h, i:i+self.cnv, j:j+self.cnv] += (self.b*counts)/sums
我不确定如何做到这一点,张量的形状确实匹配,我使用的是TF版本2.9.1.错误消息为:
TypeError: 'Tensor' object does not support item assignment
要做到这一点并不容易.但这里有一种方法:
An Example:
个
inputs = tf.random.uniform(maxval=10, dtype=tf.int32, shape=(2, 3, 2))
array([[[0, 1], [0, 8], [4, 1]], [[4, 6], [2, 8], [5, 4]]], dtype=int32)>
#Convert to tf.Variable to make the assignment
temp = tf.Variable(inputs)
h,i,offset,value = 1, 1, 2,100 #try some random offset
temp[h, i:i+offset].assign(temp[h, i:i+offset]+value)
out = tf.convert_to_tensor(temp)
#output after assignment array([[[ 0, 1], [ 0, 8], [ 4, 1]], [[ 4, 6], [102, 108], [105, 104]]], dtype=int32)>