Conv2d

1
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)
  • in_channels (int) – Number of channels in the input image
  • out_channels (int) – Number of channels produced by the convolution
  • kernel_size (int_ or _tuple) – Size of the convolving kernel
  • stride (int_ or _tuple, optional) – Stride of the convolution. Default: 1 # 步长
  • padding (int,_ _tuple_ or _str,_ _optional) – Padding added to all four sides of the input. Default: 0
  • padding_mode (str,_ _optional) – ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’
  • dilation (int_ or _tuple, optional) – Spacing between kernel elements. Default: 1 #空洞卷积
  • groups (int,_ _optional) – Number of blocked connections from input channels to output channels. Default: 1
  • bias (bool, optional) – If True, adds a learnable bias to the output. Default: True

image.png
image.png
in_channels与out_channels计算公式
image.png
image.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :Pytorch_learn
@File :conv2d_and_pool.py
@IDE :PyCharm
@Author :咋
@Date :2023/7/3 16:20
"""
import torch
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms
from torch import nn
from torch.nn import Module
from tensorboardX import SummaryWriter
dataset = datasets.CIFAR10("CIFAR10",train=True,transform=transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset=dataset,batch_size=64)


class MyCNN(Module):
def __init__(self):
super(MyCNN, self).__init__()
self.conv1 = nn.Conv2d(3,6,(3,3))
def forward(self,x):
x = self.conv1(x)
return x

CNN = MyCNN()
write = SummaryWriter("log_3")
for i,data in enumerate(dataloader):
img,label = data
write.add_images("input",img,i)
print(img.shape)

output = CNN(img)
print(output.shape)
output = torch.reshape(output,(-1,3,30,30))
print(output.shape)
write.add_images("output",output,i)

write.close()

image.png

MAXPOOL2D

1
torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
  • kernel_size (Union_[_**int,_ Tuple[int,__ int]_]) – the size of the window to take a max over**
  • stride (Union_[_**int, Tuple[_int, int]]_) – the stride of the window. Default value is kernel_size # 默认是kernel_size大小**
  • padding (Union_[_**int,_ Tuple[int,__ int]_]) – Implicit negative infinity padding to be added on both sides**
  • dilation (Union_[_**int,_ Tuple[int,__ int]_]) – a parameter that controls the stride of elements in the window**
  • return_indices (bool) – if True, will return the max indices along with the outputs. Useful for torch.nn.MaxUnpool2d later
  • ceil_mode (bool) – when True, will use ceil instead of floor to compute the output shape#有点去1法和进1法的意思

作用:保持特征,减少参数
in_channel与out_channel计算关系
image.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :Pytorch_learn
@File :conv2d_and_pool.py
@IDE :PyCharm
@Author :咋
@Date :2023/7/3 16:20
"""
import torch
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms
from torch import nn
from torch.nn import Module
from tensorboardX import SummaryWriter
dataset = datasets.CIFAR10("CIFAR10",train=True,transform=transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset=dataset,batch_size=64)


class MyCNN(Module):
def __init__(self):
super(MyCNN, self).__init__()
self.conv1 = nn.Conv2d(3,6,(3,3))
self.pool2d = nn.MaxPool2d(3)
def forward(self,x):
x = self.conv1(x)
x = self.pool2d(x)
return x

CNN = MyCNN()
write = SummaryWriter("log_3")
for i,data in enumerate(dataloader):
img,label = data
write.add_images("input",img,i)
print(img.shape)

output = CNN(img)
print(output.shape)
output = torch.reshape(output,(-1,3,10,10))
print(output.shape)
write.add_images("output",output,i)

write.close()

image.png
自定义tensor,记得加上dtype=torch.float32

非线性激活函数

RELU

image.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :Pytorch_learn
@File :RELU.py
@IDE :PyCharm
@Author :咋
@Date :2023/7/3 17:24
"""
from torch.nn import ReLU, Module
import torch


class MyModule(Module):
def __init__(self):
super(MyModule, self).__init__()
self.relu = ReLU(inplace=False)
def forward(self,x):
x = self.relu(x)
return x



tensor = torch.tensor([[1,-1],[0.5,-0.5]])
model = MyModule()
output = model(tensor)
print(output)
'''
output:
tensor([[1.0000, 0.0000],
[0.5000, 0.0000]])

'''

sigmoid

image.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :Pytorch_learn
@File :Sigomid.py
@IDE :PyCharm
@Author :咋
@Date :2023/7/3 17:30
"""
from torch.nn import Sigmoid, Module
import torch


class MyModule(Module):
def __init__(self):
super(MyModule, self).__init__()
self.sigmoid = Sigmoid()
def forward(self,x):
x = self.sigmoid(x)
return x



tensor = torch.tensor([[1,-1],[0.5,-0.5]])
model = MyModule()
output = model(tensor)
print(output)
'''
output:
tensor([[0.7311, 0.2689],
[0.6225, 0.3775]])

'''

增强非线性变化,加强对特征的提取,减少过拟合,加强模型泛化能力。

线性层

**torch.nn.Linear

1
torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)

image.png

  • in_features (int) – size of each input sample
  • out_features (int) – size of each output sample
  • bias (bool) – If set to False, the layer will not learn an additive bias. Default: True