简体   繁体   English

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

[英]Keras: model.fit() getting error for multiple inputs in siamese_model

I am new to Keras and the Siamese network architecture. 我是Keras和Siamese网络架构的新手。 I have developed a Siamese network with three inputs and one output as follows. 我开发了一个具有三个输入和一个输出的暹罗网络,如下所示。

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

while using model.fit() I have written following code: 使用model.fit()我编写了以下代码:

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

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

I am getting following error message: 我收到以下错误消息:

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]...

Any help is appreciated. 任何帮助表示赞赏。 Thank you in advance. 先感谢您。

The following code works for me. 以下代码对我有用。 Because your names are (anchor, positive, negative) , you can use those directly as the keys to your dictionary when passing input. 因为您的名字是(anchor, positive, negative) ,所以在传递输入时,可以直接将它们用作字典的键。 Also, you should make use of the concatenate layer in Keras instead of defining a Lambda . 另外,您应该使用Keras中的concatenate层,而不是定义Lambda Note that I changed the loss for purposes of this example. 请注意,出于本示例的目的,我更改了损失。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Keras 中的多个输入的标签赋予 model.fit() function? - How to give labels of multiple inputs in Keras to model.fit() function? model.fit() Keras 分类多输入-单输出给出错误:AttributeError: 'NoneType' 对象没有属性 'fit' - model.fit() Keras Classification Multiple Inputs-Single Output gives error: AttributeError: 'NoneType' object has no attribute 'fit' 在Keras中调用model.fit输入不同形状的输入吗? - Call model.fit in Keras for inputs of different shapes? Keras model.fit UnboundLocalError - Keras model.fit UnboundLocalError 在 Keras model.fit 中将训练数据指定为元组 (x, y) 的正确方法,具有多个输入和输出 - Correct way to specify training data as tuple (x, y) in Keras model.fit with multiple inputs and outputs 带有 Model.Fit() 的 Keras InvalidArgumentError - Keras InvalidArgumentError With Model.Fit() Tensorflow Keras:运行model.fit时出现尺寸/形状错误 - Tensorflow Keras: Dimension/Shape Error when running model.fit 我可以在 keras 中多次调用 model.fit 吗? - Can i call model.fit multiple times in keras? 关于在Keras中调用model.fit时出现UnicodeDecodeError错误 - regarding UnicodeDecodeError error while invoking model.fit in Keras Keras LSTM批次大小和模型.fit() - Keras LSTM Batch Size and model.fit()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM