当前位置:网站首页>Hands on deep learning pytorch version exercise solution - 2.5 automatic differentiation
Hands on deep learning pytorch version exercise solution - 2.5 automatic differentiation
2022-07-03 10:20:00 【Innocent^_^】
Here I 5 The function used in the question is written in the previous section of differential , The original book encapsulated it in a bag , See the section of differentiation at the front of the text of this book for the packaging steps . I'm just going to cv down .
Here's the code section :
# Second questions
import torch
x=torch.arange(4.0,requires_grad=True)
print(x)
y=torch.dot(x,x)
print(y)
y.backward(retain_graph=True)
print(" Run back propagation for the first time :",x.grad)
y.backward()
print(" Second run back propagation :",x.grad)
# Third questions
x.grad.zero_()
def g(a):
b = a * 2
while b.norm() < 1000:
b = b * 2
if b.sum() > 0:
c = b
else:
c = 100 * b
return c
a = torch.rand(size=(3,3),requires_grad=True)
d = g(a)
d.sum().backward()
a.grad
x.grad.zero_()
b = torch.rand(4)
b.requires_grad=True
e = g(b)
e.backward()
b.grad
# Fourth question
def practice_control_flow(a):
b = a * a
if b < 0:
return b
else:
if b>=1:
while b < 100:
b = b**2
else:
b = b + 1
return b
x.grad.zero_()
a.grad.zero_()
a = torch.tensor([5.],requires_grad=True)
result=practice_control_flow(a)
result.backward()
print(result,a.grad)
b = torch.tensor([0.1],requires_grad=True)
result2=practice_control_flow(b)
result2.backward()
print(result2,b.grad)
c = torch.tensor([-12.],requires_grad=True)
result3=practice_control_flow(c)
result3.backward()
print(result3,c.grad)
# The fifth question is
%matplotlib inline
import numpy as np
from IPython import display
import torch
from matplotlib import pyplot as plt
# Definition f(x)
def f(x):
return 3 * x ** 2 - 4 * x
# The following drawing function may also be used , Used to set several functions into the same image
def use_svg_display():
# Use svg Format ( Scalable vector , A graphic identification specification , Very HD ) stay Jupyter Draw inside
display.set_matplotlib_formats('svg')
# Set up matplotlib Chart size
def set_figsize(figsize=(3.5,2.5)):
use_svg_display()
plt.rcParams['figure.figsize'] = figsize
# Set by matplotlib⽣ Attributes of the axis of the graph ,legend Used to display image labels
def set_axes(axes,xlabel,ylabel,xlim,ylim,xscale,yscale,legend):
# Set up matplotlib The shaft
axes.set_xlabel(xlabel)
axes.set_ylabel(ylabel)
axes.set_xscale(xscale)
axes.set_yscale(yscale)
axes.set_xlim(xlim)
axes.set_ylim(ylim)
if legend:
axes.legend(legend)
# Displays the grid in the image
axes.grid()
#xscale='linear' You can make x Axis and y The axis scale value is linear ='log' be y Axis scale and x Shaft satisfaction y=lgx. The default is 'linear'
# Defined plot Function to draw multiple curves concisely , Because we need to visualize many curves throughout the book .
def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None, ylim=None,
xscale='linear',yscale='linear',fmts=('-','m--','g-.','r:'),figsize=(3.5,2.5),axes=None):
# Drawing data points
if legend is None:
legend=[]
set_figsize(figsize)
axes = axes if axes else plt.gca()
# If X If there is one axis, the output is true
def has_one_axis(X):
return (hasattr(X,"ndim") and X.ndim==1 or isinstance(X,list) and not hasattr(X[0],'__len__'))
if has_one_axis(X):
X = [X]
if Y is None:
X,Y=[[]]*len(X),X
elif has_one_axis(Y):
Y=[Y]
if len(X)!=len(Y):
X=X*len(Y)
axes.cla()
for x,y,fmt in zip(X,Y,fmts):
if(len(X)):
axes.plot(x,y,fmt)
else:
axes.plot(y,fmt)
set_axes(axes,xlabel,ylabel,xlim,ylim,xscale,yscale,legend)
x=torch.arange(-10.,10.,0.1)
x.requires_grad=True
y=torch.sin(x)
y.sum().backward()
plot(x.detach(),[y.detach(),x.grad,],'x','f(x)',legend=['f(x)','Tangent line=f\'(x)'])

边栏推荐
- Leetcode-100: same tree
- The data read by pandas is saved to the MySQL database
- LeetCode - 705 设计哈希集合(设计)
- openCV+dlib实现给蒙娜丽莎换脸
- Policy Gradient Methods of Deep Reinforcement Learning (Part Two)
- QT setting suspension button
- Discrete-event system
- [LZY learning notes -dive into deep learning] math preparation 2.1-2.4
- 20220609其他:多数元素
- CV learning notes - edge extraction
猜你喜欢

使用密钥对的形式连接阿里云服务器

Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa

YOLO_ V1 summary

LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *

1. Finite Markov Decision Process

Connect Alibaba cloud servers in the form of key pairs

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*

CV learning notes - reasoning and training

Opencv note 21 frequency domain filtering

CV learning notes - edge extraction
随机推荐
20220606 Mathematics: fraction to decimal
LeetCode - 706 设计哈希映射(设计) *
[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
20220531数学:快乐数
Label Semantic Aware Pre-training for Few-shot Text Classification
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
Leetcode-404:左叶子之和
What did I read in order to understand the to do list
Leetcode-404: sum of left leaves
QT self drawing button with bubbles
20220606数学:分数到小数
Simulate mouse click
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow
Leetcode-100: same tree
2-program logic
20220605数学:两数相除
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
Leetcode-112: path sum
Leetcode-513:找树的左下角值