码迷,mamicode.com
首页 > 其他好文 > 详细

Pytorch model saving and loading 模型保存和读取

时间:2018-02-21 10:52:06      阅读:902      评论:0      收藏:0      [点我收藏+]

标签:eth   learn   stat   loading   enc   pytorch   tutorial   .com   params   

It is really useful to save and reload the model and its parameters during or after training in deep learning.

Pytorch provides two methods to do so.

1. Only restore the parameters (recommended)

torch.save(the_model.state_dict(), PATH)    # save parameters to PATH

the_model = TheModelClass(*args, **kwargs)    # declare the_model as a object of TheModelClass
the_model.load_state_dict(torch.load(PATH))    # load parameters from PATH

 

2. Save all structure and parameters

torch.save(the_model, PATH)

the_model = torch.load(PATH)

 

3. Get parameters of certain layer

params=model.state_dict() 
for k,v in params.items():
    print(k)    # print the variable names in networks
print(params[‘conv1.weight‘])   #print conv1‘s weight
print(params[‘conv1.bias‘])   #print conv1‘s bias  

  

 

reference:http://www.pytorchtutorial.com/pytorch-note5-save-and-restore-models/

  

Pytorch model saving and loading 模型保存和读取

标签:eth   learn   stat   loading   enc   pytorch   tutorial   .com   params   

原文地址:https://www.cnblogs.com/beatets/p/8456252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!