user17273739
user17273739

Reputation:

How to plot loss and accuracy graphs using TensorFlow Lite

I would like to plot the accuracy and loss graphs of a model trained using TensorFlow Lite. Unlike a Keras model there is no model.fit() method used but instead image_classifier.create() was used to train the model. Hence, I am unsure of how to plot the graphs. If I use loss_train = model.history['train_loss'], I get the error TypeError: 'History' object is not subscriptable. I have exactly followed this documentation to write my code and would like to know how I can now plot the graphs. Thank you!

Upvotes: 1

Views: 1508

Answers (1)

Kunni
Kunni

Reputation: 11

It is quite an old question but the answer may be helpful for someone else. Please run the following first:

model.train(
        train_data,
        validation_data=validation_data,
        hparams=None,
        steps_per_epoch=None)

then

acc = model.history.history['accuracy']
val_acc = model.history.history['val_accuracy']
loss = model.history.history['loss']
val_loss = model.history.history['val_loss']
epochs_range = range(no_epochs)

then use same code as for tenstoflow examples of plotting metrics vs. epochs plot:

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

Hope that helps.

Upvotes: 1

Related Questions