栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

神经网络基本结构的使用(Convolution Layers的使用)2

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

神经网络基本结构的使用(Convolution Layers的使用)2

CLASS 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)

Parameters:
in_channels (int) – Number of channels in the input image 输入图像的通道数,彩色图像通道数 3
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 (string, 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

演示链接:https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md


代码实战:

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

# train=False为下载测试数据集还是训练数据集。此时为下载测试数据集
dataset = torchvision.datasets.CIFAR10("../data", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)
dataloader = DataLoader(dataset, batch_size=4)


class Peipei(nn.Module):
    def __init__(self):
        super(Peipei, 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


peipei = Peipei()
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
    imgs, targets = data
    # print(imgs.shape)
    # 完成卷积操作并输出
    output = peipei(imgs)
    # print(output.shape)
    # torch.Size([4, 3, 32, 32])
    writer.add_images("input", imgs, step)
    # torch.Size([4, 6, 30, 30]) 6个通道无法显示
    output = torch.reshape(output, (-1, 3, 30, 30))
    writer.add_images("output", output, step)
    step = step + 1

writer.close()

输出:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/869881.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号