10

How can I do the following in tensorflow?

mat = [4,2,6,2,3] #
mat[2] = 0 # simple zero the 3rd element

I can't use the [] brackets because it only works on constants and not on variables. I cant use the slice function either because that returns a tensor and you can't assign to a tensor.

import tensorflow as tf
sess = tf.Session()
var1 = tf.Variable(initial_value=[2, 5, -4, 0])
assignZerosOP = (var1[2] = 0) # < ------ This is what I want to do

sess.run(tf.initialize_all_variables())

print sess.run(var1)
sess.run(assignZerosOP)
print sess.run(var1)

Will print

[2, 5, -4, 0] 
[2, 5, 0, 0])
Shagas
  • 605
  • 1
  • 5
  • 12

1 Answers1

19

You can't change a tensor - but, as you noted, you can change a variable.

There are three patterns you could use to accomplish what you want:

(a) Use tf.scatter_update to directly poke to the part of the variable you want to change.

import tensorflow as tf

a = tf.Variable(initial_value=[2, 5, -4, 0])
b = tf.scatter_update(a, [1], [9])
init = tf.initialize_all_variables()

with tf.Session() as s:
  s.run(init)
  print s.run(a)
  print s.run(b)
  print s.run(a)

[ 2 5 -4 0]

[ 2 9 -4 0]

[ 2 9 -4 0]

(b) Create two tf.slice()s of the tensor, excluding the item you want to change, and then tf.concat(0, [a, 0, b]) them back together.

(c) Create b = tf.zeros_like(a), and then use tf.select() to choose which items from a you want, and which zeros from b that you want.

I've included (b) and (c) because they work with normal tensors, not just variables.

abhinonymous
  • 329
  • 2
  • 13
dga
  • 21,757
  • 3
  • 44
  • 51
  • Thanks alot, it was definately of help and I could work with that but it would be somewhat combersome.What If I actually needed to change the tensor? Is there no way to do that? I want to implement network visualisation and I need to propagate an image up to an activation layer, zero all activations except a random one a then propagate that back. – Shagas Mar 03 '16 at 18:57
  • Tensors are immutable. In general, if you want to save and change state, you would store it in a variable, which you *can* mutate. But otherwise, you just create a new tensor derived from the original and use that. In the case you're describing, that's probably the approach. – dga Mar 05 '16 at 00:49
  • Thanks for the answer. I solved the problem by using a new tensor derived from the original as you suggested. – Shagas Mar 06 '16 at 08:47
  • I can't find `tf.select` in my tensorflow version 1.0.0. Is there a replacement? – Epimetheus Mar 17 '17 at 14:07
  • 1
    `tf.select` is changed to `tf.where` just like `np.where` since tf 1.0. – Meta Fan Mar 24 '17 at 07:21
  • What would you in the following situation?Suppose I have a tensor in Tensorflow that its values are like: A = [[0.7, 0,2, 0.1],[0.1, 0.4, 0.5]] I also asked this question here. How can I change this tensor into the following: B = [[1, 0, 0],[0, 0, 1]] In other words I want to just keep the maximum and replace it with 1. Any help would be appreciated. I also asked this question here: https://stackoverflow.com/questions/44834739/argmax-on-a-tensor-and-ceiling-in-tensorflow – Rouzbeh Jun 29 '17 at 20:54