How to update a subset of a 2D tensor in Tensorflow?

I want to update the index in the 2D tensor with a value of 0. Thus, the data is a 2D tensor, the value of the second index of the 2nd column of which must be replaced by 0. However, I get a type error. Can anyone help me with this?

TypeError: Input 'ref' of 'ScatterUpdate' Op requires an input of value l

data = tf.Variable([[1,2,3,4,5], [6,7,8,9,0], [1,2,3,4,5]]) data2 = tf.reshape(data, [-1]) sparse_update = tf.scatter_update(data2, tf.constant([7]), tf.constant([0])) #data = tf.reshape(data, [N,S]) init_op = tf.initialize_all_variables() sess = tf.Session() sess.run([init_op]) print "Values before:", sess.run([data]) #sess.run([updated_data_subset]) print "Values after:", sess.run([sparse_update]) 
+2
python deep-learning neural-network tensorflow
source share
2 answers

Updating scatter only works with variables. Try this template instead.

Tensorflow version <1.0: a = tf.concat(0, [a[:i], [updated_value], a[i+1:]])

Tensorflow version> = 1.0: a = tf.concat(axis=0, values=[a[:i], [updated_value], a[i+1:]])

+2
source share

tf.scatter_update can only be applied to the Variable type. data in your code IS a Variable , and data2 NOT, because the return type of tf.reshape is Tensor .

Decision:

for tensor flow after v1.0

 data = tf.Variable([[1,2,3,4,5], [6,7,8,9,0], [1,2,3,4,5]]) row = tf.gather(data, 2) new_row = tf.concat([row[:2], tf.constant([0]), row[3:]], axis=0) sparse_update = tf.scatter_update(data, tf.constant(2), new_row) 

for tensor flow before v1.0

 data = tf.Variable([[1,2,3,4,5], [6,7,8,9,0], [1,2,3,4,5]]) row = tf.gather(data, 2) new_row = tf.concat(0, [row[:2], tf.constant([0]), row[3:]]) sparse_update = tf.scatter_update(data, tf.constant(2), new_row) 
+2
source share

All Articles