神经网络——卷积层(conv2d)

输入参数
图1. 输入参数

in_channels:输入通道个数。例如彩色图片有3个通道
out_channels:输出通道个数。out_channals=2时,会生成2个卷积核与输入图像进行卷积。
kernel_size:卷积核大小。
padding_mode:padding填充模式。
groups:一般设置成1
bias:偏置。一般设置为True
计算图片长和宽的方法
图2. 计算图像长和宽的方法

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

# 准备数据集
dataset = torchvision.datasets.CIFAR10("../pytorch_learn/dataset2", train=False, transform=torchvision.transforms.ToTensor(), download=True)

# 下载数据
dataloader = DataLoader(dataset, batch_size=64, shuffle=True)

# 搭建简单神经网络
class conv(nn.Module):
    def __init__(self):
        super(conv, self).__init__()
        self.cov1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)

    def forward(self, x):
        x = self.cov1(x)
        return x

writer = SummaryWriter("logs")

conv = conv()
# 将每张图像放到神经网络中
step = 0
for data in dataloader:
    img, target = data
    output = conv(img)
    # torch.Size([64, 3, 32, 32])
    writer.add_images("input", img, step)
    # torch.Size([64, 6, 30, 30])
    # 由于输出是6通道的,而输入是3通道的。所以会报错,需要进行变换
    output = torch.reshape(output, (-1, 3, 30, 30))
    writer.add_images("output", output, step)
    step += 1

writer.close()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值