pytorch 建立模型的几种方法
利用pytorch来构建网络模型,常用的有如下三种方式
前向传播网络具有如下结构:
卷积层--》Relu层--》池化层--》全连接层--》Relu层
对各Conv2d和Linear的解释如下
Conv2d的解释如下
"""
Conv2d(in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True) in_channels(int) – 输入信号的通道数
out_channels(int) – 卷积产生的通道数
kerner_size(int or tuple) - 卷积核的大小
stride(int or tuple,optional) - 卷积步长,即要将输入扩大的倍数。
padding(int or tuple, optional) - 输入的每一条边补充0的层数,高宽都增加2*padding
"""
Linear函数的解释如下
"""
Linear(in_features, out_features, bias=True)
in_features: size of each input sample,一般输入是[B,*,in_features]
out_features: size of each output sample,经过Linear输出的tensor是[B,*,out_features]
"""
1.建立模型方法
from torch.nn import *
class Network(Module):
def __init__(self):
super(Network, self).__init__()
self.conv = Conv2d(3, 16, 3, 1, 1)
self.dense =Linear(16 * 3, 2)
self.pool=MaxPool2d(kernel_size=2)
self.relu=ReLU(inplace=True)#inplace为True,将会改变输入的数据 ,否则不会改变原输入,只会产生新的输出 #前向传播方法
def forward(self, x):
x=self.conv(x)
x= self.relu(x)
x = MaxPool2d(x, 2)
x = x.view(x.size(0), -1)#设置成为[B,-1]格式
x= self.dense(x)
x = self.relu(x)
return x
model = Network()
print(model)
模型各参数如下
Network(
(conv): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(dense): Linear(in_features=48, out_features=2, bias=True)
(pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(relu): ReLU(inplace)
)
2.建立模型方法,通过torch.nn.Sequential建立模型
import torch
class Network(torch.nn.Module):
def __init__(self):
super(Network, self).__init__()
self.conv = torch.nn.Sequential(
torch.nn.Conv2d(3, 16, 3, 1, 1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(2)
)
self.dense = torch.nn.Sequential(
torch.nn.Linear(16 * 3, 2),
torch.nn.ReLU()
) def forward(self, x):
x = self.conv(x)
x = x.view(x.size(0), -1)
x = self.dense(x)
return x
model = Network()
print(model)
模型各参数如下
Network(
(conv): Sequential(
(0): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU()
(2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(dense): Sequential(
(0): Linear(in_features=48, out_features=2, bias=True)
(1): ReLU()
)
)
3.建立模型方法,通过torch.nn.Sequential的方法add_module添加操作
import torch
class Network(torch.nn.Module):
def __init__(self):
super(Network, self).__init__()
self.network=torch.nn.Sequential()
self.network.add_module("conv_{}".format(1),torch.nn.Conv2d(3, 16, 3, 1, 1))
self.network.add_module("relu_{}".format(1),torch.nn.ReLU())
self.network.add_module("pool_{}".format(1),torch.nn.MaxPool2d(2))
self.network.add_module("dense_{}".format(1),torch.nn.Linear(16 * 3, 2))
self.network.add_module("relu_{}".format(2),torch.nn.ReLU()) def forward(self, x):
x = self.network(x)
return x
model = Network()
print(model)
模型各参数如下
Network(
(network): Sequential(
(conv_1): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu_1): ReLU()
(pool_1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(dense_1): Linear(in_features=48, out_features=2, bias=True)
(relu_2): ReLU()
)
)
pytorch 建立模型的几种方法的更多相关文章
- ThinkPHP学习笔记 实例化模型的四种方法
创建Action类 [php] <?php class NewObjectAction extends Action{ public function index(){ ...
- pytorch 创建tensor的几种方法
tensor默认是不求梯度的,对应的requires_grad是False. 1.指定数值初始化 import torch #创建一个tensor,其中shape为[2] tensor=torch.T ...
- 莫烦python教程学习笔记——保存模型、加载模型的两种方法
# View more python tutorials on my Youtube and Youku channel!!! # Youtube video tutorial: https://ww ...
- pytorch学习: 构建网络模型的几种方法
利用pytorch来构建网络模型有很多种方法,以下简单列出其中的四种. 假设构建一个网络模型如下: 卷积层-->Relu层-->池化层-->全连接层-->Relu层--> ...
- EF Core中避免贫血模型的三种行之有效的方法(翻译)
Paul Hiles: 3 ways to avoid an anemic domain model in EF Core 1.引言 在使用ORM中(比如Entity Framework)贫血领域模型 ...
- 自然语言处理的CNN模型中几种常见的池化方法
自然语言处理的CNN模型中几种常见的池化方法 本文是在[1]的基础上进行的二次归纳. 0x00 池化(pooling)的作用 首先,回顾一下NLP中基本的CNN模型的卷积和池化的大致原理[2].f ...
- java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区
java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测).链地址法.再哈希.建立公共溢出区 标签: hashmaphashmap冲突解决冲突的方法冲突 2016-0 ...
- PyQt(Python+Qt)学习随笔:QStandardItemModel指定行和列创建模型后的数据项初始化的两种方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QStandardItemModel通过构造方法 QStandardItemModel(int ro ...
- Pytorch的模型加速方法:Dataparallel (DP) 和 DataparallelDistributedparallel (DDP)
Dataparallel 和 DataparallelDistributed 的区别 一.Dataparallel(DP) 1.1 Dartaparallel 的使用方式 Dataparallel 的 ...
随机推荐
- Ligg.EasyWinApp-10300-Ligg.EasyWinForm:View的配置
View的配置文件, 路径如下: .\Applications\xxxx(Apllication)\Clients\Form\Functions\yyyy(Function)\ Views\zzzz ...
- angular cli + primeNG
目录: 1.安装 angular cli 2.创建项目 3.构建路由 4.新建组件 5.组件之间的通信 6.引入primeNG 7.修改primeNG组件样式 8.问题 -------------- ...
- Redis未授权访问利用
转载:https://www.cnblogs.com/-qing-/p/10978912.html 0x01 kali安装redis 下载 wget http://download.redis.io/ ...
- IDEA新建Spring配置文件的方法
IDEA创建Spring Config 选择项目文件右键 输入文件名称即可 applicationContext.xml
- 3、netty第二个例子,使用netty建立客户端,与服务端通讯
第一个例子中,建立了http的服务器端,可以直接使用curl命令,或者浏览器直接访问. 在第二个例子中,建立一个netty的客户端来主动发送请求,模拟浏览器发送请求. 这里先启动服务端,再启动客户端, ...
- linux_ext4恢复超级块.txt
恢复ext4文件系统superblock 2014-04-01 17:00:17 分类: Linux 恢复ext4文件系统superblock1. Create ext4 文件系统.[root@loc ...
- August 25th, 2019. Sunday, Week 35th.
It's what you do next that counts, not what happens but what you decide to do about it. 重点不是发生了什么,而是 ...
- 使用layui框架 修改时部分参数未传给后台(查找原因)
采用的结构: <form class="layui-form reset-form" action="" id="formData"& ...
- 通过BGP实现流量劫持
BGP BGP全称是Border Gateway Protocol,翻译成中文是边界网关协议,用于全球各个AS之间的路由.它的地位是毋庸置疑的,如果没有它就没有全球的因特网.因为全球各个AS都等价的维 ...
- Yii2中$model->load($data)一直返回false问题
上次使用$model->load()方法时一直返回false,数据添加不成功,这里记录一下: 出错代码: $data = [ 'name' => 'test', 'phone' => ...