神经网络-卷积层

神经网络的基本骨架-nn.Module的使用

关于神经网络的工具在官方文档torch.nn中
在这里插入图片描述
在这里插入图片描述

Moudle

torch.nn.Module(*args, **kwargs)

Base class for all neural network modules. Your models should also subclass this class.

一个简单的例子

import torch
from torch import nn


# 定义一个类并继承pytorch.nn提供的Module
class Model(nn.Module):

    #  重写
    def __init__(self):
        super().__init__()

    def forward(self, input):
        output = input + 1
        return output

model = Model()
x = torch.tensor(1.0)
output = model(x)
print(output)

__init__ 方法是一个特殊的构造方法,在创建类的实例时会自动调用。在这个方法中,通过 super().__init__(),我们调用了父类nn.Module的构造方法,以确保正确初始化父类的一些属性。
forward 方法中,输入input经过某些处理后生成输出 output实际应用中,forward` 方法会根据具体的网络结构和任务需求进行定义。该方法中的计算过程会构成整个神经网络的前向传播过程。

通过继承 nn.Module 类并重写 forward 方法,可以很方便地构建自定义的神经网络模型。这样的模型可以用于训练、评估或进行其他与神经网络相关的任务。

卷积层

图像是一个二维矩阵,对于图像的操作,一般用二维卷积
在这里插入图片描述

#  调用方法
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 (str, optional) – 'zeros', 'reflect', 'replicate' or 'circular'. Default: 'zeros'. 控制padding以什么样的方式填充

     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. 
"""

stride和padding的理解

stride=1, padding=0

在这里插入图片描述

stride=1, padding=2

在这里插入图片描述

stride=2, padding=0

在这里插入图片描述

代码实现一个简单的神经网络

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

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

# 将数据集放到dataloader中进行加载
dataloader = DataLoader(dataset, batch_size=64)


# 搭建一个简单的神经网络
class Model(nn.Module):
    # 初始化
    def __init__(self):
        #  父类的初始化
        super(Model, 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

#  初始化网络
model = Model()

writer = SummaryWriter("logs")

step = 0
for data in dataloader:
    imgs, targets = data
    #  图片已经转换成了Tensor数据类型,可以直接放入网络中进行卷积操作
    output = model(imgs)
    print(imgs.shape)  # img格式为torch.Size([64, 3, 32, 32])
    print(output.shape)  # 经过神经网络转化后格式为torch.Size([64, 6, 30, 30])
    writer.add_images("input", imgs, step)

    #   torch.Size([64, 6, 30, 30])  -> [xxx, 3, 30, 30]将out_channels的6改为3
    output = torch.reshape(output, (-1, 3, 30, 30))
    writer.add_images("output", output, step)

    step = step + 1

writer.close()

控制台虚拟环境中输入以下命令打开tensorboard

tensorboard --logdir=logs

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值