您好,登錄后才能下訂單哦!
在神經(jīng)網(wǎng)絡(luò)訓(xùn)練中,我們常常需要畫出loss function的變化圖,log日志里會顯示每一次迭代的loss function的值,于是我們先把log日志保存為log.txt文檔,再利用這個文檔來畫圖。
1,先來產(chǎn)生一個log日志。
import mxnet as mx import numpy as np import os import logging logging.getLogger().setLevel(logging.DEBUG) # Training data logging.basicConfig(filename = os.path.join(os.getcwd(), 'log.txt'), level = logging.DEBUG) # 把log日志保存為log.txt train_data = np.random.uniform(0, 1, [100, 2]) train_label = np.array([train_data[i][0] + 2 * train_data[i][1] for i in range(100)]) batch_size = 1 num_epoch=5 # Evaluation Data eval_data = np.array([[7,2],[6,10],[12,2]]) eval_label = np.array([11,26,16]) train_iter = mx.io.NDArrayIter(train_data,train_label, batch_size, shuffle=True,label_name='lin_reg_label') eval_iter = mx.io.NDArrayIter(eval_data, eval_label, batch_size, shuffle=False) X = mx.sym.Variable('data') Y = mx.sym.Variable('lin_reg_label') fully_connected_layer = mx.sym.FullyConnected(data=X, name='fc1', num_hidden = 1) lro = mx.sym.LinearRegressionOutput(data=fully_connected_layer, label=Y, name="lro") model = mx.mod.Module( symbol = lro , data_names=['data'], label_names = ['lin_reg_label'] # network structure ) model.fit(train_iter, eval_iter, optimizer_params={'learning_rate':0.005, 'momentum': 0.9}, num_epoch=20, eval_metric='mse',) model.predict(eval_iter).asnumpy() metric = mx.metric.MSE() model.score(eval_iter, metric)
上面的代碼中l(wèi)ogging.basicConfig(filename = os.path.join(os.getcwd(), 'log.txt'), level = logging.DEBUG) # 把log日志保存為log.txt 就是把log日志保存為log.txt文件。
2,log.txt文檔如下。
INFO:root:Epoch[0] Train-mse=0.470638 INFO:root:Epoch[0] Time cost=0.047 INFO:root:Epoch[0] Validation-mse=73.642301 INFO:root:Epoch[1] Train-mse=0.082987 INFO:root:Epoch[1] Time cost=0.047 INFO:root:Epoch[1] Validation-mse=41.625072 INFO:root:Epoch[2] Train-mse=0.044817 INFO:root:Epoch[2] Time cost=0.063 INFO:root:Epoch[2] Validation-mse=23.743375 INFO:root:Epoch[3] Train-mse=0.024459 INFO:root:Epoch[3] Time cost=0.063 INFO:root:Epoch[3] Validation-mse=13.511120 INFO:root:Epoch[4] Train-mse=0.013431 INFO:root:Epoch[4] Time cost=0.063 INFO:root:Epoch[4] Validation-mse=7.670062 INFO:root:Epoch[5] Train-mse=0.007408 INFO:root:Epoch[5] Time cost=0.063 INFO:root:Epoch[5] Validation-mse=4.344374 INFO:root:Epoch[6] Train-mse=0.004099 INFO:root:Epoch[6] Time cost=0.063 INFO:root:Epoch[6] Validation-mse=2.455608 INFO:root:Epoch[7] Train-mse=0.002274 INFO:root:Epoch[7] Time cost=0.062 INFO:root:Epoch[7] Validation-mse=1.385449 INFO:root:Epoch[8] Train-mse=0.001263 INFO:root:Epoch[8] Time cost=0.063 INFO:root:Epoch[8] Validation-mse=0.780387 INFO:root:Epoch[9] Train-mse=0.000703 INFO:root:Epoch[9] Time cost=0.063 INFO:root:Epoch[9] Validation-mse=0.438943 INFO:root:Epoch[10] Train-mse=0.000391 INFO:root:Epoch[10] Time cost=0.125 INFO:root:Epoch[10] Validation-mse=0.246581 INFO:root:Epoch[11] Train-mse=0.000218 INFO:root:Epoch[11] Time cost=0.047 INFO:root:Epoch[11] Validation-mse=0.138368 INFO:root:Epoch[12] Train-mse=0.000121 INFO:root:Epoch[12] Time cost=0.047 INFO:root:Epoch[12] Validation-mse=0.077573 INFO:root:Epoch[13] Train-mse=0.000068 INFO:root:Epoch[13] Time cost=0.063 INFO:root:Epoch[13] Validation-mse=0.043454 INFO:root:Epoch[14] Train-mse=0.000038 INFO:root:Epoch[14] Time cost=0.063 INFO:root:Epoch[14] Validation-mse=0.024325 INFO:root:Epoch[15] Train-mse=0.000021 INFO:root:Epoch[15] Time cost=0.063 INFO:root:Epoch[15] Validation-mse=0.013609 INFO:root:Epoch[16] Train-mse=0.000012 INFO:root:Epoch[16] Time cost=0.063 INFO:root:Epoch[16] Validation-mse=0.007610 INFO:root:Epoch[17] Train-mse=0.000007 INFO:root:Epoch[17] Time cost=0.063 INFO:root:Epoch[17] Validation-mse=0.004253 INFO:root:Epoch[18] Train-mse=0.000004 INFO:root:Epoch[18] Time cost=0.063 INFO:root:Epoch[18] Validation-mse=0.002376 INFO:root:Epoch[19] Train-mse=0.000002 INFO:root:Epoch[19] Time cost=0.063 INFO:root:Epoch[19] Validation-mse=0.001327
3,利用log.txt文件來畫圖。
import re import matplotlib.pyplot as plt import numpy as np def main(): file = open('log.txt','r') list = [] # search the line including accuracy for line in file: m=re.search('Train-mse', line) if m: n=re.search('[0]\.[0-9]+', line) # 正則表達(dá)式 if n is not None: list.append(n.group()) # 提取精度數(shù)字 file.close() plt.plot(list, 'go') plt.plot(list, 'r') plt.xlabel('count') plt.ylabel('accuracy') plt.title('Accuracy') plt.show() if __name__ == '__main__': main()
以上這篇python保存log日志,實現(xiàn)用log日志來畫圖就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。