将训练的结果转换成excel是送到画图的关键,但是在转的过程中出现了问题,发现是正则化公式的结果。
使用网站进行调试,最终可以转了。下面是调试的工具以及调试好的代码。
regex101: build, test, and debug regex
上面是正则化公式,下面是他的txt文件中的格式,要对应好才能转换。最后调好了,是没加空格的问题。
import re
import pandas as pd# 读取日志文件
log_file = r"origin\原始\20241102_191949.txt" # 替换为你的日志文件路径
with open(log_file, "r", encoding="utf-8") as f:lines = f.readlines()# 初始化存储数据的列表
data = []# 正则表达式提取数据
pattern = r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} - pyskl - INFO - Epoch \[(\d+)\]\[(\d+)\/(\d+)\]\s+lr: ([\d.e+-]+),\s+eta: ([\d\w\s:,]+),\s+time: ([\d.]+),\s+data_time: ([\d.]+),\s+memory: ([\d]+),\s+top1_acc: ([\d.]+),\s+top5_acc: ([\d.]+),\s+loss_cls: ([\d.]+),\s+loss: ([\d.]+),\s+grad_norm: ([\d.]+)"# 解析日志文件
for line in lines:print(f"Processing line: {line.strip()}") # 调试信息match = re.search(pattern, line)if match:print(f"Match found: {match.groups()}") # 调试信息# 提取匹配的数据epoch = int(match.group(1))current_step = int(match.group(2))total_steps = int(match.group(3))lr = float(match.group(4))eta = match.group(5)time = float(match.group(6))data_time = float(match.group(7))memory = int(match.group(8))top1_acc = float(match.group(9))top5_acc = float(match.group(10))loss_cls = float(match.group(11))loss = float(match.group(12))grad_norm = float(match.group(13))# 将数据添加到列表中data.append([epoch, current_step, total_steps, lr, eta, time, data_time, memory, top1_acc, top5_acc, loss_cls, loss, grad_norm])else:print("No match found") # 调试信息# 创建 DataFrame
columns = ["Epoch", "Current Step", "Total Steps", "Learning Rate", "ETA", "Time", "Data Time", "Memory", "Top1 Accuracy", "Top5 Accuracy", "Loss Cls", "Loss", "Grad Norm"]
df = pd.DataFrame(data, columns=columns)# 保存为 Excel 文件
output_file = r"rigin\原始\20241102_191949.xlsx" # 替换为你的输出文件路径
df.to_excel(output_file, index=False)print(f"数据已保存到 {output_file}")