web-dev-qa-db-ja.com

テンソルフローでテンソル(重複する値)を拡大する方法は?

TensorFlowの新人です。このホワイトペーパーでglobal_context抽出を実装しようとしています https://arxiv.org/abs/1506.04579 、これは実際にはフィーチャマップ全体の平均プールであり、1x1フィーチャマップを複製して元のサイズ。イラストは以下の通り

enter image description here

具体的には、想定される動作は以下の通りです。入力:[N、1、1、C]テンソル、ここでNはバッチサイズ、Cはチャネル出力の数:[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はゼロ以外の定数値をパディングできません。

9
jackykuo

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は、この軸で繰り返す回数を意味します
in y繰り返しaxis0 3回
in _y__ axis0を3回繰り返し、axis1を2回繰り返します

最初に_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)
_
18
xxi