我想在 Tensorflow 中增加張量的某些值。我嘗試使用的代碼是:
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
uj5u.com熱心網友回復:
沒有簡單的方法來做到這一點。但這里有一種方法:
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)>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/524103.html
標籤:Python张量流张量
下一篇:ValueError:`logits`和`labels`必須具有相同的形狀,收到((None,10)vs(None,1))
