可以重复元素,然后连接。
from keras.layers import Input, Lambda, Concatenate
from keras.models import Model
import keras.backend as K
rows = 2
cols = 2
features = 2
row = Input((1, cols, features))
col = Input((rows, 1, features))
row_repeated = Lambda(lambda x: K.repeat_elements(x, rows, axis=1))(row)
col_repeated = Lambda(lambda x: K.repeat_elements(x, cols, axis=2))(col)
out = Concatenate()([row_repeated, col_repeated])
model = Model(inputs=[row,col], outputs=out)
model.summary()
实验:
import numpy as np
x = np.array([1,2,3,4]).reshape((1, 1, 2, 2))
y = np.array([5,6,7,8]).reshape((1, 2, 1, 2))
model.predict([x, y])
#array([[[[1., 2., 5., 6.],
# [3., 4., 5., 6.]],
#
# [[1., 2., 7., 8.],
# [3., 4., 7., 8.]]]], dtype=float32)