python - 名称错误 : name 'history' is not defined

标签 python tensorflow keras

设置模型,为实验设置训练和验证数据,模型训练已经执行。但是在训练神经网络之后,在可视化训练过程中,我收到了一个错误。

import matplotlib.pyplot as plt
%matplotlib inline

acc=history.history['categorical_accuracy']
val_acc=history.history['val_categorical_accuracy']
loss=history.history['loss']
val_loss=history.history['val_loss']

epochs=range(1,len(acc)+1)
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()

plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
NameError                                 Traceback (most recent call last)
<ipython-input-7-e1591c31f061> in <module>()
      2 get_ipython().magic('matplotlib inline')
      3 
----> 4 acc=history.history['categorical_accuracy']
      5 val_acc=history.history['val_categorical_accuracy']
      6 loss=history.history['loss']

NameError: name 'history' is not defined

最佳答案

当你调用 model.fit 时,它应该是

history = model.fit()

关于python - 名称错误 : name 'history' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66576876/

相关文章:

python - 如何使用带有 Python 的 Selenium WebDriver 获取选定的选项?

python - tensorflow Python 3.7

merge - 如何在 Keras 中组合 2 个经过训练的模型

python - 如何在缩进中使用制表符和空格不一致? | python

python - 如何比较两个列表并找出 `added` `deleted` `unchanged ` 部分

python - 构建多个输出的神经网络

python - 如何从具有向量列的 DataFrame 创建 tensorflow 数据集?

python - Keras:加权二进制交叉熵实现

python - Keras 单热编码器

python - 类型错误 : bad operand type for unary ~: float