当前位置:网站首页>torch.autograd.grad finds the second derivative
torch.autograd.grad finds the second derivative
2022-08-05 00:44:00 【Ghost Road 2022】
1 用法介绍
pytorch中torch.autograd.gradThe function is mainly used to calculate and return the sum of the gradients of the output with respect to the input,The specific parameters are as follows:
torch.tril(input, diagonal=0, *, out=None) * \longrightarrow *Tensor
- outputs(sequence of Tensor):represents the output of the differential function
- inputs (sequence of Tensor):Represents the input to the differential function
- grad_outputs (sequence of Tensor):表示“向量-雅克比矩阵”的向量
- retain_graph (bool, optional):Indicates whether the computation graph needs to be released,It needs to be set to when calculating the second derivativeTrue
- create_graph (bool, optional):Indicates whether the gradient needs to be added to the computational graph,It will be set as required when computing higher-order derivatives or other calculationsTrue
- allow_unused (bool, optional):Indicates whether to return only the gradient of the input,Instead of returning the gradients of other leaf nodes
2 实例讲解
Mathematical examples of specific second-order derivatives analytical solutions are given below
给定一个向量 x = ( x 1 , x 2 ) ⊤ {\bf{x}}=(x_1,x_2)^{\top} x=(x1,x2)⊤,可以得到向量 y = ( y 1 , y 2 ) ⊤ = ( x 1 2 , x 2 2 ) ⊤ {\bf{y}}=(y_1,y_2)^{\top}=(x^2_1,x^2_2)^{\top} y=(y1,y2)⊤=(x12,x22)⊤.对向量 y {\bf{y}} yThe loss function can be obtained by averaging the elements of l o s s 1 \mathrm{loss}_1 loss1为: l o s s 1 ( x ) = m e a n ( y ) = x 1 2 + x 2 2 2 \mathrm{loss}_1({\bf{x}})=\mathrm{mean}({\bf{y}})=\frac{x_1^2+x^2_2}{2} loss1(x)=mean(y)=2x12+x22向量 y {\bf{y}} yThe components of the elements are x {\bf{x}} x求偏导,Then add and average to get the loss function l o s s 2 \mathrm{loss}_2 loss2为 { h 1 ( x ) = ∂ y 1 ∂ x = ( 2 x 1 , 0 ) ⊤ h 2 ( x ) = ∂ y 2 ∂ x = ( 0 , 2 x 2 ) ⊤ , l o s s 2 ( x ) = m e a n ( h 1 ( x 1 ) − h 2 ( x 2 ) ) = x 1 − x 2 \left\{\begin{aligned}h_1({\bf{x}})&=\frac{\partial y_1}{\partial {\bf{x}}}=(2x_1,0)^{\top}\\h_2({\bf{x}})&=\frac{\partial y_2}{\partial {\bf{x}}}=(0,2x_2)^{\top}\end{aligned}\right.,\quad \mathrm{loss}_2({\bf{x}})=\mathrm{mean}(h_1({\bf{x}}_1)-h_2({\bf{x}}_2))=x_1-x_2 ⎩⎨⎧h1(x)h2(x)=∂x∂y1=(2x1,0)⊤=∂x∂y2=(0,2x2)⊤,loss2(x)=mean(h1(x1)−h2(x2))=x1−x2将损失函数 l o s s 1 \mathrm{loss}_1 loss1与损失函数 l o s s 2 \mathrm{loss}_2 loss2相加可以得到 l o s s ( x ) = l o s s 1 ( x ) + l o s s 2 ( x ) = x 1 2 + x 2 2 2 + x 1 − x 2 \mathrm{loss}({\bf{x}})=\mathrm{loss}_1({\bf{x}})+\mathrm{loss}_2({\bf{x}})=\frac{x_1^2+x_2^2}{2}+x_1-x_2 loss(x)=loss1(x)+loss2(x)=2x12+x22+x1−x2最终损失函数 l o s s \mathrm{loss} loss对向量 x {\bf{x}} x的偏导数为 ∂ l o s s ∂ x = ( x 1 + 1 , x 2 − 1 ) ⊤ \frac{\partial {\mathrm{loss}}}{\partial{ {\bf{x}}}}=(x_1+1,x_2-1)^{\top} ∂x∂loss=(x1+1,x2−1)⊤
以下为用pytorchImplement the code example corresponding to the second derivative:
import torch
x = torch.tensor([5.0, 7.0], requires_grad=True)
y = x**2
loss1 = torch.mean(y)
h1 = torch.autograd.grad(y[0], x, retain_graph = True, create_graph=True)
h2 = torch.autograd.grad(y[1], x, retain_graph = True, create_graph=True)
loss2 = torch.mean(h1[0] - h2[0])
loss = loss1 + loss2
result = torch.autograd.grad(loss, x)
print(result)
当向量 x {\bf{x}} x取值为 ( 5 , 7 ) ⊤ (5,7)^{\top} (5,7)⊤时,The second derivative obtained from the mathematical analytical solution is ( 6 , 6 ) ⊤ (6,6)^{\top} (6,6)⊤,The experimental results of the corresponding code run are also ( 6 , 6 ) (6,6) (6,6).
边栏推荐
猜你喜欢

Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!

《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术

oracle create tablespace

TinyMCE disable escape

电赛必备技能___定时ADC+DMA+串口通信

redis可视化管理软件Redis Desktop Manager2022

"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology

matlab中rcosdesign函数升余弦滚降成型滤波器

MongoDB搭建及基础操作

Kubernetes 网络入门
随机推荐
Kubernetes 网络入门
gorm joint table query - actual combat
Helm Chart
Software Testing Interview Questions: What do you think about software process improvement? Is there something that needs improvement in the enterprise you have worked for? What do you expect the idea
2022 Nioke Multi-School Training Session H Question H Take the Elevator
SV 类的虚方法 多态
Software testing interview questions: test life cycle, the test process is divided into several stages, and the meaning of each stage and the method used?
Will domestic websites use Hong Kong servers be blocked?
GCC:屏蔽动态库之间的依赖
机器学习(公式推导与代码实现)--sklearn机器学习库
Pytorch usage and tricks
Software testing interview questions: Have you used some tools for software defect (Bug) management in your past software testing work? If so, please describe the process of software defect (Bug) trac
软件测试面试题:做好测试计划的关键是什么?
【idea】idea配置sql格式化
软件测试面试题:BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
leetcode:266. 回文全排列
5.PCIe官方示例
tiup status
Software testing interview questions: What is the difference between load testing, capacity testing, and strength testing?
Software testing interview questions: What are the three modules of LoadRunner?