【问题标题】:How to enlarge a tensor(duplicate value) in tensorflow?如何在张量流中放大张量(重复值)?
【发布时间】:2017-07-31 08:33:03
【问题描述】:

我是 TensorFlow 的新手。我正在尝试实现本文https://arxiv.org/abs/1506.04579 中的 global_context 提取,这实际上是对整个特征图的平均池化,然后将 1x1 特征图复制回原始大小。图示如下

具体来说,预期的操作如下。 输入:[N, 1, 1, C] 张量,其中 N 是批量大小,C 是通道数 output: [N, H, W, C] 张量,其中H, W是原始特征图的高度和宽度,输出的所有H * W值与1x1输入相同。

例如,

    [[1, 1, 1]
1 -> [1, 1, 1]
     [1, 1, 1]]

我不知道如何使用 TensorFlow 来做到这一点。 tf.image.resize_images 需要 3 个通道,tf.pad 不能填充除零以外的常量值。

【问题讨论】:

    标签: tensorflow neural-network deep-learning conv-neural-network


    【解决方案1】:

    tf.tile 可以帮到你

    x = tf.constant([[1, 2, 3]]) # shape (1, 3)
    y = tf.tile(x, [3, 1]) # shape (3, 3)
    y_ = tf.tile(x, [3, 2]) # shape (3, 6)
    
    with tf.Session() as sess:
        a, b, c = sess.run([x, y, y_])
    
    >>>a
    array([[1, 2, 3]], dtype=int32)
    >>>b
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]], dtype=int32)
    >>>c
    array([[1, 2, 3, 1, 2, 3],
           [1, 2, 3, 1, 2, 3],
           [1, 2, 3, 1, 2, 3]], dtype=int32)
    

    tf.tile(input, multiples, name=None)
    multiples 表示你想在这个轴上重复多少次
    y 重复axis0 3 次
    y_ 中重复axis0 3 次,axis1 2 次​​p>

    您可能需要先使用tf.expand_dim

    是的,它接受动态形状

    x = tf.placeholder(dtype=tf.float32, shape=[None, 4])
    x_shape = tf.shape(x)
    y = tf.tile(x, [3 * x_shape[0], 1])
    
    with tf.Session() as sess:
        x_ = np.array([[1, 2, 3, 4]])
        a = sess.run(y, feed_dict={x:x_})
    >>>a
    array([[ 1.,  2.,  3.,  4.],
           [ 1.,  2.,  3.,  4.],
           [ 1.,  2.,  3.,  4.]], dtype=float32)
    

    【讨论】:

    • 谢谢,我应该早点找到这个。是否可以使用具有动态张量形状的 tf.tile?例如,tf.tile(input, [1, ori.get_shape()[1], ori.get_shape()[2], 1])。我不想修复网络中的放大率。
    • 如果我没记错的话,你的第一个代码块中的第二行应该有注释 #shape (3,3)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 2023-01-11
    • 1970-01-01
    • 2021-12-08
    • 2020-02-08
    • 2020-10-13
    • 1970-01-01
    相关资源
    最近更新 更多