Skip to content

Instantly share code, notes, and snippets.

@stewartm888
Last active September 23, 2019 19:54
Show Gist options
  • Save stewartm888/4859c14d90aef0dbc2fcbe5f59b9c39b to your computer and use it in GitHub Desktop.
Save stewartm888/4859c14d90aef0dbc2fcbe5f59b9c39b to your computer and use it in GitHub Desktop.
PLOTTING TENSORFLOW EPOCH LOSSES
########################################
### PLOTTING TENSORFLOW EPOCH LOSSES ###
########################################
"""
NOTE: this script assumes the epoch output of a tensorflow run has been saved in a .TXT file called 'val.txt' in the working directory.
The copied text should be formatted to look like the following...
---------------------------------------------------------------------------------------------------
200/200 [==============================] - 9s 46ms/step - loss: 0.3036 - val_loss: 0.2138
Epoch 2/20
200/200 [==============================] - 4s 22ms/step - loss: 0.2013 - val_loss: 0.2014
Epoch 3/20
---------------------------------------------------------------------------------------------------
In particular, this script assumes the losses are printed after the word 'loss' or 'val_loss' followed by a colon and space, and that the losses are printed to four decimal places.
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
row_num = 0
loss_line = []
val_loss_line = []
txt = open('val.txt','r')
for line in txt:
if line.find('loss')!=-1:
loss_pos = line.find('loss')
loss = float(line[loss_pos+6:loss_pos+12])
loss_line.append(loss)
val_loss_pos = line.find('val_loss')
val_loss = float(line[val_loss_pos+10:val_loss_pos+16])
val_loss_line.append(val_loss)
epoch = (list(range(1,len(loss_line)+1)))
dict = {'Epoch':epoch,'Loss':loss_line,'Val_Loss':val_loss_line}
df = pd.DataFrame(dict)
df = df.set_index('Epoch')
#print(df)
plt.plot(df)
plt.legend(['Loss','Val_loss'])
plt.xticks(np.arange(0, len(epoch)+1, 1))
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.title('Loss Over Epoch')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment