神经网络-非线性激活

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的结果

相关推荐
X54先生(人文科技)2 小时前
《元创力》纪实录·卷宗 2.2烛火传递:硅基纪元的第一个黎明
人工智能·深度学习·开源·ai写作
冰西瓜6002 小时前
深度学习的数学原理(四十三)—— 模型量化
人工智能·深度学习
Kobebryant-Manba2 小时前
记录暂退法
人工智能·深度学习
X54先生(人文科技)3 小时前
ELR-SELLM 碳硅光阴协同演进系统架构文档
人工智能·深度学习·系统架构·开源协议
Kobebryant-Manba3 小时前
记录正则化
人工智能·深度学习·机器学习
Sirius Wu3 小时前
MoE与Fengyu-Dense_架构对比及训练方案
人工智能·深度学习·算法·机器学习·语言模型·架构
daphne odera�3 小时前
Windows 环境下安装 triton、causal-conv1d 和 mamba-ssm 教程
深度学习·mamba
HERR_QQ4 小时前
端到端课程自用 8 规划 端到端与VLA 世界模型 RL的关系
人工智能·深度学习·自动驾驶·transformer
tyler_download4 小时前
揉扁搓圆transformer架构:交叉熵损失函数
人工智能·深度学习·transformer
cyyt4 小时前
深度学习周报(6.1~6.7)
人工智能·深度学习