Pytorch使用小技巧

1、nn.Conv2d默认padding为'valid',如何设置为'same'?

使用公式计算:

  • o = output
  • p = padding
  • k = kernel_size
  • s = stride
  • d = dilation
o = [i + 2*p - k - (k-1)*(d-1)]/s + 1

参考网址:

https://discuss.pytorch.org/t/how-to-keep-the-shape-of-input-and-output-same-when-dilation-conv/14338

2、在创建网络的class NetworkName(nn.Module):的def __init__(self):中,一定要加上super(NetworkName, self).__init__()

否则会报错:AttributeError: cannot assign module before Module.__init__() call

正确使用方式(代码片段来源:Neural Networks - Pytorch Tutorials):

import torch
import torch.nn as nn
import torch.nn.functional as F


class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 5x5 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

参考网址:

https://discuss.pytorch.org/t/attributeerror-cannot-assign-module-before-module---init---call/1446

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值