pytorch教程及实例

pytorch的语法,参考博客:https://www.cnblogs.com/hellcat/tag/PyTorch/default.html?page=1

一、Resnet——pytorch模型的快速实现

import torch as t
import torch.nn as nn
from torch.nn import functional as F
 
 
class ResidualBlock(nn.Module):
    def __init__(self, inchannel, outchannel, stride=1, shortcut=None):
        super(ResidualBlock, self).__init__()
        self.left = nn.Sequential(
            nn.Conv2d(inchannel, outchannel, kernel_size=3, stride=stride, padding=1),
            nn.BatchNorm2d(outchannel),
            nn.ReLU(inplace=True),
            nn.Conv2d(outchannel, outchannel, 3, 1, 1, bias=False),
            nn.BatchNorm2d(outchannel)
        )
        self.right = shortcut
 
    def forward(self, x):
        out = self.left(x)
        residual = x if self.right is None else self.right(x)
        out += residual
        return F.relu(out)
 
 
class ResNet(nn.Module):
    def __init__(self, num_classes=1000):
        super(ResNet, self).__init__()
        self.pre = nn.Sequential(
            nn.Conv2d(3, 64, 7, 2, 3, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        )
        self.layer1 = self._make_layer(inchannel=64, outchannel=128, block_num=3)
        self.layer2 = self._make_layer(inchannel=128, outchannel=256, block_num=4, stride=2)
        self.layer3 = self._make_layer(inchannel=256, outchannel=512, block_num=6, stride=2)
        self.layer4 = self._make_layer(inchannel=512, outchannel=512, block_num=3, stride=2)
 
        self.fc = nn.Linear(512, num_classes)
 
    def _make_layer(self, inchannel, outchannel, block_num, stride=1):
        shortcut = nn.Sequential(
            nn.Conv2d(inchannel, outchannel, 1, stride, bias=False),
            nn.BatchNorm2d(outchannel)
        )
        layers = []
        layers.append(ResidualBlock(inchannel, outchannel, stride, shortcut))
        for i in range(1, block_num):
            layers.append(ResidualBlock(outchannel, outchannel))
        return nn.Sequential(*layers)
 
    def forward(self, x):
        x = self.pre(x)  # [1, 64, 56, 56]
        x = self.layer1(x)  # [1, 128, 56, 56]
        x = self.layer2(x)  # [1, 256, 28, 28]
        x = self.layer3(x)  # [1, 512, 14, 14]
        x = self.layer4(x)  # [1, 512, 7, 7]
 
 
 
        x = F.avg_pool2d(x, 7)
        x = x.view(x.size(0), -1)
        return self.fc(x)
 
def hook(module, inputdata, output):
    '''把这层的输出拷贝到features中'''
    print("钩子输出:", output.data.size())
 
 
module = ResNet()
img = t.autograd.Variable(t.randn(1, 3, 224, 224))
handle = module.pre[0].register_forward_hook(hook)
out = module(img)
handle.remove()
print(out)

上面代码中,我们注册了钩子尝试分析一下中间的输出,可以看到,torch中的卷积层默认是SAME模式,输出就是in/stride,和TensorFlow一致。

结果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是小白学习PyTorch的一些教程: 1. 官方文档:PyTorch提供了详细的官方文档,从安装到使用教程,以及高级深度学习开发的资料。学习PyTorch的第一步是查看官方文档:https://pytorch.org/docs/stable/index.html 2. PyTorch中文文档:如果英语不是很好,这是一个很好的PyTorch中文文档。虽然有一些不是很清晰或者过时的部分,但是它仍然是较好的教程之一。:https://pytorch-cn.readthedocs.io/zh/latest/ 3. PyTorch Handbook:PyTorch Handbook汇集了PyTorch的基础知识和高级技巧,适合新手学习,也适合进阶使用PyTorch的人参考。:https://github.com/zergtant/pytorch-handbook 4. Udacity深度学习班“入门PyTorch”课程:入门PyTorch是Udacity的深度学习班的一门课程。 该课程提供了关于PyTorch的综合介绍,包括从张量到神经网络的构建。该课程的重点是实战:利用 PyTorch 实现著名的 MNIST 实例,训练卷积神经网络,基于迁移学习的图像分类等等。:https://www.udacity.com/course/deep-learning-pytorch--ud188 5. PyTorch实战教程:完整的 PyTorch 实战教程,包括深度神经网络,零件库,图像和自然语言处理等:https://github.com/yunjey/pytorch-tutorial 6. PyTorch 60分钟教程PyTorch 60分钟教程PyTorch 的入门课程,该课程提供了有关 PyTorch 库和 API 的指南。:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 7. 深度学习理论入门:这本书不仅介绍了深度学习领域的基础知识,还介绍了用PyTorch实现深度学习模型的方法,并且包含了许多实际案例示例。:https://github.com/huanhuanZhang/rampy/tree/main/PyTorch 以上是小白学习PyTorch的一些教程PyTorch是一个强大的深度学习框架,它的文档和教程都很详细。选择合适的教程和实践,不断探索和学习,才能真正掌握这个框架。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值