keras暹罗模型中的形状不匹配

时间:2018-06-05 03:38:27

标签: tensorflow machine-learning neural-network keras keras-layer

我正在尝试在具有三重损失函数的mnist数据集上实现siamese模型,并且在管理形状方面存在一些困难,可能在模型的最终lambda层中。 这就是我到目前为止所做的......

#Dataset
pairs_train.shape #(54200, 3, 28, 28)->each sample contain 3 images(anchor,positive,negative)
pairs_test.shape #(8910, 3, 28, 28)

#Base model
input_shape = (28,28)
input = Input(shape = input_shape)
x = Flatten()(input)
x = Dense(128 , activation = "relu")(x)
x = Dropout(0.1)(x)
x = Dense(128 , activation = "relu")(x)
x = Dropout(0.1)(x)
x = Dense(128 , activation = "relu")(x)

#Sharing
base_network = Model(input , x)
input_1 = Input(shape = input_shape)
input_2 = Input(shape = input_shape)
input_3 = Input(shape = input_shape)

output_1 = base_network(input_1)
output_2 = base_network(input_2)
output_3 = base_network(input_3)

#Function to stack final outputs for calculating triplet loss
def func(x):
    return K.stack([x[0] , x[1] , x[2]] , axis = -1)

#Final lamda layer -> This is where problem lies I think.
finalOutput = Lambda(func)([output_1 , output_2 , output_3])

#Final model
finalModel = Model(inputs=[input_1 , input_2 , input_3] , outputs = finalOutput)

finalModel.compile(loss = triplet_loss , optimizer=RMSprop() , metrics = ["accuracy"])
y_dummie = np.ones(54200)
finalModel.fit([pairs_train[:,0] , pairs_train[:,1] , pairs_train[:,2]] , np.ones(54200),
         batch_size = 128 , epochs = 200)

训练时我收到以下错误。

Cannot feed value of shape (128, 1) for Tensor 'lambda_5_target:0', which has shape '(?, ?, ?)'

请帮忙......

0 个答案:

没有答案