Tf.keras "history not defined" ...但在我看来是定义的



我正在TensorFlow中为我正在学习的入门课程构建一个简单的模型。我找不到错误的原因;未定义历史"我将非常感谢任何帮助,以确定这个错误的来源并修复它

这是代码:

def train_model(model, scaled_train_images, train_labels):
history = model.fit(scaled_train_images, train_labels, epochs=5, batch_size=256)
# Run function to train the model
train_model(model, scaled_train_images, train_labels)

输出:

Train on 60000 samples
Epoch 1/5
60000/60000 [===] - 56s 936us/sample - loss: 0.0623 - accuracy: 0.9809
Epoch 2/5
60000/60000 [===] - 56s 932us/sample - loss: 0.0538 - accuracy: 0.9838
Epoch 3/5
60000/60000 [===] - 56s 925us/sample - loss: 0.0475 - accuracy: 0.9858
Epoch 4/5
60000/60000 [===] - 55s 923us/sample - loss: 0.0422 - accuracy: 0.9869- los
Epoch 5/5
60000/60000 [===] - 55s 917us/sample - loss: 0.0380 - accuracy: 0.9883
#Load the model history into a pandas DataFrame
frame = pd.DataFrame(history.history)
---------------------------------------------------------------------------
NameError
Traceback (most recent call last)
<ipython-input-22-895ca3f31ddf> in <module>
1 # Run this cell to load the model history into a pandas DataFrame
2 
----> 3 frame = pd.DataFrame(history.history)
NameError: name 'history' is not defined

历史变量仅在train_model函数内部定义,因此无法在外部访问。

修复此返回:

def train_model(model, scaled_train_images, train_labels):
return model.fit(scaled_train_images, train_labels, epochs=5, batch_size=256)
history = train_model(model, scaled_train_images, train_labels)
frame = pd.DataFrame(history.history)

相关内容

最新更新