当前位置:网站首页>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)'])
边栏推荐
- QT creator uses OpenCV Pro add
- When the reference is assigned to auto
- On the problem of reference assignment to reference
- [C question set] of Ⅵ
- 20220610其他:任务调度器
- CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
- QT detection card reader analog keyboard input
- LeetCode - 900. RLE 迭代器
- 20220604 Mathematics: square root of X
- Neural Network Fundamentals (1)
猜你喜欢
RESNET code details
Cases of OpenCV image enhancement
openCV+dlib實現給蒙娜麗莎換臉
[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
Vgg16 migration learning source code
CV learning notes - deep learning
Label Semantic Aware Pre-training for Few-shot Text Classification
CV learning notes convolutional neural network
Configure opencv in QT Creator
Leetcode - the k-th element in 703 data flow (design priority queue)
随机推荐
CV learning notes alexnet
Leetcode-404:左叶子之和
[LZY learning notes dive into deep learning] 3.1-3.3 principle and implementation of linear regression
使用密钥对的形式连接阿里云服务器
CV learning notes - feature extraction
LeetCode - 673. 最长递增子序列的个数
YOLO_ V1 summary
LeetCode - 715. Range module (TreeSet)*****
Leetcode-513: find the lower left corner value of the tree
2-program logic
Deep Reinforcement learning with PyTorch
Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa
Leetcode-112:路径总和
QT self drawing button with bubbles
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
Leetcode-112: path sum
2021-11-11 standard thread library
20220531 Mathematics: Happy numbers
Opencv notes 20 PCA
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)