【问题标题】:TensorFlow DNN on Colab (ValueError: Expect x to be a non-empty array or dataset)Colab 上的 TensorFlow DNN(ValueError: Expect x to be an non-empty array or dataset)
【发布时间】:2021-08-24 09:49:56
【问题描述】:

我正在尝试关注此博客post。我想使用 DNN 预测客户生命周期价值。当我运行将 DNN 模型拟合到我的数据集的代码时,我遇到了错误。我对机器学习还很陌生,我试图尽职尽责地了解代码在做什么。

#DNN
def build_model():
    model = keras.Sequential([
    layers.Dense(32, activation='relu', input_shape=[len(X_train.columns), ]),
    layers.Dropout(0.3),
    layers.Dense(32, activation='relu'),
    layers.Dense(1)
    ])

    optimizer = tf.keras.optimizers.Adam(0.001)

    model.compile(loss='mse',
            optimizer=optimizer,
            metrics=['mae', 'mse'])
    
    return model

# The patience parameter is the amount of epochs to check for improvement
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=20)

model = build_model()
early_history = model.fit(X_train, y_train, 
                    epochs=EPOCHS, validation_split = 0.2, verbose=0, 
                    callbacks=[early_stop, tfdocs.modeling.EpochDots()])

#Predicting
dnn_preds = model.predict(X_test).ravel()

我看过一些类似的问题,答案是,X_train 必须是空的,但 X_train 是一个非常大的表。

错误代码:

ValueError                                Traceback (most recent call last)
<ipython-input-31-0fda30035cb2> in <module>()
      3                     epochs=1000,
      4                     verbose=0,
----> 5                     validation_split = 0.2)
      6 #                    callbacks=[early_stop, tfdocs.modeling.EpochDots()])
      7 

/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1193         logs = tf_utils.sync_to_numpy_or_python_type(logs)
   1194         if logs is None:
-> 1195           raise ValueError('Expect x to be a non-empty array or dataset.')
   1196         epoch_logs = copy.copy(logs)
   1197 

ValueError: Expect x to be a non-empty array or dataset.

感谢您的帮助!

【问题讨论】:

    标签: python tensorflow validation keras callback


    【解决方案1】:

    在我看来,您为 X_train 使用了错误的类型。我不认为 model.fit 可以接受你的桌子是什么(熊猫桌子?)。很可能您想向它传递一个 numpy 形状数组 [示例数(表中的行)、输入特征(表中的列)]。查看 model.fit 的文档。 https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 2020-11-23
      • 2021-12-19
      • 2020-11-02
      • 2018-11-01
      • 1970-01-01
      • 2022-12-26
      • 2022-12-02
      • 1970-01-01
      相关资源
      最近更新 更多