ValueError:x应为非空数组或数据集。(Colab上的张量流DNN模型)

2024-04-27 03:27:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用DNN来预测客户的终身价值。我跟踪了this blog post。当我运行将DNN模型拟合到数据集的代码时,我遇到了一个错误。我对机器学习非常陌生,我已经尝试过尽职调查来理解代码在做什么

#DNN
def build_model():
    model = tf.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 = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=20)

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

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

错误发生在callbacks参数上。我理解回调是为了停止模型的训练,因为被监控的损失指标已经不能最小化

我已经访问了这么多的网站,以确定为什么会出现错误标志,但我一直没有找到答案。如能提供任何帮助,我们将不胜感激

错误代码如下所示

ValueError                                Traceback (most recent call last)
<ipython-input-23-317a4345770f> in <module>()
     22 early_history = dnn_model.fit(X_train, y_train,
     23                     epochs=1500, validation_split = 0.2, verbose=0,
---> 24                     callbacks=[early_stop, tfdocs.modeling.EpochDots()])
     25 
     26 #Predicting

/usr/local/lib/python3.7/dist-packages/tensorflow/python/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)
   1108 
   1109         if logs is None:
-> 1110           raise ValueError('Expect x to be a non-empty array or dataset.')
   1111         epoch_logs = copy.copy(logs)
   1112 

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

Tags: tomodellayerstf错误trainkerasdense