1、list存储为txt文件:
acc = [ ]
for epoch in range ( 300 ) :
for i, ( tr_data, tr_labels) in enumerate ( train_loader) :
acc. append( acc1. item( ) )
acc_vurve = open ( 'file/acc_curve.txt' , 'a+' )
acc_vurve. write( 'name1' + '\n' )
acc_vurve. write( str ( acc) + '\n' )
acc_vurve. close( )
print ( 'Finished Training' )
2、读取按行存储的list文件,并绘制acc/loss训练过程图并保存,此处以记录了2个训练过程acc的文件为例:
import os
import matplotlib. pyplot as plt
import numpy as np
epoch = 300
path = os. getcwd( )
print ( path)
acc1 = [ ]
acc2 = [ ]
path = os. path. join( path, 'file/acc_curve.txt' )
f = open ( path, "r" )
lines = f. readlines( )
label1 = lines[ 0 ]
acc1 = list ( map ( float , lines[ 1 ] [ 1 : - 2 ] . split( ', ' ) ) )
label2 = lines[ 2 ]
acc2 = list ( map ( float , lines[ 3 ] [ 1 : - 2 ] . split( ', ' ) ) )
plt. title( 'Result Analysis' )
plt. figure( num= 3 , figsize= ( 8 , 8 ) )
plt. xlabel( "epoch" )
plt. ylabel( "acc" )
my_y_ticks = np. arange( 0 , 1 , 0.05 )
plt. yticks( my_y_ticks)
iteration = list ( range ( 1 , epoch + 1 ) )
plt. plot( iteration, acc1, color= 'green' , label= label1)
plt. plot( iteration, acc2, color= 'red' , label= label2)
plt. legend( )
plt. savefig( "accuracy_differ_conv.jpg" )
f. close( )