Manipulating matrix elements in tensorflow

11,909

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.

Share:
11,909
Shagas
Author by

Shagas

Updated on June 26, 2022

Comments

  • Shagas
    Shagas almost 2 years

    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
    Shagas about 8 years
    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.
  • dga
    dga about 8 years
    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.
  • Shagas
    Shagas about 8 years
    Thanks for the answer. I solved the problem by using a new tensor derived from the original as you suggested.
  • Epimetheus
    Epimetheus about 7 years
    I can't find tf.select in my tensorflow version 1.0.0. Is there a replacement?
  • Meta Fan
    Meta Fan about 7 years
    tf.select is changed to tf.where just like np.where since tf 1.0.
  • Rouzbeh
    Rouzbeh almost 7 years
    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: stackoverflow.com/questions/44834739/…