tensorflowサイクルtensorの値を変更する

1728 ワード

tfを使用する.concat()は4次元tensorのループ付与を実現する
alist=[[[[1,1,1],[2,2,2],[3,3,3]],[[4,4,4],[5,5,5],[6,6,6]]],[[[7,7,7],[8,8,8],[9,9,9]],[[10,10,10],[11,11,11],[12,12,12]]]] #2,2,3,3-n,c,h,w
kenel=(np.asarray(alist)*2).tolist()
print(kenel)
inputs=tf.constant(alist,dtype=tf.float32)
kenel=tf.constant(kenel,dtype=tf.float32)
inputs=tf.transpose(inputs,[0,2,3,1]) #n,h,w,c
kenel=tf.transpose(kenel,[0,2,3,1]) #n,h,w,c
uints=inputs.get_shape()
h=int(uints[1])
w=int(uints[2])
encoder_output=[]
for b in range(int(uints[0])):
    encoder_output_c=[]
    for c in range(int(uints[-1])):
        one_channel_in = inputs[b, :, :, c]
        one_channel_in = tf.reshape(one_channel_in, [1, h, w, 1])
        one_channel_kernel = kenel[b, :, :, c]
        one_channel_kernel = tf.reshape(one_channel_kernel, [h, w, 1, 1])
        encoder_output_cc = tf.nn.conv2d(input=one_channel_in, filter=one_channel_kernel, strides=[1, 1, 1, 1], padding="SAME")
        if c==0:
            encoder_output_c=encoder_output_cc
        else:
            encoder_output_c=tf.concat([encoder_output_c,encoder_output_cc],axis=3)

    if b==0:
        encoder_output=encoder_output_c
    else:
        encoder_output = tf.concat([encoder_output, encoder_output_c], axis=0)

with tf.Session() as sess:
    print(sess.run(tf.transpose(encoder_output,[0,3,1,2])))
    print(encoder_output.get_shape())

出力:
[[[[  32.   48.   32.]
   [  56.   84.   56.]
   [  32.   48.   32.]]

  [[ 200.  300.  200.]
   [ 308.  462.  308.]
   [ 200.  300.  200.]]]


 [[[ 512.  768.  512.]
   [ 776. 1164.  776.]
   [ 512.  768.  512.]]

  [[ 968. 1452.  968.]
   [1460. 2190. 1460.]
   [ 968. 1452.  968.]]]]
(2, 3, 3, 2)