PyTorch快速入门教程【小土堆】之卷积层
视频地址神经网络-卷积层_哔哩哔哩_bilibili
in_channels (int) -输入图像的通道数 |
out_channels (int) -输出图像的通道数 |
kernel_size (int或tuple)-卷积核的大小 |
stride(int或tuple,可选)-卷积的步幅。默认值:1 |
padding (int, tuple或str,可选)-填充四个边的值。默认值:0 |
dilation(int或tuple,可选)-内核元素之间的间距。默认值:1 |
groups (int,可选)-从输入通道到输出通道的阻塞连接数。默认值:1 |
bias (bool,可选)-如果为True,则在输出中添加可学习的偏差。默认值:True |
padding_mode (str,可选)- 填充的模式'zero ', ‘reflect‘, ‘ replication ’或’circular’。默认值:“0” |
长宽计算公式
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
dataset = torchvision.datasets.CIFAR10("CIFAR10", train=False, transform=torchvision.transforms.ToTensor(),
download=True)
dataloader = DataLoader(dataset, batch_size=64)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
def forward(self, x):
x = self.conv1(x)
return x
tudui = Tudui()
for data in dataloader:
imgs, targets = data
output = tudui(imgs)
print(imgs.shape) # torch.Size([64, 3, 32, 32]) ,批次数,卷积前通道数,长,宽
print(output.shape) # torch.Size([64, 6, 30, 30]) ,批次数,经过卷积后通道数,经过卷积后长,经过卷积后宽