【发布时间】: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