最大池化、非线性激活、线性层

一、最大池化原理

二、最大池化实例

复制代码
import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("../chihua",train=False,
                               download=True,transform=torchvision.transforms.ToTensor()) # 对数据集的操作
dataloader = DataLoader(dataset,batch_size=64) # 加载数据集

构建最大池化神经网络:

复制代码
class SUN(nn.Module):
    def __init__(self):
        super(SUN, self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=False)

    def forward(self, input):
        output = self.maxpool1(input)
        return output

sun = SUN()

使用tensorboard显示图片:

复制代码
writer = SummaryWriter("../logs_maxpool")
step = 0

for data in dataloader:
    imgs,targets = data
    writer.add_image("input", imgs,  step, dataformats="NCHW")# 注意输入的图片,可能出现数据类型的不匹配
    output = sun(imgs)
    writer.add_image("output", output, step, dataformats="NCHW") # 数据通道的设置
    step +=1

writer.close()

显示的结果:

池化的作用,减小了像素,但是对应的,变得更加模糊。

三、非线性激活

非线性激活的作用,就是主要是给模型加上一些非线性特征,非线性特征越多,才能训练出符合各种特征的模型,提高模型的泛化能力。

四、非线性激活的实例

复制代码
import torch
import torchvision
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

input = torch.tensor([[1,-0.5],
                      [-1,3]])
input = torch.reshape(input,(-1, 1, 2, 2))
print(input.shape)

dataset = torchvision.datasets.CIFAR10("../datas", train = False, download=True,
                                       transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64)

class SUN(nn.Module):
    def __init__(self):
        super(SUN, self).__init__()
        self.relu1 = ReLU() # 添加对应的网络
        self.sigmoid = Sigmoid()

    def forward (self, input):
        output = self.sigmoid(input) # 使用了Sigmoid函数
        return output

sun = SUN()
step = 0
write = SummaryWriter("../logs_relu")
for data in dataloader:
    imgs,targets = data
    write.add_image("input", imgs, global_step=step)
   output = sun(imgs)
    write.add_image("output",output,global_step=step)
    step +=1

write.close()

结果

五、 线性层

主要作用是通过线性变换将输入数据映射到一个新的空间,改变数据的维度,便于后续层进一步处理。‌

六、线性层实例

复制代码
import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("../datalinear", train=False,
                                       transform=torchvision.transforms.ToTensor(), download=True)

dataloader = DataLoader(dataset, batch_size=64, drop_last=True)# 此处特别注意,要设置该参数,否则出现报错:
python 复制代码
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x49152 and 196608x10)
复制代码
class SUN(nn.Module):
    def __init__(self):
        super(SUN, self).__init__()
        self.linear1 = Linear(196608, 10)

    def forward(self,input):
        output = self.linear1(input)
        return output

sun = SUN()


# writer = SummaryWriter("../logslinear")
# step = 0

for data in dataloader:
    imgs, targets = data
    print(imgs.shape)
    # output = torch.reshape(imgs,(1, 1, 1, -1))
    # 展平
    output = torch.flatten(imgs)
    print(output.reshape)
    output = sun(output)
    print(output.shape)

结果:

将196608的in_future输出out_future变为10。

相关推荐
千寻girling2 小时前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
千寻girling2 小时前
Python 是用来做 AI 人工智能 的 , 不适合开发 Web 网站 | 《Web框架》
人工智能·后端·算法
AI攻城狮2 小时前
OpenClaw 里 TAVILY_API_KEY 明明写在 ~/.bashrc,为什么还是失效?一次完整排查与修复
人工智能·云原生·aigc
stark张宇2 小时前
构建第一个AI聊天机器人:Flask+DeepSeek+Postgres实战
人工智能·postgresql·flask
yiyu07164 小时前
3分钟搞懂深度学习AI:自我进化的最简五步法
人工智能·深度学习
浪浪山_大橙子6 小时前
OpenClaw 十分钟快速,安装与接入完全指南 - 推荐使用trae 官方 skills 安装
前端·人工智能
databook6 小时前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
火山引擎开发者社区6 小时前
OpenClaw 快速上手:把云手机变成你的 7×24 小时 AI 手机助手
人工智能
Qlly6 小时前
DDD 架构为什么适合 MCP Server 开发?
人工智能·后端·架构
Lee川6 小时前
从零构建智能对话系统:AI Agent 实战指南
人工智能