Search code examples
pythontensorflowmatplotlibkerasattributeerror

AttributeError: 'Model' object has no attribute 'epoch' - Keras


I huild an auto encoder in keras following steps given in a simple autoencoder based on a fully-connected layer (https://blog.keras.io/building-autoencoders-in-keras.html)

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

After training and cross vaidation. plotting the results gives me error given below:

    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

The second thing i tried gives be result : No handles with labels found to put in legend. however plot is generated. how do i solve this problem

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()

Solution

  • You need to define range properly as epochs is not defined which you used inside range(). You can use any other number apart from 100 depending on your requirement.

    epochs = range(100)