当一层的尺寸为(None,512)而另一层的尺寸为(18577,4)时,如何在keras中连接2层。我尝试使用串联
concat_layer = Concatenate()([z1,agp]
但这给我一个错误提示:
ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 512), (18577, 4)]
该模型如下所示:
a1= (Convolution2D(32, filter_dim, activation='linear',
padding='same',kernel_regularizer=regularizers.l2(reg)))(input_img)
b1 = (BatchNormalization())(a1)
c1 = (PReLU())(b1)
d1 = (Convolution2D(32, filter_dim, activation='linear',kernel_regularizer=regularizers.l2(reg)))(c1)
e1 = (BatchNormalization())(d1)
f1 = (PReLU())(e1)
g1 = (MaxPooling2D(pool_size=(2,2)))(f1)
h1 = (Dropout(0.2))(g1)
i1= (Convolution2D(64, filter_dim, activation='linear', padding='same',kernel_regularizer=regularizers.l2(reg)))(h1)
j1 = (BatchNormalization())(i1)
k1 = (PReLU())(j1)
l1 = (Convolution2D(64, filter_dim, activation='linear',kernel_regularizer=regularizers.l2(reg)))(k1)
m1 = (BatchNormalization())(k1)
n1 = (PReLU())(m1)
o1 = (MaxPooling2D(pool_size=(2,2)))(n1)
p1 = (Dropout(0.2))(o1)
q1= (Convolution2D(128, filter_dim, activation='linear', padding='same',kernel_regularizer=regularizers.l2(reg)))(p1)
r1=q1
s1 = (BatchNormalization())(r1)
t1 = (PReLU())(s1)
u1 = (Convolution2D(128, filter_dim, activation='linear',kernel_regularizer=regularizers.l2(reg)))(t1)
v1 = (BatchNormalization())(u1)
w1 = (PReLU())(v1)
x1 = (MaxPooling2D(pool_size=(3,3)))(w1)
y1 = (Dropout(0.2))(x1)
z1 = (Flatten())(y1)
agp=tf.convert_to_tensor(agp,np.float32)
z1 = Concatenate(axis=1)([z1,agp])
a2 = (Dense(128, activation='linear',kernel_regularizer=regularizers.l2(reg)))(z1)
b2 = (BatchNormalization())(a2)
c2 = (PReLU())(b2)
d2 = (Dropout(0.2))(c2)
e2 = (Dense(32, activation='linear',kernel_regularizer=regularizers.l2(reg)))(d2)
f2 = (BatchNormalization())(e2)
g2 = (PReLU())(f2)
h2 = (Dropout(0.3))(g2)
我的输入图像的尺寸为(32,32,3)。我想用agp(18577,4)连接z1(None,512)
最佳答案
#!/usr/bin/env python
def create_model(nb_classes, input_shape):
"""Create a NN model."""
# from keras.layers import Dropout
from keras.layers import Activation, Input
from keras.layers import Dense, Concatenate
from keras.models import Model
input_ = Input(shape=input_shape)
x = input_
# Branch in two directions - this can be more
# complex, of course
x1 = Dense(512, activation='relu')(x)
x2 = Dense(4, activation='relu')(x)
# And this is how you use concatenation
x = Concatenate(axis=-1)([x1, x2])
# And then finish it
x = Dense(nb_classes, activation='softmax')(x)
model = Model(inputs=input_, outputs=x)
return model
model = create_model(10, (512, ))
print(model.summary())
给
Using TensorFlow backend.
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
input_1 (InputLayer) (None, 512) 0
____________________________________________________________________________________________________
dense_1 (Dense) (None, 512) 262656 input_1[0][0]
____________________________________________________________________________________________________
dense_2 (Dense) (None, 4) 2052 input_1[0][0]
____________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 516) 0 dense_1[0][0]
dense_2[0][0]
____________________________________________________________________________________________________
dense_3 (Dense) (None, 10) 5170 concatenate_1[0][0]
====================================================================================================
Total params: 269,878
Trainable params: 269,878
Non-trainable params: 0
____________________________________________________________________________________________________
None
关于machine-learning - 串联Keras(None,512)和(18577,4)中的层,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48478784/