Python loss函数

code"

import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import math
import re
import pylab
from pylab import figure, show, legend
from mpl_toolkits.axes_grid1 import host_subplot
 
# read the log file
fp = open('log.txt', 'r')
train_iterations = []
total_loss = []
cout = 0
for ln in fp:
  cout+=1
  if 'INFO: Iter: ' in ln:
    arr = ln.find(r'INFO: Iter: ')
    iteraion = ln[arr+12:arr+12+6]
    #print(iteraion)
    train_iterations.append(int(iteraion))
  if 'total_loss: ' in ln:
    arr = ln.find(r'total_loss: ')
    loss = ln[arr+len(r'total_loss: '):arr+len(r'total_loss: ')+5]
    #print(loss)
    total_loss.append(float(loss))
fp.close()


host = host_subplot(111)
plt.subplots_adjust(right=0.8) # ajust the right boundary of the plot window

print(cout)
# set labels
host.set_xlabel("Iterations")
host.set_ylabel("total loss")

# plot curves
p1, = host.plot(train_iterations, total_loss, label="total_loss")
host.legend(loc=5)
 
# set label color
host.axis["left"].label.set_color(p1.get_color())

host.set_xlim([-200, 5100])

plt.draw()
plt.show()

猜你喜欢

转载自blog.csdn.net/nineship/article/details/102760428