Python PyTorch2:自动求导、梯度

本文介绍了神经网络中常见的激活函数,如Sigmoid、tanh和ReLU,并通过PyTorch展示了它们的输出。同时,讨论了均方误差作为损失函数时的梯度计算。此外,还演示了感知机模型的梯度推导和自动求导的基本用法,包括计算梯度和反向传播的过程。这些内容对于理解深度学习模型的训练过程至关重要。
摘要由CSDN通过智能技术生成

1. 激活函数

激活函数是在人工神经网络的神经元上运行的函数,负责将神经元的输入映射到输出端。

常用的激活函数:

import torch

a = torch.linspace(-10, 10, 20)
# sigmoid函数 0~1
res = torch.sigmoid(a)
print(res)
# tanh函数 -1~1
res = torch.tanh(a)
print(res)
# ReLU函数 0~inf
res = torch.relu(a)
print(res)

输出:

tensor([4.5398e-05, 1.3006e-04, 3.7256e-04, 1.0667e-03, 3.0503e-03, 8.6901e-03,
        2.4502e-02, 6.7134e-02, 1.7094e-01, 3.7138e-01, 6.2862e-01, 8.2906e-01,
        9.3287e-01, 9.7550e-01, 9.9131e-01, 9.9695e-01, 9.9893e-01, 9.9963e-01,
        9.9987e-01, 9.9995e-01])
tensor([-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -0.9998, -0.9987, -0.9897,
        -0.9184, -0.4826,  0.4826,  0.9184,  0.9897,  0.9987,  0.9998,  1.0000,
         1.0000,  1.0000,  1.0000,  1.0000])
tensor([ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
         0.0000,  0.0000,  0.5263,  1.5789,  2.6316,  3.6842,  4.7368,  5.7895,
         6.8421,  7.8947,  8.9474, 10.0000])

2. loss的梯度

均方误差(MSE)是回归损失函数中常用的误差,它是预测值与目标值之间差值平方和的均值。

import torch
import torch.nn.functional as F

x = torch.ones(1)
w = torch.full([1], 2.)
w.requires_grad_()
# 均方误差
mse = F.mse_loss(torch.ones(1), x * w)
print(x, w, mse)
# 梯度
grad = torch.autograd.grad(mse, [w])
print(grad)

输出:

tensor([1.]) tensor([2.], requires_grad=True) tensor(1., grad_fn=<MseLossBackward>)
(tensor([2.]),)

import torch
import torch.nn.functional as F

a = torch.rand(3)
a.requires_grad_()
p = F.softmax(a, dim=0)
print(torch.autograd.grad(p[1], [a], retain_graph=True))
print(torch.autograd.grad(p[2], [a]))

输出:

(tensor([-0.1407,  0.2497, -0.1090]),)
(tensor([-0.0658, -0.1090,  0.1748]),)

3. 感知机的梯度推导

import torch
import torch.nn.functional as F

x = torch.randn(1, 10)
w = torch.randn(1, 10, requires_grad=True)
o = torch.sigmoid(x @ w.t())
print(o.shape)

loss = F.mse_loss(torch.ones(1, 1), o)
loss.backward()
print(w.grad)

输出:

torch.Size([1, 1])
tensor([[ 0.4680, -0.5402,  0.2481, -0.1499, -0.0996, -0.2685, -0.2663,  0.2420,
         -0.3194, -0.1558]])

4. 自动求导

import torch

# requires_grad=True x可以计算梯度
x = torch.ones((2, 2), requires_grad=True)
print(x)
y = x + 2
print(y)
z = y * y * 3
print(z)
# out的结果是z的平均值 这里的out结果是与x相关的
out = z.mean()
print(out)
# backward 对out求导,计算out的梯度
out.backward()
# 输出x的梯度
print(x.grad)
print(y.grad)

输出:

tensor([[1., 1.],
        [1., 1.]], requires_grad=True)
tensor([[3., 3.],
        [3., 3.]], grad_fn=<AddBackward0>)
tensor([[27., 27.],
        [27., 27.]], grad_fn=<MulBackward0>)
tensor(27., grad_fn=<MeanBackward0>)
tensor([[4.5000, 4.5000],
        [4.5000, 4.5000]])
None

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hinomoto Oniko

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值