【问题标题】:Model error: Layer model_1 expects 1 input(s), but it received 2 input tensors模型错误:层 model_1 需要 1 个输入,但它接收到 2 个输入张量
【发布时间】:2021-06-16 09:05:01
【问题描述】:
hist_model = Model.fit(x=train_average, y=train_zero,
                  epochs=5,
                  batch_size=256,
                  verbose = 2, 
                  validation_data=(train_average, validate))

我正在使用自动编码器模型进行推荐。当我运行上面的代码时,我在validation_data上收到以下错误。我正在使用谷歌 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.

我需要帮助。

【问题讨论】:

    标签: python tensorflow keras deep-learning autoencoder


    【解决方案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))
    
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        您能否向我们展示您的 NN 模型的代码以及您提供给它的数据类型?我认为问题在于您提供的数据由 2 个张量组成,但您的模型输入层被编程为仅接收 1 个张量数据。

        【讨论】:

          【解决方案4】:

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

          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)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-07-14
            • 2020-07-15
            • 2022-09-28
            • 2021-12-20
            • 1970-01-01
            • 1970-01-01
            • 2021-09-28
            • 2021-09-02
            相关资源
            最近更新 更多