Keras:model.fit()在siamese_model中出现多个输入错误

时间:2019-03-18 17:04:52

标签: python keras deep-learning

我是Keras和暹罗网络体系结构的新手。我开发了一个具有三个输入和一个输出的暹罗网络,如下所示。

def get_siamese_model(input_shape):


# Define the tensors for the three input phrases
anchor = Input(input_shape, name='anchor')
positive = Input(input_shape, name='positive')
negative = Input(input_shape, name='negative')

# Convolutional Neural Network
model = Sequential()
model.add(Conv2D(64, kernel_size=(2, 2), activation='relu', input_shape=input_shape, padding='same'))
model.add(Conv2D(32, kernel_size=(2, 2), activation='relu', padding='same'))
model.add(Conv2D(16, kernel_size=(2, 2), activation='relu', padding='same'))
model.add(Conv2D(8, kernel_size=(2, 2), activation='relu', padding='same'))
model.add(Conv2D(4, kernel_size=(2, 2), activation='relu', padding='same'))
model.add(Conv2D(2, kernel_size=(2, 2), activation='relu', padding='same'))
model.add(Conv2D(1, kernel_size=(2, 2), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2,1)))
model.add(Flatten())

# Generate the encodings (feature vectors) for the three phrases
anchor_out = model(anchor)
positive_out = model(positive)
negative_out = model(negative)

# Add a customized layer to combine individual output
concat = Lambda(lambda tensors:K.concatenate((tensors[0],tensors[1],tensors[2]),0))
output = concat([anchor_out, positive_out, negative_out])


# Connect the inputs with the outputs
siamese_net = Model(inputs=[anchor,positive,negative],outputs=output)

#plot the model
plot_model(siamese_net, to_file='siamese_net.png',show_shapes=True, show_layer_names=True)

#Error optimization
siamese_net.compile(optimizer=Adam(), 
 loss=triplet_loss)

# return the model
return siamese_net

在使用model.fit()时,我编写了以下代码:

model = get_siamese_model(input_shape)
X = {
    'anchor' : anchor,
    'positive' : positive,
    'negative' : negative
}

model.fit(np.asarray(X), Y)

我收到以下错误消息:

ValueError: Error when checking model input: 
The list of Numpy arrays that you are passing to your model is not the size the model expected. 
Expected to see 3 array(s), but instead got the following list of 1 arrays: [array({'anchor': array([[[[ 4.49218750e-02]...

感谢您的帮助。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

以下代码对我有用。因为您的名字是(anchor, positive, negative),所以您在传递输入时可以直接将它们用作字典的键。另外,您应该使用Keras中的concatenate层,而不是定义Lambda。请注意,出于本示例的目的,我更改了损失。

from keras.layers import Input, Conv2D, MaxPooling2D, Flatten, concatenate
from keras.models import Model, Sequential
from keras.optimizers import Adam
from keras.losses import mean_squared_error
import numpy as np

def get_siamese_model(input_shape):


    # Define the tensors for the three input phrases
    anchor = Input(input_shape, name='anchor')
    positive = Input(input_shape, name='positive')
    negative = Input(input_shape, name='negative')

    # Convolutional Neural Network
    model = Sequential()
    model.add(Conv2D(64, kernel_size=(2, 2), activation='relu', input_shape=input_shape, padding='same'))
    model.add(Conv2D(32, kernel_size=(2, 2), activation='relu', padding='same'))
    model.add(Conv2D(16, kernel_size=(2, 2), activation='relu', padding='same'))
    model.add(Conv2D(8, kernel_size=(2, 2), activation='relu', padding='same'))
    model.add(Conv2D(4, kernel_size=(2, 2), activation='relu', padding='same'))
    model.add(Conv2D(2, kernel_size=(2, 2), activation='relu', padding='same'))
    model.add(Conv2D(1, kernel_size=(2, 2), activation='relu', padding='same'))
    model.add(MaxPooling2D(pool_size=(2,1)))
    model.add(Flatten())

    # Generate the encodings (feature vectors) for the three phrases
    anchor_out = model(anchor)
    positive_out = model(positive)
    negative_out = model(negative)

    # Add a concatenate layer
    output = concatenate([anchor_out, positive_out, negative_out])

    # Connect the inputs with the outputs
    siamese_net = Model(inputs=[anchor,positive,negative],outputs=output)

    # Error optimization
    siamese_net.compile(optimizer=Adam(), loss=mean_squared_error)

    # Summarize model
    siamese_net.summary()

    # Return the model
    return siamese_net

input_shape = (100, 100, 1)
model = get_siamese_model(input_shape)
X = {'anchor': np.ones((5, 100, 100, 1)),   # define input as dictionary
     'positive': np.ones((5, 100, 100, 1)), 
     'negative': np.ones((5, 100, 100, 1))}
Y = np.ones((5, 15000))
model.fit(X, Y)                        # use a dictionary
model.fit([i for i in X.values()], Y)  # use a list