Tensorboards add_scalar only logs last value assigned in a for loop

108 Views Asked by At

I am trying to log the threshold dependent accuracy values for a ML model stored in a dict object to Tensorboard with the add_scalar method. However, it results in only adding the very last data point to the graph.

The dict basically looks as such:

eval_metrics['accuracy'] = {0.2: 0.375,
 0.4: 0.8,
 0.6: 0.75,
 0.8: 0.35}

My for-loop looks like this:

for key in eval_metrics['accuracy']: 
    writer.add_scalar('acc', eval_metrics['accuracy'][key], key)

However, only the last of the four key-value pairs gets registered and the resulting scalar thus only contains a single data point. Does anyone know why that is or if there is a way around?

I already tried with different data types for the global_step value or introducing a new variable replacing the key variable.

2

There are 2 best solutions below

0
On

add_scalars instead of add_scalar is a more suitable function for your need.

0
On

Using add_scalars also brought its problems. It requires strings for the keys of the dict which does not suit the use case well. Since scalars are generally not designed for visualizing a 'static' dict object but rather for iteratively constructing charts, I used a different way to achieve my goal. Using matplotlib I constructed a histogram and added it as a figure to tensorboard:

fig = plt.figure(figsize=(10,6))
plt.bar(range(len(list(eval_metrics_test['accuracy'].keys()))), list(eval_metrics_test['accuracy'].values()))
plt.title('Accuracy on thresholds')
plt.xlabel('Thresholds')
plt.ylabel('Accuracy')
plt.xticks(range(len(list(eval_metrics_test['accuracy'].keys()))), list(eval_metrics_test['accuracy'].keys()))
writer.add_figure('Test Accuracy Thresholds', fig)

Thank you for your help though!