The use of the log file log when training in python
A record of the training process
train_metrics = {
"train_loss": 5, "train_acc": 6}
val_metrics = {
"val_loss": 7, "val_acc": 8}
epochs = 10
metriclog = open('metric_2' + '.log', 'w') # 创建日志文件
metriclog.write("train_loss" + " " + "train_score" + " " + "epoch" + "\n")
for epoch in range(1, epochs + 1):
train_metrics["epoch"] = epoch
metriclog.write(str(train_metrics["train_loss"])+" "+str(train_metrics["train_acc"])+" "+str(train_metrics["epoch"])+'\n')
metriclog.flush()
metriclog.close()
The result is
train_metrics = {
"train_loss": 5, "train_acc": 6}
val_metrics = {
"val_loss": 7, "val_acc": 8}
epochs = 10
metriclog = open('metric_2' + '.log', 'w') # 创建日志文件
metriclog.write("train_loss" + " " + "train_score" + " " + "val_loss" + " " + "val_score" + " " + "epoch" + "\n")
for epoch in range(1, epochs + 1):
val_metrics["epoch"] = epoch
metriclog.write(str(train_metrics["train_loss"]) + " " + str(train_metrics["train_acc"]) + ' ')
metriclog.write(str(val_metrics["val_loss"]) + " " + str(val_metrics["val_acc"]) + " " + str(val_metrics["epoch"]) + '\n')
metriclog.flush()
metriclog.close()
The advantage of using the log file to record the training process is that opening and viewing the log file during training will not affect the training process and will not interrupt the program.