这是看过莫凡python的学习笔记。

搭建网络,两种方式

(1)建立Sequential对象

import torch
net = torch.nn.Sequential(
torch.nn.Linear(2,10),
torch.nn.ReLU(),
torch.nn.Linear(10,2))

输出网络结构

Sequential(
(0): Linear(in_features=2, out_features=10, bias=True)
(1): ReLU()
(2): Linear(in_features=10, out_features=2, bias=True)
)

(2)建立网络类,继承torch.nn.module

class Net(torch.nn.Module):
def __init__(self):
super(Net,self).__init__()
self.hidden = torch.nn.Linear(2,10)
self.predict = torch.nn.Linear(10,2)
def forward(self,x):
x = F.relu(self.hidden(x))
x = self.predict(x)
return x

输出和上面基本一样,略微不同

Net(
(hidden): Linear(in_features=2, out_features=10, bias=True)
(predict): Linear(in_features=10, out_features=2, bias=True)
)

保存模型,两种方式

(1)保存整个网络,及网络参数

torch.save(net,'net.pkl')

(2)只保存网络参数

torch.save(net.state_dict(),'net_params.pkl')

恢复模型,两种方式

(1)加载整个网络,及参数

net2 = torch.load('net.pkl')

(2)加载参数,但需实现网络

net3 = torch.nn.Sequential(
torch.nn.Linear(2,10),
torch.nn.ReLU(),
torch.nn.Linear(10,2))
net3.load_state_dict(torch.load('net_params.pkl'))

pytorch搭建网络,保存参数,恢复参数的更多相关文章

  1. 一文弄懂pytorch搭建网络流程+多分类评价指标

    讲在前面,本来想通过一个简单的多层感知机实验一下不同的优化方法的,结果写着写着就先研究起评价指标来了,之前也写过一篇:https://www.cnblogs.com/xiximayou/p/13700 ...

  2. TensorFlow进阶(六)---模型保存与恢复、自定义命令行参数

    模型保存与恢复.自定义命令行参数. 在我们训练或者测试过程中,总会遇到需要保存训练完成的模型,然后从中恢复继续我们的测试或者其它使用.模型的保存和恢复也是通过tf.train.Saver类去实现,它主 ...

  3. TensorFlow 训练好模型参数的保存和恢复代码

    TensorFlow 训练好模型参数的保存和恢复代码,之前就在想模型不应该每次要个结果都要重新训练一遍吧,应该训练一次就可以一直使用吧. TensorFlow 提供了 Saver 类,可以进行保存和恢 ...

  4. Pytorch从0开始实现YOLO V3指南 part2——搭建网络结构层

    本节翻译自:https://blog.paperspace.com/how-to-implement-a-yolo-v3-object-detector-from-scratch-in-pytorch ...

  5. pytorch基础-搭建网络

    搭建网络的步骤大致为以下: 1.准备数据 2. 定义网络结构model 3. 定义损失函数4. 定义优化算法 optimizer5. 训练 5.1 准备好tensor形式的输入数据和标签(可选) 5. ...

  6. pytorch autograd backward函数中 retain_graph参数的作用,简单例子分析,以及create_graph参数的作用

    retain_graph参数的作用 官方定义: retain_graph (bool, optional) – If False, the graph used to compute the grad ...

  7. caffe-windows之网络描述文件和参数配置文件注释(mnist例程)

    caffe-windows之网络描述文件和参数配置文件注释(mnist例程) lenet_solver.prototxt:在训练和测试时涉及到一些参数配置,训练超参数文件 <-----lenet ...

  8. react native 网络get请求方式参数不可为undefined或null

    react native 网络get请求方式参数不可为undefined(为空的话默认变为)或null 错误写法: export function addToCartAction(isRefreshi ...

  9. loadrunner 脚本开发-参数化之将内容保存为参数、参数数组及参数值获取Part 2

    脚本开发-参数化之将内容保存为参数.参数数组及参数值获取 by:授客 QQ:1033553122 ----------------接 Part 1--------------- 把内容保存到参数数组 ...

随机推荐

  1. Eclipse中Next Difference的快捷键

    原文:http://stackoverflow.com/questions/10177460/is-there-a-key-binding-for-next-difference-and-previo ...

  2. xcode编写c/c++静态库使用系统头文件问题

    c/c++编写的静态库中有引用ios系统头文件比如: #include <EGL/egl.h> 在xcode编译的时候需要设置静态库程序: Build Settings-Header Se ...

  3. Shell杀tomcat进程

    一.killandclean.sh #!/bin/bash pid=($(ps -ef | grep tomcat | egrep -v grep | awk '{print $2}')) lengt ...

  4. java获取Linux持续运行时间及友好显示

    一.uptime命令 uptime命令可以查看系统的运行时间和负载 终端输入uptime 04:03:58 up 10 days, 13:19, 1 user, load average: 0.54, ...

  5. C++知识点总结(四)——面向对象的编程细节总结

    1.空类的默认函数 一般情况下,对于任意一个类A,如果程序员不显示的声明和定义上述函数,C++编译器将会自动的为A产生4个public inline(公有.内联)的默认函数,这4个函数最常见的形式为: ...

  6. MSSQL 数据库日志爆涨

    解决方法有两种,现只用最简单的方法: 1.数据库属性----选项----恢复模式由完整改为简单--确定 2.右击数据库---任务---收缩 3.数据库属性----选项----恢复模式由简单改为完整-- ...

  7. Solr之缓存篇

    原文出自:http://my.oschina.net/u/1026644/blog/123957 Solr在Lucene之上开发了很多Cache功能,从目前提供的Cache类型有: (1)filter ...

  8. oracle行转列练习

    ----------------------第一题--------------------------- create table STUDENT_SCORE ( name ), subject ), ...

  9. 容器控件JPanel的使用

    -----------------siwuxie095 工程名:TestUI 包名:com.siwuxie095.ui 类名:TestPanel.java 工程结构目录如下: 在默认窗体 JFrame ...

  10. iOS 隐藏App图标

    1.在进入Info.plist文件 2.在Info.plist文件中新添加一项,把Key值设置为SBAppTags,在Type选项中选取Array 3.在Array中新添加一项Item0,Type类型 ...