0

I am trying to mimic the following behavior of numpy in TensorFlow.

z = np.zeros(2 * 10 - 1, dtype=np.float32)
z[[2,10]] = 1

what I have

test = tf.Variable(tf.zeros(2 * 10 - 1, dtype=tf.float32))
test[tf.constant([2,10])].assign(1)

I need the variable thus cannot just use the constant zeros.

When attempting this I get an error

InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'strided_slice' (op: 'StridedSlice') with input shapes: [19], [1,2], [1,2], [1].

But this doesn't make sense since the index I am providing is of rank 1, and it gets reshaped for some reason.

How would I mimic the behavior above?

2
  • I think this answer may be what you're looking for Commented Dec 6, 2018 at 17:22
  • Not exactly ( I said earlier "top notch") but realized that this does not solve it. Because the values are updated by one index at a time. will have to use scatter update. I think Commented Dec 6, 2018 at 18:19

1 Answer 1

1

This seems the closest to what I was looking for, but I am worried that this creates a duplicate value and if test is big, then this will be huge.

tf.scatter_add(test,[2,10],1)

Better answers are welcome.

Sign up to request clarification or add additional context in comments.

Comments

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.