保存结果到文件中(Lua/Python)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_27990891/article/details/82596916

在训练网络中经常需要保存一些参数或者结果,通常保存为.log日志文件或.txt文件。
保存到文件中,一般为三个步骤:
1. 创建可写文件,并打开
2. 序列化要保存的数据
3. 关闭文件

因为会训练多次,如果这些结果都想保存下来,那么通常会获取系统时间作为文件名。
Note:因为涉及到Torch(Lua)实现转TensorFlow(Python)实现,这里给出两种语言的实现。

Lua:

require 'os'
localDate = os.date("%Y%m%d-%H%M%S")  --获取系统时间格式为:20180910-213256,即2018年09月10213256秒。
resultFd = io.open("./result/result_" .. localDate .. ".txt", "w")
for i = 1, Num do
    resultFd:write(tostring(output[i][1]) .. "\n")
end
resultFd:close()

Python2:

import time
localTime = time.strfTime('%Y%m%d-%H%M%S',time.localtime(time.time()))
resultFd = open('./result/result_' + localDate + '.txt', 'w')
for i in xrange(Num):
    resultFd.write(str(output[i,1]) + '\n')
resultFd.close()

猜你喜欢

转载自blog.csdn.net/sinat_27990891/article/details/82596916