在Tensorflow中,如何根据索引在Tensor中分配值? [英] In Tensorflow, how to assign values in Tensor according to the indices?

查看:522
本文介绍了在Tensorflow中,如何根据索引在Tensor中分配值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据索引分配张量值。

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

例如,
根据池值和<一个href = https://www.tensorflow.org/versions/r0.9/api_docs/python/nn.html#max_pool_with_argmax> tf.nn.max_pool_with_argmax ,我想将这些合并值放回

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.

我发现 tf.nn.max_pool_with_argmax 的输出索引是平坦的。
一个问题:如何将它们重新分解成Tensorflow中的坐标?

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?

另一个问题:如何将合并张量的每个值分配给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?

非常感谢你。

我试图使代码来实现这一点,但是我只能使用 numpy 。在 tf.nn.max_pool_with_argmax 并分配到Tensorflow的解压缩张量之后,我不怎么获得平坦的索引。

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})


推荐答案

同一问题在 tf.unravel_index中列出(原为:tf.argmin,所有维度)#2075

这篇关于在Tensorflow中,如何根据索引在Tensor中分配值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆