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
参考网址:
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