How do I increment certain values in Tensorflow tensor?

Question:

I want to increment certain values of an tensor in Tensorflow. The code I am attempting to use is:

outs[h, i:i+self.cnv, j:j+self.cnv] += (self.b*counts)/sums

I am not sure how to do this, and the shapes of the Tensors do match, and I am using tf version 2.9.1. The error message is:

TypeError: 'Tensor' object does not support item assignment
Asked By: MaxPC08

||

Answers:

No easy way of doing it. But here is one way:

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)>
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.