热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Pytorch创建模型的多种方法

目录Method1Method2Method3Method4Reference网络结构:conv–relu–pool–FC—relu–FC导入包importtorchim

目录

  • Method 1
  • Method 2
  • Method 3
  • Method 4
  • Reference
网络结构:

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))

《Pytorch创建模型的多种方法》

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))

《Pytorch创建模型的多种方法》

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())

《Pytorch创建模型的多种方法》

这种方法是对第二种方法的改进:通过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())

《Pytorch创建模型的多种方法》

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

Reference

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


推荐阅读
author-avatar
小Reve_942
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有