前言:
在深度学习训练的过程中经常需要看loss的曲线,如果loss被存成npy文件了,如何去查看,查看之后如何利用代码实现在TensorBoard里面的将曲线变平缓的功能。
用下面的一行代码读取loss.npy文件
y = np.load(r"G:\Competition\AI challenge\Vnet\loss (1).npy")
然后计算y的移动平均,里面有用到np.convolve函数,这个是卷积的意思,一维卷积,接下来看看怎么实现:
def movingaverage(interval, window_size):
window= numpy.ones(int(window_size))/float(window_size)
return numpy.convolve(interval, window, 'same')
如果你了解一维信号的卷积你就可以知道这段代码实现的意思
假设interval=[a,b,c],window_size=3,那么函数里面的window=[1/3,1/3,1/3]
那么这段代码返回的结果就是:
[a/3 b/3 c/3]
+ +
a/3 b/3 c/3
+ +
a/3 b/3 c/3]
a/3 (a+b)/3 (a+b+c)/3 (c+b)/3 c/3
整段代码如下:
from pylab import plot, ylim, xlim, show, xlabel, ylabel, grid
from numpy import linspace, loadtxt, ones, convolve
import numpy as numpy
import matplotlib.pyplot as pt
def movingaverage(interval, window_size):
window= numpy.ones(int(window_size))/float(window_size)
return numpy.convolve(interval, window, 'same')
y = np.load(r"G:\Competition\AI challenge\Vnet\loss (1).npy")
x = np.array(range(len(y)))
plot(x,y,"k.")
y_av = movingaverage(y, 500)#window_size可调
#pt.figure(figsize=(10,5))
plot(x, y_av,"r")
xlim(0,len(y))
xlabel("iteration.")
ylabel("Loss.")
grid(True)
pt.savefig(r'G:\Competition\AI challenge\Vnet\loss2.png',dip=500)
show()
结果:
滑动之前:
滑动之后:
或者这样(推荐):
import numpy as np
import matplotlib.pyplot as plt
def smooth_curve(points, factor=0.9):
smoothed_points = []
for point in points:
if smoothed_points:
previous = smoothed_points[-1]
smoothed_points.append(previous * factor + point * (1 - factor))
else:
smoothed_points.append(point)
return smoothed_points
#acc=np.random.randn(100)
acc=[23,24,25,1,27,26,26,25,23,24,25,26,27,27,28,23,24,25,26]
plt.figure(figsize=(10,1))
plt.grid(True)
plt.plot(range(len(acc)),acc, 'b', label='Smoothed training acc',color='r')
plt.plot(range(len(acc)),smooth_curve(acc), 'b', label='Smoothed training acc')