TensorFlow在Colab上的DNN (ValueError:期望x是非空数组或数据集) &g



我正在努力跟随这篇博文。我想用深度神经网络预测客户终身价值。当我运行用于拟合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.

谢谢你的帮助!

在我看来,你正在使用错误的类型X_train。我不认为模型。Fit可以接受您的任何表(熊猫表?)最有可能的是,你想传递一个numpy数组的形状[示例数(表中的行),输入特征(表中的列)]。请查看model.fit的文档。https://www.tensorflow.org/api_docs/python/tf/keras/Model符合

相关内容

  • 没有找到相关文章

最新更新