神经网络-非线性激活

ReLU

python 复制代码
import torch
from torch import nn

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

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.relu1 = nn.ReLU()

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

tudui = Tudui()
output = tudui(input)
print(output) # tensor([[[[1., 0.],[0., 3.]]]])

Result

Sigmoid

python 复制代码
import torch
import torchvision
from torch import nn
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) # torch.Size([1, 1, 2, 2])     .shape = .size()'''

dataset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64, shuffle=True)
class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.relu1 = nn.ReLU()
        self.sigmoid1 = nn.Sigmoid()

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

tudui = Tudui()
writer = SummaryWriter('./logs_relu')
step = 0
for data in dataloader:
    imgs, targets = data
    writer.add_images('input', imgs, step)
    output = tudui(imgs)
    writer.add_images('output', output, step)
    step += 1

writer.close()

目的:引入非线性特征,非线性越多,才能训练出符合各种曲线,符合各种特征的模型,泛化能力好

下面是ReLU的结果

相关推荐
做个文艺程序员1 小时前
华为昇腾NPU部署开源大模型全攻略(以Qwen3-8B为例)
人工智能·深度学习·华为
weiwei228442 小时前
torch.nn神经网络详解
pytorch·mnist·手写识别
rebekk2 小时前
PyTorch Dispatcher介绍
人工智能·pytorch·python
gorgeous(๑>؂<๑)3 小时前
【CVPR26-索尼】EW-DETR:通过增量低秩检测Transformer实现动态世界目标检测
人工智能·深度学习·目标检测·计算机视觉·transformer
胖祥4 小时前
onnx之优化器
人工智能·深度学习
li三河5 小时前
paddleocr识别和推理,并用MNN进行推理
人工智能·深度学习·mnn
bryant_meng5 小时前
【AI】《Explainable Machine Learning》(2)
人工智能·深度学习·机器学习·计算机视觉·explanation
老鱼说AI5 小时前
大模型学习与面试精讲第六期:损失函数篇
人工智能·深度学习·神经网络·学习·机器学习·语言模型
Joker 0076 小时前
Ubuntu上安装FreeSurfer的详细流程
linux·深度学习·ubuntu
智算菩萨7 小时前
【论文复现】ML-MLM:基于PyTorch的多标签极小学习机完整复现教程(附GPT-5.4辅助科研提示词工程)
人工智能·pytorch·python·gpt·深度学习·论文笔记