当前位置:网站首页>Pytoch (II) -- activation function, loss function and its gradient
Pytoch (II) -- activation function, loss function and its gradient
2022-07-01 04:46:00 【CyrusMay】
Pytorch( Two ) —— Activation function 、 The loss function and its gradient
1. Activation function
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. Loss function
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. Derivation and back propagation
3.1 Derivation
- 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 Back propagation
- 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
life It's just In a moment
human It's just Between heaven and earth
—————— May day ( Because of you So I )——————
边栏推荐
- Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
- 【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)
- Kodori tree board
- [FTP] the solution to "227 entering passive mode" during FTP connection
- 分布式数据库数据一致性的原理、与技术实现方案
- [2020 overview] overview of link prediction based on knowledge map embedding
- 解决:Thread 1:[<*>setValue:forUndefinedKey]:this class is not key value coding-compliant for the key *
- Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
- Summary of testing experience - Testing Theory
- [ue4] event distribution mechanism of reflective event distributor and active call event mechanism
猜你喜欢

Kodori tree board

STM32扩展板 数码管显示

LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver

How to do the performance pressure test of "Health Code"

Ten wastes of software research and development: the other side of research and development efficiency

LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target

Basic usage, principle and details of session

Pytorch convolution operation

The longest increasing subsequence and its optimal solution, total animal weight problem

Pytorch(三) —— 函数优化
随机推荐
Technology sharing | broadcast function design in integrated dispatching
Pytorch(一) —— 基本语法
LeetCode_ 66 (plus one)
How to use common datasets in pytorch
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
OdeInt與GPU
Pytorch(四) —— 可视化工具 Visdom
Overview of the construction details of Meizhou veterinary laboratory
Leecode question brushing record 1332 delete palindrome subsequence
神经网络的基本骨架-nn.Moudle的使用
STM32 光敏电阻传感器&两路AD采集
Ten wastes of software research and development: the other side of research and development efficiency
Dual contractual learning: text classification via label aware data augmentation reading notes
VR线上展览所具备应用及特色
科研狗可能需要的一些工具
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
Some tools that research dogs may need
Shell之一键自动部署Redis任意版本
C read / write application configuration file app exe. Config and display it on the interface
AssertionError assert I.ndim == 4 and I.shape[1] == 3