Model error: Layer model_1 expects 1 input(s), but it received 2 input tensors

Model error: Layer model_1 expects 1 input(s), but it received 2 input tensors

hist_model = Model.fit(x=train_average, y=train_zero,
                  epochs=5,
                  batch_size=256,
                  verbose = 2, 
                  validation_data=(train_average, validate))

我正在使用自动编码器模型进行推荐。当我 运行 上面的代码时,我在 validation_data 上收到以下错误。我正在使用 google colab。

 /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1298 test_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1282 run_step  *
        outputs = model.test_step(data)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1241 test_step  *
        y_pred = self(x, training=False)
    /usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:989 __call__  *
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py:197 assert_input_compatibility  *
        raise ValueError('Layer ' + layer_name + ' expects ' +

    ValueError: Layer model_1 expects 1 input(s), but it received 2 input tensors.

我需要帮助。

你能告诉我们你的神经网络模型的代码和你给它的数据类型吗?我认为问题在于您提供的数据由 2 个张量组成,但您的模型输入层被编程为仅接收 1 个张量数据。

这是模型


# Convert data types from int64 to float32 to use as tensor inputs for Keras model
train_zero = tf.convert_to_tensor(train_zero, dtype=tf.float32)
train_one = tf.convert_to_tensor(train_one, dtype=tf.float32)
train_two = tf.convert_to_tensor(train_two, dtype=tf.float32)
#train_three = tf.convert_to_tensor(train_three, dtype=tf.float32)
#train_four = tf.convert_to_tensor(train_four, dtype=tf.float32)
#train_five = tf.convert_to_tensor(train_five, dtype=tf.float32)
train_average = tf.convert_to_tensor(train_average, dtype=tf.float32)
validate = tf.convert_to_tensor(validate, dtype=tf.float32)
test = tf.convert_to_tensor(test, dtype=tf.float32)



def AutoRec(X, reg, first_activation, last_activation):
  
    input_layer = x = Input(shape=(X.shape[1],), name='UserRating')
    x = Dense(500, activation=first_activation, name='LatentSpace', kernel_regularizer=regularizers.l2(reg))(x)
    output_layer = Dense(X.shape[1], activation=last_activation, name='UserScorePred', kernel_regularizer=regularizers.l2(reg))(x)
    model = Model(input_layer, output_layer)

    return model

AutoRec = AutoRec(train_zero, 0.0005, 'elu', 'elu')

AutoRec.compile(optimizer = Adam(lr=0.0001), loss=masked_mse, metrics=[masked_rmse_clip])
 
AutoRec.summary()


hist_model = AutoRec.fit(x=train_average, y=train_zero,
                  epochs=5,
                  batch_size=256,
                  verbose = 2, 
                  validation_data=(train_average, validate))

您应该尝试更改输入层的形状,或者查看它们在您的数据中是否存在问题。 另外,我不知道你是否需要它,但你的模型只有输入和输出层,也许你忘记了在“AutoRec”函数中添加你的密集层'x'。

我能够通过首先在外部定义它并使用它来修复这个错误

data_valid =(train_average, validate)

然后

hist_model = Model.fit(x=train_average, y=train_zero,
                  epochs=5,
                  batch_size=256,
                  verbose = 2, 
                  validation_data=data_valid)