基础绘图,由实验结果画acc和loss图

画一条曲线:给x,y(一定要同维),label

plt.plot(epoches, train_loss, label = 'Train Loss')
plt.plot(epoches, val_loss, label = 'Validation Loss')

x and y must have same first dimension, but have shapes (41,) and (0,)
制作x常用手法:

epochs = range(1, len(train_acc) + 1)

re模块提供了丰富的正则表达式功能,可用于处理文本数据。你可以根据需要使用不同的正则表达式操作来解决文本处理和分析问题

处理文本数据放到列表里
train_loss = []
val_loss = []
model = 'irnn'
path = f'./saved_res/{model}_result.txt'
with open(path, 'r') as f:
    for line in f:
        trainloss = re.search(r'Train loss : ([\d.]+)', line)
        valloss = re.search(r'Validation loss : ([\d.]+)', line)
        if(trainloss):
            train_loss.append(float(trainloss.group(1)))
        if(valloss): 
            val_loss.append(float(valloss.group(1)))

正则表达式,要注意空格!

AttributeError: module ‘matplotlib’ has no attribute ‘plot’
解决方法:
import matplotlib as plt 改为
import matplotlib.pyplot as plt

你可能感兴趣的:(python)