How to log a plot in azureml?

Question:

I want to log a created plot to my workspace like this:

from azureml.core import Run
from matplotlib import pyplot as plt

run = Run.get_context()
Foo = [1,2,3,4]
Bar = [4,3,2,1]

plt.title('Foo vs Bar')
plt.plot(Foo, label='Foo')
plt.plot(Bar, '-r', label='Bar')

run.log_image('Plot', plt)

But I’m getting the following error:

"type": "AttributeError",
"message": "module 'matplotlib.pyplot' has no attribute 'tell'",

This happens when it tries to compute this:

File "/usr/lib/python3.6/imghdr.py", line 19, in what
location = file.tell()

I can log variables to azureml.
If I run my script locally without azureml I can see the plots correctly.
How can I log my plot to my azure experiment?

Asked By: Gonzalo Garcia

||

Answers:

I found the answer. In order to save the plt into azureml you have to specify which is the plot and not just send it as a second parameter.

...
run.log_image('Plot', plot=plt)
Answered By: Gonzalo Garcia

Ok here my solution to print two values in one graph, it prints "val_loss" together with "train_loss" in one graph with one value per epoch.
You have to log both losses per epoch via run.parent.log(name, value). Than you can use:

# overfitting monitor on epochs
metrics = run.get_metrics()
plt.plot(list(range(len(metrics.get('val_loss')))), metrics.get('val_loss'), label='val_loss')
plt.plot(list(range(len( metrics.get('train_loss')))),  metrics.get('train_loss'), label='train_loss')
plt.legend()
plt.xlabel("Epoch")
plt.ylabel("Loss")
self.run.log_image('overfitting_monitor_img', plot=plt)
Answered By: Manute