Keras / tensorflow中CNN的级联向量

时间:2018-07-15 15:54:32

标签: python-3.x tensorflow keras concatenation conv-neural-network

我正在尝试将CNN的平展输出和标量值向量连接起来。我试图通过图像和矢量来影响网络。因此,平整的CNN大小的输出为(1,1024),而我要连接的向量为(1,5)

当然,keras希望它们的大小相似。那么在这种情况下最佳实践是什么?用零将向量扩展到1024?我正在尝试使媒介产生影响,我还能采取其他哪些选择?我正在尝试执行与此处(https://arxiv.org/abs/1603.02199)类似的操作。

1 个答案:

答案 0 :(得分:1)

为什么不将它们在最后一个维度上连接起来以获得形状为(1, 1029)的张量?

from keras.models import Model
from keras.layers import Input, Concatenate

img = Input(shape=(1,1024))
vec = Input(shape=(1,5))
res = Concatenate(axis=-1)([img, vec])
model = Model(inputs=[img, vec], outputs=res)
model.summary()
# _______________________________________________________________________________
# Layer (type)                    Output Shape         Param #     Connected to
# ===============================================================================
# input_1 (InputLayer)            (None, 1, 1024)      0              
# _______________________________________________________________________________
# input_2 (InputLayer)            (None, 1, 5)         0              
# _______________________________________________________________________________
# concatenate_1 (Concatenate)     (None, 1, 1029)      0           input_1[0][0]
#                                                                  input_2[0][0]
# ===============================================================================
# Total params: 0
# Trainable params: 0
# Non-trainable params: 0
# _______________________________________________________________________________
相关问题