当前位置:网站首页>Pytorch(二) —— 激活函数、损失函数及其梯度
Pytorch(二) —— 激活函数、损失函数及其梯度
2022-07-01 04:35:00 【CyrusMay】
Pytorch(二) —— 激活函数、损失函数及其梯度
1.激活函数
1.1 Sigmoid / Logistic
δ ( x ) = 1 1 + e − x δ ′ ( x ) = δ ( 1 − δ ) \delta(x)=\frac{1}{1+e^{-x}}\\\delta'(x)=\delta(1-\delta) δ(x)=1+e−x1δ′(x)=δ(1−δ)
import matplotlib.pyplot as plt
import torch.nn.functional as F
x = torch.linspace(-10,10,1000)
y = F.sigmoid(x)
plt.plot(x,y)
plt.show()

1.2 Tanh
t a n h ( x ) = e x − e − x e x + e − x ∂ t a n h ( x ) ∂ x = 1 − t a n h 2 ( x ) tanh(x)=\frac{e^x-e^{-x}}{e^x+e^{-x}}\\\frac{\partial tanh(x)}{\partial x}=1-tanh^2(x) tanh(x)=ex+e−xex−e−x∂x∂tanh(x)=1−tanh2(x)
import matplotlib.pyplot as plt
import torch.nn.functional as F
x = torch.linspace(-10,10,1000)
y = F.tanh(x)
plt.plot(x,y)
plt.show()

1.3 ReLU
f ( x ) = m a x ( 0 , x ) f(x)=max(0,x) f(x)=max(0,x)
import matplotlib.pyplot as plt
import torch.nn.functional as F
x = torch.linspace(-10,10,1000)
y = F.relu(x)
plt.plot(x,y)
plt.show()

1.4 Softmax
p i = e a i ∑ k = 1 N e a k ∂ p i ∂ a j = { p i ( 1 − p j ) i = j − p i p j i ≠ j p_i=\frac{e^{a_i}}{\sum_{k=1}^N{e^{a_k}}}\\ \frac{\partial p_i}{\partial a_j}=\left\{ \begin{array}{lc} p_i(1-p_j) & i=j \\ -p_ip_j&i\neq j\\ \end{array} \right. pi=∑k=1Neakeai∂aj∂pi={ pi(1−pj)−pipji=ji=j
import torch.nn.functional as F
logits = torch.rand(10)
prob = F.softmax(logits,dim=0)
print(prob)
tensor([0.1024, 0.0617, 0.1133, 0.1544, 0.1184, 0.0735, 0.0590, 0.1036, 0.0861,
0.1275])
2.损失函数
2.1 MSE
import torch.nn.functional as F
x = torch.rand(100,64)
w = torch.rand(64,1)
y = torch.rand(100,1)
mse = F.mse_loss(y,[email protected])
print(mse)
tensor(238.5115)
2.2 CorssEntorpy
import torch.nn.functional as F
x = torch.rand(100,64)
w = torch.rand(64,10)
y = torch.randint(0,9,[100])
entropy = F.cross_entropy([email protected],y)
print(entropy)
tensor(3.6413)
3. 求导和反向传播
3.1 求导
- Tensor.requires_grad_()
- torch.autograd.grad()
import torch.nn.functional as F
import torch
x = torch.rand(100,64)
w = torch.rand(64,1)
y = torch.rand(100,1)
w.requires_grad_()
mse = F.mse_loss([email protected],y)
grads = torch.autograd.grad(mse,[w])
print(grads[0].shape)
torch.Size([64, 1])
3.2 反向传播
- Tensor.backward()
import torch.nn.functional as F
import torch
x = torch.rand(100,64)
w = torch.rand(64,10)
w.requires_grad_()
y = torch.randint(0,9,[100,])
entropy = F.cross_entropy([email protected],y)
entropy.backward()
w.grad.shape
torch.Size([64, 10])
by CyrusMay 2022 06 28
人生 只是 须臾的刹那
人间 只是 天地的夹缝
——————五月天(因为你 所以我)——————
边栏推荐
- LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
- 2022年上海市安全员C证考试题模拟考试题库及答案
- Programs and processes, process management, foreground and background processes
- 嵌入式系统开发笔记79:为什么要获取本机网卡IP地址
- [learn C and fly] S1E20: two dimensional array
- Shell之分析服务器日志命令集锦
- JS rotation chart
- Kodori tree board
- Shell之Unix运维常用命令
- Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
猜你喜欢

Use winmtr software to simply analyze, track and detect network routing

Account sharing technology enables the farmers' market and reshapes the efficiency of transaction management services

2022年化工自动化控制仪表操作证考试题库及答案
![[godot] unity's animator is different from Godot's animplayer](/img/51/48f40a7b6736d7f78040eabbbd3395.jpg)
[godot] unity's animator is different from Godot's animplayer

尺取法:有效三角形的个数

js 图片路径转换base64格式

Dual contractual learning: text classification via label aware data augmentation reading notes
![[pat (basic level) practice] - [simple simulation] 1064 friends](/img/37/0ef0f8aae15ae574be1d76c97497c9.jpg)
[pat (basic level) practice] - [simple simulation] 1064 friends

VR线上展览所具备应用及特色

测量三相永磁同步电机的交轴直轴电感
随机推荐
Task04 mathematical statistics
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and
嵌入式系統開發筆記80:應用Qt Designer進行主界面設計
Some small knowledge points
总结全了,低代码还需要解决这4点问题
Mallbook: how can hotel enterprises break the situation in the post epidemic era?
Shell analysis server log command collection
CUDA development and debugging tool
[send email with error] 535 error:authentication failed
Day 52 - tree problem
2022危险化学品生产单位安全生产管理人员题库及答案
Maixll-Dock 快速上手
Obtain detailed ideas for ABCDEF questions of 2022 American Games
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
slf4j 简单实现
网站服务器:好用的网站服务器怎么选这五方面要关注
Qt development experience tips 226-230
尺取法:有效三角形的个数
What is uid? What is auth? What is a verifier?