import numpy as np
import shutil
import keras.backend as K
import tensorflow as tf
def test():
grid_shape =(2,3)
grid_y = np.tile(np.reshape(np.arange(0, stop=grid_shape[0]), [-1, 1, 1, 1]),
[1, grid_shape[1], 1, 1])
# print(grid_y)
# print('11111111111111')
grid_x = np.tile(np.reshape(np.arange(0, stop=grid_shape[1]), [1, -1, 1, 1]),
[grid_shape[0], 1, 1, 1])
# print(grid_x)
# print('\n22222222222222\n')
grid = np.concatenate([grid_x, grid_y])
grid1=K.concatenate([K.variable(grid_x),K.variable(grid_y)])
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(grid)
print('\n22222222222222\n')
print(sess.run(grid1))
输出结果:
[[[[0]]
[[1]]
[[2]]]
[[[0]]
[[1]]
[[2]]]
[[[0]]
[[0]]
[[0]]]
[[[1]]
[[1]]
[[1]]]]
22222222222222
[[[[0. 0.]]
[[1. 0.]]
[[2. 0.]]]
[[[0. 1.]]
[[1. 1.]]
[[2. 1.]]]]
np.concatenate 默认按第一个维度拼接,K.concatenate默认按最后一个维度拼接