当前位置:网站首页>Torch learning notes (5) -- autograd
Torch learning notes (5) -- autograd
2022-07-03 18:22:00 【ZRX_ GIS】
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
# autograd
# fn1:torch.autograd.backward() Automatically calculate the gradient
# Parameters :tensors: For derivation tensor;retain_graph: Save calculation diagram ;create_graph: Create derivative calculation diagram , For higher-order derivation ;grad_tensors: Multi gradient weight
# fn2:torch.autograd.grad() Automatically calculate the gradient
# Parameters :outputs: For derivation tensor;input: Requirements grad Of tensor;create_graph: Create derivative calculation diagram , For higher-order derivation ;retain_graph: Save calculation diagram ;grad_outputs: Extract more weights
# Case a :backward() application
w = torch.tensor([1.], requires_grad=True)
x = torch.tensor([2.], requires_grad=True)
a = torch.add(w, x)
b = torch.add(w, 1)
y0 = torch.mul(a, b)
y1 = torch.add(a, b)
loss = torch.cat([y0, y1], dim=0)
grad_tensors = torch.tensor([1., 1.]) # Set weight
loss.backward(gradient=grad_tensors)
# y0.backward(retain_graph=True) # Keep the calculation chart
print(w.grad)
# Case 2 :grad() application
x = torch.tensor([3.], requires_grad=True)
y = torch.pow(x, 2)
grad_1 = torch.autograd.grad(y, x, create_graph=True) # y Yes x Find gradient ,6
print(grad_1)
grad_2 = torch.autograd.grad(grad_1[0], x) # y Yes x Find the gradient of ,2
print(grad_2)
# autograd Precautions for use
# 1、grad It doesn't automatically release
# 2、requires_grad Default True
# 3、leaf Can't execute in-place
# Case three :grad Consequences of non release
w = torch.tensor([1.], requires_grad=True)
x = torch.tensor([2.], requires_grad=True)
for i in range(4):
a = torch.add(w, x)
b = torch.add(w, 1)
y = torch.mul(a, b)
y.backward()
# print(w.grad)# requires_grad Default True
print(b.requires_grad)
# w.grad.zero_() # grad Zero clearing ,name_ Express in-place( In situ ) operation
# in-place operation
# Routine operations will open up new addresses
a = torch.ones((1,))
print(id(a), a)
a = a + torch.ones((1,))
print(id(a), a)
# in-place Operation will not open up new addresses
a += torch.ones((1,))
print(id(a), a)
# leaf Nodes cannot execute in-place operation
边栏推荐
- English语法_名词 - 分类
- English语法_形容词/副词3级 - 倍数表达
- supervisor监控Gearman任务
- [教程]在 CoreOS 上构建你的第一个应用
- Self executing function
- [combinatorics] exponential generating function (example of exponential generating function solving multiple set arrangement)
- [combinatorics] exponential generating function (concept of exponential generating function | permutation number exponential generating function = combinatorial number ordinary generating function | e
- Recent learning experience
- How to quickly view the inheritance methods of existing models in torchvision?
- Supervisor monitors gearman tasks
猜你喜欢

How to draw non overlapping bubble chart in MATLAB

How do microservices aggregate API documents? This wave of operation is too good

Mysql45 lecture learning notes (II)
![Golang string (string) and byte array ([]byte) are converted to each other](/img/41/20f445ef9de4adf2a2aa97828cb67f.jpg)
Golang string (string) and byte array ([]byte) are converted to each other

Sensor debugging process

PHP MySQL preprocessing statement

2022-2028 global marking ink industry research and trend analysis report

How many convolution methods does deep learning have? (including drawings)

Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)

Computer graduation project PHP library book borrowing management system
随机推荐
Win32: dump file analysis of heap corruption
[tutorial] build your first application on coreos
[Yu Yue education] family education SPOC class 2 reference materials of Shanghai Normal University
What London Silver Trading software supports multiple languages
Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding
Gear2021 monthly update - December
[combinatorics] generating function (property summary | important generating function)*
Distributed task distribution framework gearman
Grammaire anglaise Nom - Classification
Computer graduation design PHP sports goods online sales system website
Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
[combinatorics] generating function (shift property)
Sensor 调试流程
Theoretical description of linear equations and summary of methods for solving linear equations by eigen
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
Recent learning experience
Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing
[combinatorics] generating function (commutative property | derivative property | integral property)
Setinterval CPU intensive- Is setInterval CPU intensive?
2022-2028 global lithium battery copper foil industry research and trend analysis report