2017-02-05 7 views
1

テンソルを2つのdimmで連結したいと思います。テンソルを両方のdimで連結する方法

たとえば、4つの次元の4つのテンソルがあります。すべてのテンソルはテンソルフローのイメージのようなものなので、各次元は[batch_size、image_width_size、image_height_size、image_channel_size]を意味します。

(私は画像の幅と画像の高さについて考慮して、言うことを意味し)、私は次のように出て働きたいバッチサイズおよびチャネルサイズについて考慮せずに
import tensorflow as tf 

image_tensor_1 = 1*tf.ones([60, 2, 2, 3]) 
image_tensor_2 = 2*tf.ones([60, 2, 2, 3]) 
image_tensor_3 = 3*tf.ones([60, 2, 2, 3]) 
image_tensor_4 = 4*tf.ones([60, 2, 2, 3]) 

image_result_wanted = ... # Some operations here 

sess = tf.Session() 
print(sess.run([image_result_wanted]) 

[[1, 1, 2, 2], 
[1, 1, 2, 2], 
[3, 3, 4, 4], 
[3, 3, 4, 4]] 

このように、 image_result_wantedの形状は(60, 4, 4, 3)でなければなりません。

この操作はどのように行うべきですか?

答えて

3

tf.concatを使用すると、必要な軸に沿ってテンソルを連結できます。ここで

import tensorflow as tf 

image_tensor_1 = 1*tf.ones([60, 2, 2, 3]) 
image_tensor_2 = 2*tf.ones([60, 2, 2, 3]) 
image_tensor_3 = 3*tf.ones([60, 2, 2, 3]) 
image_tensor_4 = 4*tf.ones([60, 2, 2, 3]) 

try: 
    temp_1 = tf.concat_v2([image_tensor_1, image_tensor_2], 2) 
    temp_2 = tf.concat_v2([image_tensor_3, image_tensor_4], 2) 
    result = tf.concat_v2([temp_1, temp_2], 1) 
except AttributeError: 
    temp_1 = tf.concat(2, [image_tensor_1, image_tensor_2]) 
    temp_2 = tf.concat(2, [image_tensor_3, image_tensor_4]) 
    result = tf.concat(1, [temp_1, temp_2]) 


sess = tf.Session() 
print sess.run([result[0,:,:,0]]) 
2

本当にどのように1行でこれを行うには考えているので、私は次のように作ってみた:

import tensorflow as tf 

image_tensor_1 = 1 * tf.ones([60, 2, 2, 3]) 
image_tensor_2 = 2 * tf.ones([60, 2, 2, 3]) 
image_tensor_3 = 3 * tf.ones([60, 2, 2, 3]) 
image_tensor_4 = 4 * tf.ones([60, 2, 2, 3]) 

# make two tensors with shapes of [60, 2, 4, 3] 
concat1 = tf.concat(2, [image_tensor_1, image_tensor_2]) 
concat2 = tf.concat(2, [image_tensor_3, image_tensor_4]) 
# stack two tensors together to obtain desired result with shape [60, 4, 4, 3] 
result = tf.concat(1, [concat1, concat2]) 

次のコード:

sess = tf.Session() 
print(sess.run(result[0, :, :, 0])) 

の結果は

[[ 1. 1. 2. 2.] 
[ 1. 1. 2. 2.] 
[ 3. 3. 4. 4.] 
[ 3. 3. 4. 4.]] 

が望ましい。

あまりにも遅い、笑:

+1

申し訳ありません兄 – indraforyou

関連する問題