网络结构:

conv --> relu --> pool --> FC -- > relu --> FC

导入包
import torch
import torch.nn.functional as F
from collections import OrderedDict
from torchsummary import summary

Method 1

class Net1(torch.nn.Module):
def __init__(self):
super(Net1, self).__init__()
self.conv1 = torch.nn.Conv2d(3, 32, 3, 1, 1)
self.dense1 = torch.nn.Linear(32 * 3 * 3, 128)
self.dense2 = torch.nn.Linear(128, 10) def forward(self, x):
# [2, 3, 6, 6]
x = F.max_pool2d(F.relu(self.conv1(x)), 2)
x = x.view(x.size(0), -1)
x = F.relu(self.dense1(x))
x = self.dense2(x)
return x print("Method 1:")
summary(Net1(), (3, 6, 6))

Method 2

class Net2(torch.nn.Module):
def __init__(self):
super(Net2, self).__init__()
self.conv = torch.nn.Sequential(torch.nn.Conv2d(3, 32, 3, 1, 1),
torch.nn.ReLU(), torch.nn.MaxPool2d(2))
self.dense = torch.nn.Sequential(torch.nn.Linear(32 * 3 * 3, 128),
torch.nn.ReLU(),
torch.nn.Linear(128, 10)) def forward(self, x):
# [2, 3, 6, 6]
x = self.conv(x)
x = x.view(x.size(0), -1)
x = self.dense(x)
return x print("Method 2:")
summary(Net2(), (3, 6, 6))

Method 3

class Net3(torch.nn.Module):
def __init__(self):
super(Net3, self).__init__()
self.conv = torch.nn.Sequential()
self.conv.add_module("conv1", torch.nn.Conv2d(3, 32, 3, 1, 1))
self.conv.add_module("relu1", torch.nn.ReLU())
self.conv.add_module("pool1", torch.nn.MaxPool2d(2))
self.dense = torch.nn.Sequential()
self.dense.add_module("dense1", torch.nn.Linear(32 * 3 * 3, 128))
self.dense.add_module("relu2", torch.nn.ReLU())
self.dense.add_module("dense2", torch.nn.Linear(128, 10)) def forward(self, x):
# [2, 3, 6, 6]
x = self.conv(x)
x = x.view(x.size(0), -1)
x = self.dense(x)
return x print("Method 3:")
#summary(Net3(), (3, 6, 6))
print(Net3())

这种方法是对第二种方法的改进:通过add_module()添加每一层,并且为每一层增加了一个单独的名字。

Method 4

class Net4(torch.nn.Module):
def __init__(self):
super(Net4, self).__init__()
self.conv = torch.nn.Sequential(
OrderedDict([("conv1", torch.nn.Conv2d(3, 32, 3, 1, 1)),
("relu1", torch.nn.ReLU()),
("pool", torch.nn.MaxPool2d(2))])) self.dense = torch.nn.Sequential(
OrderedDict([("dense1", torch.nn.Linear(32 * 3 * 3, 128)),
("relu2", torch.nn.ReLU()),
("dense2", torch.nn.Linear(128, 10))])) def forward(self, x):
# [2, 3, 6, 6]
x = self.conv(x)
x = x.view(x.size(0), -1)
x = self.dense(x)
return x print("Method 4:")
#summary(Net4(), (3, 6, 6))
print(Net4())

是第三种方法的另外一种写法,通过字典的形式添加每一层,并且设置单独的层名称。

Reference

https://www.cnblogs.com/denny402/p/7593301.html

Pytorch创建模型的多种方法的更多相关文章

  1. Gradle学习系列之二——创建Task的多种方法

    在本系列的上篇文章中,我们讲到了Gradle入门,在本篇文章中我们将讲到创建Task的多种方法. 请通过以下方式下载本系列文章的Github示例代码: git clone https://github ...

  2. ReadyAPI创建功能测试的多种方法

    原文:ReadyAPI创建功能测试的多种方法 声明:如果你想转载,请标明本篇博客的链接,请多多尊重原创,谢谢! 本篇使用的 ReadyAPI版本是2.5.0 在ReadyAPI中有多种方法可以创建功能 ...

  3. 创建Task的多种方法

    Gradle的Project从本质上说只是含有多个Task的容器,一个Task与Ant的Target相似,表示一个逻辑上的执行单元. 我们可以通过多种方式定义Task,所有的Task都存放在Proje ...

  4. cocos2dx创建sprite的多种方法

    方法一 最常用,也是最简单的一种方法 CCSprite *bg=CCSprite::create(,,,)); bg->setAnchorPoint(ccp(,)); bg->setPos ...

  5. python中创建字典的多种方法

    dict={} dict['key']='value dict={'key':"value","key2":"value2"} dict=d ...

  6. django 两种创建模型实例的方法

    1. 添加一个classmethod from django.db import models class Book(models.Model): title = models.CharField(m ...

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

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

  8. [炼丹术]使用Pytorch搭建模型的步骤及教程

    使用Pytorch搭建模型的步骤及教程 我们知道,模型有一个特定的生命周期,了解这个为数据集建模和理解 PyTorch API 提供了指导方向.我们可以根据生命周期的每一个步骤进行设计和优化,同时更加 ...

  9. 《Entity Framework 6 Recipes》翻译系列 (4) -----第二章 实体数据建模基础之从已存在的数据库创建模型

    不知道对EF感兴趣的并不多,还是我翻译有问题(如果是,恳请你指正),通过前几篇的反馈,阅读这个系列的人不多.不要这事到最后成了吃不讨好的事就麻烦了,废话就到这里,直奔主题. 2-2 从已存在的数据库创 ...

随机推荐

  1. Abusing SUDO Advance for Linux Privilege Escalation

    Index What is SUDO? Scenario. Sudoer FIle Syntax. Exploiting SUDO zip tar strace tcpdump nmap scp ex ...

  2. Python语法速查:目录

    1. 数据类型与内置函数 2. 列表.元组.字典.集合操作 3. 字符串格式化 4. 字符串常用操作 5. 运算符.math模块.表达式 6. 循环与迭代 7. 函数基础 8. 类与对象 9. 函数进 ...

  3. python(一) jupyter 安裝

    copy from https://jupyter.org/install Getting started with JupyterLab Installation JupyterLab can be ...

  4. 《软件安装》VMware Workstation 不注册 下载

    问答环节 问:为什么要下载安装VMware Workstation 答:VMware Workstation 可以安装虚拟机,我们可以把我们安装的一些软件装在虚拟机上面,防止自己的电脑卡顿(软件装多了 ...

  5. 关于soapui接口的笔记

    1.接口包含内容 #request: HTTP版本/请求地址url 请求方法:GET.POST.PUT.DELETE等 请求头:content—type 请求正文:请求参数 #response: 状态 ...

  6. Docker-Nginx,发布前端服务

    1.安装环境: yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 yum-config-manager \ --add-r ...

  7. C语言结构选择语句

    总结一下常用的if else与switch,其中switch中的break知识点是笔试题经常考到的内容. if else与else if 在C语言中,经常使用if else选择语句,来实现很多对应的功 ...

  8. SpringBoot控制台版图书借阅程序

    // 实验存档... 效果图: 完整程序:https://pan.baidu.com/s/1-d1J90dkEtM0WKkABu0K0Q 提取码:hcnm DAO层代码由MyBatis Generat ...

  9. Java描述设计模式(12):外观模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.场景描述 在移动互联网没有普及之前,去饭店吃饭的流程大致如下:选座位,排队,点菜,结账.后来移动互联网普及,通过手机APP就 ...

  10. window.innerHeight和document.documentElement.clientHeight区别

    今天有人问我这个问题,做了个小例子来记录一下子. 首先这两个都是获取可视区域的高度,那他们有什么区别呢 1.window.innerHeight属于BOM(浏览器对象模型),而document.doc ...