Python Keras错误:密集层3的输入0与层不兼容::预期最小值ndim=2,发现ndim=1。收到完整表格:[16]

Python Keras错误:密集层3的输入0与层不兼容::预期最小值ndim=2,发现ndim=1。收到完整表格:[16],python,tensorflow,keras,neural-network,Python,Tensorflow,Keras,Neural Network,我想比较两个由特征向量表示的图像。为此,我使用了一些现有的暹罗神经网络代码,最后一部分计算两个特征向量的绝对差异,然后给出一个相似性分数(使用sigmoid函数) 因此,我尝试调整代码,以下是我得到的: # Add a customized layer to compute the absolute difference between the encodings L1_layer = tensorflow.keras.layers.Lambda(lambda tensors:K.abs(ten

我想比较两个由特征向量表示的图像。为此,我使用了一些现有的暹罗神经网络代码,最后一部分计算两个特征向量的绝对差异,然后给出一个相似性分数(使用sigmoid函数)

因此,我尝试调整代码,以下是我得到的:

# Add a customized layer to compute the absolute difference between the encodings
L1_layer = tensorflow.keras.layers.Lambda(lambda tensors:K.abs(tensors[0] - tensors[1]), output_shape=(1,16))
L1_distance = L1_layer(K.constant(vectors(x_train, 0)))

# Add a dense layer with a sigmoid unit to generate the similarity score
prediction = Dense(1,activation='sigmoid', bias_initializer=initializers.Constant(0.1))(L1_distance)

# Connect the inputs with the outputs
siamese_net = Model(inputs= keras.layers.Input(shape=(2, 16)),outputs=prediction)
函数向量(x_列,0)计算包含两个特征向量的numpy数组(每个特征向量也是长度为16的数组,因此函数的最终结果是[[vector1],[vector2]])

当我执行代码时,我在创建致密层的那一行遇到以下错误:

ValueError: Input 0 of layer dense_3 is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: [16]
我真的不明白,因为我已经明确指定了Lambda层的输出大小是(1,16),它有两个密度层所期望的维度

谢谢你的帮助