Pytorch深度学习-神经网络-卷积层(小土堆)

  • 主要为Conv2d->2D卷积
Conv2d详细
  1. 查询官网https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d
  2. API
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
  1. 参数
1. in_channels (int) – 输入图像中的通道数
2. out_channels (int) - 卷积产生的通道数
3. kernel_size (int or tuple) – 卷积内核的大小
4. stride (int 或 tuple,可选) - 卷积的步幅。默认值:1
5. padding (int, tuple or str, 可选) – 添加到输入的所有四个边的填充。默认值:0
6. padding_mode (str, 可选) - 'zeros','reflect','circular'。默认值:'zeros'
7. dilation (int or tuple, 可选) - 内核元素之间的间距。默认值:1
8. groups (int, 可选) - 从输入通道到输出通道的阻塞连接数。默认值:1
9. bias (bool, 可选) - 如果 `True` ,则在输出中添加可学习的偏差。默认: `True`
  1. 官网直观图片网址:https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md
  2. 代码示例
'''
原图片: torch.Size([64, 3, 32, 32])
卷积后图片: torch.Size([64, 6, 30, 30])
'''
import torch  
import torchvision  
from torch.utils.data import DataLoader  
import torch.nn as nn  
from torch.nn import Conv2d  
  
dataset = torchvision.datasets.CIFAR10("./datasets",train=False,transform=torchvision.transforms.ToTensor(),download=True)  
  
dataloader = DataLoader(dataset,batch_size=64)  
  
class conv_test(nn.Module):  
  
    def __init__(self, *args, **kwargs) -> None:  
        super().__init__(*args, **kwargs)  
        #这里传入的是图片,所以输出通道是3  
        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  
  
test1 = conv_test()  
for data in dataloader:  
    imgs,targets = data  
    output = test1(imgs)  
    print("原图片:",imgs.shape)  
    print("卷积后图片:",output.shape)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值