Python Keras ValueError:检查输入时出错:预期anchor_输入有3个维度,但得到了形状为(18,1)的数组

Python Keras ValueError:检查输入时出错:预期anchor_输入有3个维度,但得到了形状为(18,1)的数组,python,keras,deep-learning,dimensions,siamese-network,Python,Keras,Deep Learning,Dimensions,Siamese Network,我在尝试创建暹罗网络时遇到了一个维度问题 这是我用作自定义损失函数和模型的代码 input_shape = (1, 18) embedding_size = 25 class CosineLossLayer(Layer): def __init__(self, **kwargs): super(CosineLossLayer, self).__init__(**kwargs) def cosine_loss(self, inputs): x,

我在尝试创建暹罗网络时遇到了一个维度问题

这是我用作自定义损失函数和模型的代码

input_shape = (1, 18)
embedding_size = 25

class CosineLossLayer(Layer):
    def __init__(self, **kwargs):
        super(CosineLossLayer, self).__init__(**kwargs)

    def cosine_loss(self, inputs):
        x, y = inputs
        x = K.l2_normalize(x, axis=-1)
        y = K.l2_normalize(y, axis=-1)
        return -K.mean(x * y, axis=-1, keepdims=True)

    def call(self, inputs):
        loss = self.cosine_loss(inputs)
        self.add_loss(loss)
        return loss

def build_network(input_shape, embeddingsize):
    model = models.Sequential()
    print(input_shape)
    model.add(Dense(64, activation="relu", input_shape=input_shape))
    model.add(Dense(64, activation="relu"))
    model.add(Flatten())
    model.add(Dense(embeddingsize, activation=None))
    return model

def build_model(input_shape, network):
    '''
    Define the Keras Model for training 
        Input : 
            input_shape : shape of input images
            network : Neural network to train outputing embeddings

    '''
    print(input_shape)
     # Define the tensors for the three input images
    train_input = Input(input_shape, name="train_input")
    anchor_input = Input(input_shape, name="anchor_input")

    # Generate the encodings (feature vectors) for the three images
    encoded_t = network(train_input)
    encoded_a = network(anchor_input)

    # cosine distance
    loss_layer = CosineLossLayer(name='Cosine_loss_layer')([encoded_a,encoded_t])

    # Connect the inputs with the outputs
    network_train = models.Model(inputs=[anchor_input,train_input],outputs=loss_layer)

    # return the model
    return network_train
当我这样编译和总结它时:

network = build_network(input_shape,embeddingsize=25)
network_train = build_model(input_shape,network)
optimizer = Adam(lr = 0.00006)
network_train.compile(loss=None,optimizer=optimizer)
network_train.summary()
我得到

(18, 1)
(18, 1)
WARNING:tensorflow:Output Cosine_loss_layer missing from loss dictionary. We assume this was done on purpose. The fit and evaluate APIs will not be expecting any data to be passed to Cosine_loss_layer.
Model: "model_4"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
anchor_input (InputLayer)       [(None, 18, 1)]      0                                            
__________________________________________________________________________________________________
train_input (InputLayer)        [(None, 18, 1)]      0                                            
__________________________________________________________________________________________________
sequential_4 (Sequential)       (None, 25)           33113       train_input[0][0]                
                                                                 anchor_input[0][0]               
__________________________________________________________________________________________________
Cosine_loss_layer (CosineLossLa (None, 1)            0           sequential_4[2][0]               
                                                                 sequential_4[1][0]               
==================================================================================================
Total params: 33,113
Trainable params: 33,113
Non-trainable params: 0
__________________________________________________________________________________________________
这正是我想要的

但是,当我尝试使用数据拟合模型时,由于维度错误,我无法执行以下操作:

network_train.fit(x =[train_1, train_2], y=[X_tr1, X_tr2], epochs = 50, batch_size = 1)

    .
    .
    .
opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    571                            ': expected ' + names[i] + ' to have ' +
    572                            str(len(shape)) + ' dimensions, but got array '
--> 573                            'with shape ' + str(data_shape))
    574         if not check_batch_axis:
    575           data_shape = data_shape[1:]

ValueError: Error when checking input: expected anchor_input to have 3 dimensions, but got array with shape (91965, 18)

当我在网上查找时,我不明白这里发生了什么,为什么我的网络需要三维空间,有人能解释一下吗

通过这样定义输入形状:input_shape=1,18 您正在指定您的模型将接受一批二维向量


因此,如果您的输入是1D,请这样定义:input_shape=18,

input_shape=1,18这一行告诉您的模型,一个输入具有形状1,18。但是,模型的输入形状为None、18、1。没有人在那里。你不必指定有多少样品和东西。我很确定如果你把输入形状改为无,你的代码会运行得很好,18我理解你的观点,但是这样做我得到了错误的值错误:应该定义输入的最后一个维度。找不到。“啊,对不起,可能是我错过了正确的语法: