当我尝试训练具有2个输入和一个输出的模型时,出现此错误。 该模型是这样的:
input_shape = (data.shape[1],)
print(input_shape) #(474,)
left_input = Input(input_shape)
right_input = Input(input_shape)
convnet = Sequential()
convnet.add(Dense(numNeuronsFirstTwo, kernel_regularizer=l2(0.001)))
convnet.add(Dense(numNeuronsFirstTwo, kernel_regularizer=l2(0.001)))
convnet.add(Dropout(dropoutRate))
convnet.add(Dense(numNeurons, kernel_regularizer=l2(0.001)))
encoded_l = convnet(left_input)
encoded_r = convnet(right_input)
#layer to merge two encoded inputs with the l1 distance between them
L1_layer = Lambda(lambda tensors:K.abs(tensors[0] - tensors[1]))
#call this layer on list of two input tensors.
L1_distance = L1_layer([encoded_l, encoded_r])
prediction = Dense(1,activation='sigmoid',bias_initializer='zeros')(L1_distance)
siamese_net = Model(inputs=[left_input,right_input],outputs=prediction)
optimizer = Adam(0.00006)
#//TODO: get layerwise learning rates and momentum annealing scheme described in paperworking
siamese_net.compile(loss="binary_crossentropy",optimizer=optimizer)
模型摘要如下:
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (None, 474) 0
__________________________________________________________________________________________________
input_2 (InputLayer) (None, 474) 0
__________________________________________________________________________________________________
sequential_1 (Sequential) (None, 40) 22280 input_1[0][0]
input_2[0][0]
__________________________________________________________________________________________________
lambda_1 (Lambda) (None, 40) 0 sequential_1[1][0]
sequential_1[2][0]
__________________________________________________________________________________________________
dense_4 (Dense) (None, 1) 41 lambda_1[0][0]
==================================================================================================
我的两个输入是两个2个Numpy数组trainValiData[1]
和trainValiData[2]
,每个数组的形状为(474,)
,但出现上述错误。
我尝试通过以下操作将尺寸更改为(1,474)
:
train1 = np.array([trainValiData[1]])
train2 = np.array([trainValiData[2]])
#train1.shape --> (1,474)
labels = [1]
labels = np.asarray(labels)
pairs = [train1, train2]
siamese_net.fit(pairs, labels) #Still get the same error.
请告诉我该如何解决。谢谢!