Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do slice assignment in Tensorflow

I found that Tensorflow provides scatter_update() to assign values to the slice of a tensor in the 0 dimension. For example, if the tensor T is three dimensional, I can assign value v[1, :, :] to T[i, :, :].

a = tf.Variable(tf.zeros([10,36,36]))    value = np.ones([1,36,36])    d = tf.scatter_update(a,[0],value)  with tf.Session() as sess:     sess.run(tf.initialize_all_variables())     print a.eval()     sess.run(d)     print a.eval() 

But how to assign values v[1,1,:] to T[i,j,:]?

a = tf.Variable(tf.zeros([10,36,36]))    value1 = np.random.randn(1,1,36)     e = tf.scatter_update(a,[0],value1) #Error  with tf.Session() as sess:     sess.run(tf.initialize_all_variables())     print a.eval()     sess.rum(e)     print a.eval() 

Is there any other function that TF provide or a simple way to do this?

like image 322
user270700 Avatar asked Aug 26 '16 03:08

user270700


People also ask

What is a slice in Tensorflow?

Used in the notebooksThis operation extracts a slice of size size from a tensor input_ starting at the location specified by begin . The slice size is represented as a tensor shape, where size[i] is the number of elements of the 'i'th dimension of input_ that you want to slice.

Can you slice tensors?

You can use tf. slice on higher dimensional tensors as well. You can also use tf. strided_slice to extract slices of tensors by 'striding' over the tensor dimensions.

What is slice assignment?

Slice assignment is a little-used, beautiful Python feature to replace a slice with another sequence. Simply select the slice you want to replace on the left and the values to replace it on the right side of the equation.

How do you transpose in Tensorflow?

transpose(x, perm=[1, 0]) . As above, simply calling tf. transpose will default to perm=[2,1,0] . To take the transpose of the matrices in dimension-0 (such as when you are transposing matrices where 0 is the batch dimension), you would set perm=[0,2,1] .


1 Answers

Currently, you can do slice assignment for variables in TensorFlow. There is no specific named function for it, but you can select a slice and call assign on it:

my_var = my_var[4:8].assign(tf.zeros(4)) 

First, note that (after having looked at the documentation) it seems that the return value of assign, even when applied to a slice, is always a reference to the whole variable after applying the update.

EDIT: The information below is either deprecated, imprecise or was always wrong. The fact is that the returned value of assign is a tensor that can be readily used and already incorporates the dependency to the assignment, so simply evaluating that or using it in further operations will ensure it gets executed without need for an explicit tf.control_dependencies block.


Note, also, that this will only add the assignment op to the graph, but will not run it unless it is explicitly executed or set as a dependency of some other operation. A good practice is to use it in a tf.control_dependencies context:

with tf.control_dependencies([my_var[4:8].assign(tf.zeros(4))]):     my_var = tf.identity(my_var) 

You can read more about it in TensorFlow issue #4638.

like image 114
jdehesa Avatar answered Sep 20 '22 15:09

jdehesa