利用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 建立模型的几种方法的更多相关文章

  1. ThinkPHP学习笔记 实例化模型的四种方法

    创建Action类   [php]   <?php   class NewObjectAction extends Action{       public function index(){ ...

  2. pytorch 创建tensor的几种方法

    tensor默认是不求梯度的,对应的requires_grad是False. 1.指定数值初始化 import torch #创建一个tensor,其中shape为[2] tensor=torch.T ...

  3. 莫烦python教程学习笔记——保存模型、加载模型的两种方法

    # View more python tutorials on my Youtube and Youku channel!!! # Youtube video tutorial: https://ww ...

  4. pytorch学习: 构建网络模型的几种方法

    利用pytorch来构建网络模型有很多种方法,以下简单列出其中的四种. 假设构建一个网络模型如下: 卷积层-->Relu层-->池化层-->全连接层-->Relu层--> ...

  5. EF Core中避免贫血模型的三种行之有效的方法(翻译)

    Paul Hiles: 3 ways to avoid an anemic domain model in EF Core 1.引言 在使用ORM中(比如Entity Framework)贫血领域模型 ...

  6. 自然语言处理的CNN模型中几种常见的池化方法

    自然语言处理的CNN模型中几种常见的池化方法 本文是在[1]的基础上进行的二次归纳. 0x00 池化(pooling)的作用   首先,回顾一下NLP中基本的CNN模型的卷积和池化的大致原理[2].f ...

  7. java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区

    java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测).链地址法.再哈希.建立公共溢出区 标签: hashmaphashmap冲突解决冲突的方法冲突 2016-0 ...

  8. PyQt(Python+Qt)学习随笔:QStandardItemModel指定行和列创建模型后的数据项初始化的两种方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QStandardItemModel通过构造方法 QStandardItemModel(int ro ...

  9. Pytorch的模型加速方法:Dataparallel (DP) 和 DataparallelDistributedparallel (DDP)

    Dataparallel 和 DataparallelDistributed 的区别 一.Dataparallel(DP) 1.1 Dartaparallel 的使用方式 Dataparallel 的 ...

随机推荐

  1. IDEA 下使用JSTL 非maven

    原文链接:https://www.cnblogs.com/xiehang/p/9430342.html 习惯了eclipse和myeclipse开发的我们总是依赖于系统的插件,而当我想当然的以为Int ...

  2. mongodb主备配置

    前言:mongodb目前推荐的方式是副本集的方式实现,但是副本集需要三台服务器,目前配置为主备方式 假设你已经安装好了mongo,并配置好了响应的用户 下面修改mongodb.conf配置文件,开启认 ...

  3. Vm虚拟机最小化安装linux并配置NAT网络连接(全图)

  4. OOAD 面向对象的分析与设计

      OOAD  面向对象的分析与设计            OOA-----分析阶段(针对业务问题清晰视图, 列出系统完成任务,  整理业务的公共词汇,  列出解决业务的解决方法)         O ...

  5. cordova+vue混合式开发App

    应要求第一次使用cordova打包了一下vue写的app项目,期间遇到了不少问题,整理一下流程并记录一下常见问题吧.        cordova打包项目需要的环境配置啥的就不具体讲啦,百度一下很多教 ...

  6. ajax配置项中的type与method

    1. jQuery中ajax配置项中的使用type与method的区别本质上两个配置项是没有区别的,区别在于两者出现的时间不同,type对于目前jQuery的版本全部兼容,也就是说$.ajax({ t ...

  7. java之对象创建时各成员变量的初始值

    除了byte short int long float double char bollean这基础类型外,其余的都是引用类型 成员变量类型 初始值 byte 0 short 0 int 0 long ...

  8. HashMap如何在Java中工作?

    通过优锐课学习笔记分享,我们可以看到HashMap问题在工作面试中很常见. 这也是HashMaps在Java内部如何工作的一些深入说明,分享给大家参考学习. HashMap在内部如何工作已成为几乎所有 ...

  9. ETC到底要不要办?有什么好处?

    一说到ETC,开车的朋友想必不会陌生.但很多车友却不太愿意办理ETC, 究其原因,主要是一些谣言所致,一传一十传百最后变成了真实的谎言,并且对此深信不疑, 比如下面5个广泛流传的谣言     在来看看 ...

  10. HttpModules配置事项

    前沿:还是那句话 ASP.NET管道,浏览器 - isAPI32.dll - HttpModules - HttpHandler - 返回客户端Web.Config:<httpModules&g ...