0

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

5
  • Please provide a Minimal, Reproducible Example that leads to the mentioned error. Please also include the full traceback. Commented Jun 11, 2019 at 22:12
  • Sure, I am adding it Commented Jun 11, 2019 at 22:23
  • @a_guest Thank you following with my question, I have updated it, please let me know if I need to add more data. Commented Jun 11, 2019 at 22:28
  • You code examples are still not self-contained. Please update with something that we can copy & paste in order to verify the behavior you describe. Furthermore the statement "I want to find rows which the left and right item are the same, then change it to a constant number like N." is not clear to me. Could you please elaborate a bit more? What means "left" and "right" element here? Commented Jun 12, 2019 at 13:20
  • @a_guest I have updated my question with a good example and explanations Commented Jun 12, 2019 at 14:47

1 Answer 1

1

As the error message says, " 'Tensor' object does not support item assignment". But there are at least one workaround. One method is to element-wise multiply each element you want to change by zero (in the original matrix) and then create a new matrix (same shape) with all element zeros without the one you want to change. And then you can just add them together to get the desired matrix.

If i understand you're numpy solution correctly for this specific case, you always want to ignore the first column? If that is so, I think this tensorflow solution should work for you (tested and verified for tensorflow version 1.13.1).

import tensorflow as tf
tf.enable_eager_execution()

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]]))

N=9

first_col_change = tf.zeros([tf_a2.shape[0],1], dtype=tf.int32)
last_cols_change = tf.cast(tf.equal(tf_a2[:,1:], tf_a2[:, :-1]),tf.int32)
change_bool = tf.concat([first_col_change, last_cols_change],axis=-1)
not_change_bool = 1-change_bool
tf_a2_changed = tf_a2*not_change_bool + change_bool*N

print(tf_a2_changed)

which gives the output:

tf.Tensor(
[[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]], shape=(5, 5), dtype=int32)
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for following my question, I can not run your code successfully it raises this error: FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable [[Node: Variable/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](Variable)]]
I have also updated the question with better example
What tensorflow version are you on? try "print("TensorFlow Ver: ", tf.__version__)". You can try "tf.enable_eager_execution()" it gives you ability to go line by line and print each tensor. great for error search. Just tested the code again here, and works for tensorflow 1.13.1
I have 1.10.0 tensorflow. Can you also, share your code in which you get its output? I just want to double check
@sariii good that it worked! Tensorflow 1.13.1 is an "in-between" release for tensorflow 2.0. Therefore it works without sessions etc, which i don't think 1.10.0 support. I suggest you try out the new version as it will be more similar to the new 2.0 when it get out, and easier to prototype with.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.