当前位置:网站首页>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 )——————
边栏推荐
- Basic exercise of test questions hexadecimal to decimal
- Summary of testing experience - Testing Theory
- Neural network convolution layer
- 1076 Forwards on Weibo
- Neural networks - use of maximum pooling
- LeetCode_ 28 (implement strstr())
- Pytorch(三) —— 函数优化
- 无器械健身
- Solve the problem that the external chain file of Qiankun sub application cannot be obtained
- C read / write application configuration file app exe. Config and display it on the interface
猜你喜欢

测量三相永磁同步电机的交轴直轴电感

How to use common datasets in pytorch

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

pytorch 卷积操作

数据加载及预处理

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

Dede collection plug-in does not need to write rules

【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点

手动实现一个简单的栈

LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
随机推荐
数据加载及预处理
JVM栈和堆简介
STM32 photoresistor sensor & two channel AD acquisition
Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
【FTP】FTP连接时出现“227 Entering Passive Mode”的解决方法
Pytorch convolution operation
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism
STM32 extended key scan
Advanced application of ES6 modular and asynchronous programming
Extension fragment
Pytorch(二) —— 激活函数、损失函数及其梯度
常用的Transforms中的方法
Quelques outils dont les chiens scientifiques pourraient avoir besoin
LeetCode_58(最后一个单词的长度)
How to use maixll dock
LeetCode_ 66 (plus one)
分布式全局唯一ID解决方案详解
LeetCode_53(最大子数组和)
科研狗可能需要的一些工具
Difficulties in the development of knowledge map & the importance of building industry knowledge map