【问题标题】:Tensorflow: Undo global average poolingTensorflow:撤消全局平均池化
【发布时间】:2019-04-10 23:01:49
【问题描述】:

在 Tensorflow 中,我在网络末端执行以下全局平均池化:

x_ = tf.reduce_mean(x, axis=[1,2])

我的张量x 的形状为(n, h, w, c),其中n 是输入的数量,wh 对应于宽度和高度尺寸,c 是通道/过滤器的数量.

从张量x 开始,在调用tf.reduce_mean() 之后,张量大小为(n, h, w, c),结果张量的大小为(n, c)

我怎样才能扭转这个过程?如何进行 unpooling 操作?

编辑

这是一个没有按预期工作的示例:

import tensorflow as tf
import numpy as np

n, c = 1, 2 
h, w = 2, 2

x = tf.ones([n, h, w, c])
y = tf.reduce_mean(x, axis=[1,2], keepdims=True)
z = tf.reshape(y, [n, 1, 1, c])
u = tf.tile(z, [n, h, w, c])

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(x)
    print("x", sess.run(x))
    print("\n")
    print(y)
    print("y", sess.run(y))
    print("\n")
    print(z)
    print("z", sess.run(z))
    print("\n")
    print(u)
    print("u", sess.run(u))

输出是:

Tensor("ones:0", shape=(1, 2, 2, 2), dtype=float32)
x [[[[1. 1.]
   [1. 1.]]

  [[1. 1.]
   [1. 1.]]]]


Tensor("Mean:0", shape=(1, 1, 1, 2), dtype=float32)
y [[[[1. 1.]]]]


Tensor("Reshape:0", shape=(1, 1, 1, 2), dtype=float32)
z [[[[1. 1.]]]]


Tensor("Tile:0", shape=(1, 2, 2, 4), dtype=float32)
u [[[[1. 1. 1. 1.]
   [1. 1. 1. 1.]]

  [[1. 1. 1. 1.]
   [1. 1. 1. 1.]]]]

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    您可以使用tf.reshapetf.tile 进行取消池化操作。

    x = tf.random_uniform([n, c])
    y = tf.reshape(x, [n, 1, 1, c])
    z = tf.tile(y, [1, h, w, 1])
    

    在调用tf.reshapetf.tile 之后,从大小为(n, c) 的张量x 开始,生成的张量z 的大小为(n, h, w, c)

    【讨论】:

    • 当我测试你的答案时,张量z 的大小是[n*n, h, w, c*c]?如何检查取消池操作是否正常工作?
    • 显示张量形状。打印(z.shape)
    • 我加了一个例子
    • 对不起,贴错了,修改一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2020-09-05
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多