I want to find part of a tensor and then assign a constant number to that but I received this error. It seems that the assignment to a tensor is not allowed in tensorflow. with that in mind, anyone has any idea how can I accomplish this one?
So for example if the tensor is like this:
tf_a2 = tf.Variable(([[2, 5, 1, 4, 3],
[1, 6, 4, 2, 3],
[0, 0, 0, 6, 6],
[2, 1, 1, 3, 3],
[4, 4, 1, 2, 3]]))
I want to find the elements per row in which they have the same value then I replace any element with N except the first element from left.
For example in the example above, in row=3, three elements =0, so
I kept the most left one the same as it is and then replace the right elements with the same value with N. in the same row there are two element with 6 value,I kept the most left element and replce all elements after that with the same value with N.
In row=4 ,1 is repeated two times, I again keep the most left one and replace any the right item which has the same value.
In row =5, 4 is repeated two times again. I keep the most left item, and then replace any item after that with the same value with N.
So for N= 9, the result would be:
[[2 5 1 4 3]
[1 6 4 2 3]
[0 9 9 6 9]
[2 1 9 3 9]
[4 9 1 2 3]]
I have the correct code in numpy below:
numpy code: (a2[:,1:])[a2[:,1:]==a2[:,:-1]] = N
but I need to do it in tensorflow , I tried the below code though it still raises the same error:
tf.where(tf.equal(a2[:,1:], a2[:, :-1]),N,a2[:,1:])
The error:
a2[:,1:][a2[:,1:]==a2[:,:-1]] = N
TypeError: 'Tensor' object does not support item assignment
I also looked at the links with the same error here, but they propose a solution exactly regarding their coding which did not match my code.
Thank you in advance