AttributeError:'Model'对象没有属性'epoch'-Keras

时间:2018-12-21 05:20:11

标签: python tensorflow matplotlib keras attributeerror

按照基于全连接层(https://blog.keras.io/building-autoencoders-in-keras.html)的简单自动编码器中给出的步骤,我会在keras中创建一个自动编码器

wiki_autoencoder.fit(wiki_train, wiki_train,
                epochs=100,
                batch_size=256,
                shuffle=True,
                validation_data=(wiki_test, wiki_test))  

经过训练和交叉验证。绘制结果会给我以下错误:

    loss = wiki_autoencoder.history.history['loss']
    val_loss = wiki_autoencoder.history.history['val_loss']
    epochs = range(epochs)
    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.legend()
    plt.show()


NameError                                 Traceback (most recent call last)
<ipython-input-32-6acdd795daf3> in <module>()
      1 loss = wiki_autoencoder.history.history['loss']
      2 val_loss = wiki_autoencoder.history.history['val_loss']
----> 3 epochs = range(epochs)
      4 plt.figure()
      5 plt.plot(epochs, loss, 'bo', label='Training loss')

NameError: name 'epochs' is not defined

我尝试给出的第二件事是结果:没有找到带有图例的带有标签的句柄。但是会生成情节。我该如何解决这个问题

enter image description here

plt.plot(wiki_autoencoder.history.history['val_loss'], 'r', wiki_autoencoder.history.history['loss'], 'bo')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()

2 个答案:

答案 0 :(得分:1)

您需要正确定义范围,因为未定义在range()内部使用的时期。您可以根据需要使用100以外的任何其他数字。

epochs = range(100)

答案 1 :(得分:1)

您实际上可以从历史对象中找到纪元。

header{background-color:#221816; grid-column: 1/13;}

这将为您提供训练模型的时代。