Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Tensorflow, how to assign values in Tensor according to the indices?

Tags:

I want to assign values in a tensor according to the indices.

For example, According to the pooling values and the corresponding indices output of tf.nn.max_pool_with_argmax, I want to put these pooling values back into the original unpooling Tensor with the indices.

I find the output indices of tf.nn.max_pool_with_argmax is flattened. One question: How to unravel them back into the coordinates in Tensorflow?

Another question: How to assign each value of the pooling tensor to the position of the original unpooling tensor in Tensorflow, given the indices?

Thank you very much.

I tried to make the codes to achieve that, but I can just use numpy. I do not how to obtain the flattened indices after the tf.nn.max_pool_with_argmax and assigning into the unpooling tensor in Tensorflow.

ksize = 3 stride = 1  input_image = tf.placeholder(tf.float32, name='input_image')  #conv1 kernel = tf.Variable(tf.truncated_normal([ksize, ksize, 3, 16],stddev=0.1),                     name='kernel') conv = tf.nn.conv2d(input_image, kernel, [1,stride,stride,1], padding='SAME') biases = tf.Variable(tf.constant(0.0, shape = [16]), name = 'biases') bias = tf.nn.bias_add(conv, biases) conv1 = tf.nn.relu(bias, name='conv1')  #pool1 pool1, pool1_indices = tf.nn.max_pool_with_argmax(conv1, ksize=[1, 2, 2, 1],                                                    strides=[1, 2, 2, 1],                                                    padding='SAME', name='pool1')  #upsample by assigning the values of pool1 to the position in unpooling Tensor according to pool1_indices                                                 indices = pool1_indices unravel_pool1_indices = np.unravel_index(indices,[4,32,32,16]) unravel_pool1_coordinates = np.array(unravel_pool1_indices) coor_shape = np.shape(unravel_pool1_coordinates) unravel_pool1_coordinates = np.reshape(unravel_pool1_coordinates,(coor_shape[0],coor_shape[1]*coor_shape[2]*coor_shape[3]*coor_shape[4])) unravel_pool1_coordinates = unravel_pool1_coordinates.T  values = pool1 values = np.reshape(values,(np.size(values)))  up1 = tf.constant(0.0, shape = [4,32,32,16]) delta = tf.SparseTensor(unravel_pool1_coordinates, values, shape = [4,32,32,16])  result = up1 + tf.sparse_tensor_to_dense(delta)   with tf.Session() as session:     session.run(tf.initialize_all_variables())     test_image = np.random.rand(4,32,32,3)     sess_outputs = session.run([pool1, pool1_indices],                                {input_image.name: test_image}) 
like image 227
karl_TUM Avatar asked Jun 07 '16 12:06

karl_TUM


People also ask

How do I assign a value in TensorFlow?

Tensorflow variables represent the tensors whose values can be changed by running operations on them. The assign() is the method available in the Variable class which is used to assign the new tf. Tensor to the variable. The new value must have the same shape and dtype as the old Variable value.

Can you index tensors?

Single element indexing for a 1-D tensors works mostly as expected. Like R, it is 1-based. Unlike R though, it accepts negative indices for indexing from the end of the array. (In R, negative indices are used to remove elements.)


1 Answers

There's a pending PR that should fix this:

https://github.com/tensorflow/tensorflow/issues/1793

like image 149
Pete Warden Avatar answered Sep 19 '22 20:09

Pete Warden