当前位置:网站首页>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 )——————
边栏推荐
- Codeforces Round #771 (Div. 2) ABCD|E
- LeetCode_28(实现 strStr())
- 2022 t elevator repair new version test questions and t elevator repair simulation test question bank
- Leecode record 1351 negative numbers in statistical ordered matrix
- Neural network convolution layer
- VIM easy to use tutorial
- Difference between cookie and session
- Shell analysis server log command collection
- Shell之分析服务器日志命令集锦
- LeetCode_ 35 (search insertion position)
猜你喜欢

STM32扩展版 按键扫描

2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis

Dede collection plug-in does not need to write rules

Introduction to JVM stack and heap

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

Kodori tree board

Neural network - nonlinear activation

Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and

Dual contractual learning: text classification via label aware data augmentation reading notes

神经网络-最大池化的使用
随机推荐
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
2022 question bank and answers for safety production management personnel of hazardous chemical production units
分布式全局唯一ID解决方案详解
C#读写应用程序配置文件App.exe.config,并在界面上显示
分布式锁的实现
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
JS to solve the problem of floating point multiplication precision loss
【FTP】FTP常用命令,持续更新中……
如何看待智慧城市建设中的改变和机遇?
先有网络模型的使用及修改
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
[pat (basic level) practice] - [simple simulation] 1064 friends
[2020 overview] overview of link prediction based on knowledge map embedding
I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
LeetCode_35(搜索插入位置)
神经网络-最大池化的使用
Some tools that research dogs may need
pytorch中常用数据集的使用方法
Codeworks round 449 (Div. 1) C. Kodori tree template