图示
代码
import matplotlib.pyplot as plt
import numpy as np
def plot_epoch_for_performance_and_loss(model_name, res_dict):
"""Function: 评价指标以及训练集loss和epoch的关系曲线
- param:
model_name: (str) 模型的名称
res_dict: (dict) 包含loss, 表现随epoch变化的list
"""
color = ['r', 'g', 'b', 'y']
shape = ['o', 'v', '^']
loss = res_dict['epoch_loss']
fig = plt.figure(figsize=(15,6)) # figsize指定给个图大小(两个数字分别表示横轴纵轴)
ax1 = fig.add_subplot(1, 2, 1) # 1行2列的图,相当于四个图,1是第一个
ax2 = fig.add_subplot(1, 2, 2) # 1行2列的图,相当于四个图,3是第三个
ax1.plot(np.arange(len(loss)), np.array(loss))
ax1.set_xlabel("Epoch")
ax1.set_ylabel("Loss")
legend = []
for idx, key in enumerate(list(res_dict.keys())):
if 'loss' in key:
continue
c = color[idx%len(color)]
s = shape[idx%len(shape)]
ax2.plot(np.arange(len(res_dict[key])), np.array(res_dict[key]), color=c, marker=s)
legend.append(key)
ax2.legend(legend)
ax2.set_xlabel("Epoch")
ax2.set_ylabel("Perfermance")
plt.show()
plt.savefig(model_name + '.png')
model_name = 'BertCrf'
res_dict = {
'epoch_loss':[10,6,5,5,3,2,1],
'train_f1':[1,2,3,4,5,6,7],
'dev_f1':[1,3,5,4,5,1,2,3]
}
plot_epoch_for_performance_and_loss(model_name, res_dict)