当前位置:网站首页>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
边栏推荐
- Golang string (string) and byte array ([]byte) are converted to each other
- 204. Count prime
- The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
- 4. Load balancing and dynamic static separation
- A. Odd Selection【BruteForce】
- The second largest gay dating website in the world was exposed, and the status of programmers in 2022
- Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
- Redis cache avalanche, penetration, breakdown
- 2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report
- Design limitations of structure type (struct)
猜你喜欢
Codeforces Round #803 (Div. 2) C. 3SUM Closure
win32:堆破坏的dump文件分析
PHP MySQL preprocessing statement
Redis core technology and practice - learning notes (11): why not just string
Valentine's day, send you a little red flower~
Naoqi robot summary 27
Mysql45 lecture learning notes (II)
MySQL duplicate check
Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
模块九作业
随机推荐
Count the number of pixel values in the image
图像24位深度转8位深度
Fedora 21 installs lamp host server
Usage of laravel conditional array in
Image 24 bit depth to 8 bit depth
How to quickly view the inheritance methods of existing models in torchvision?
[combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
解决Zabbix用snmp监控网络流量不准的问题
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation example 2 | extended to integer solution)
模块九作业
Self executing function
OpenSSL的SSL/BIO_get_fd
Three gradient descent methods and code implementation
4. Load balancing and dynamic static separation
Unsafe类的使用
How to analyze the rising and falling rules of London gold trend chart
How to expand the capacity of golang slice slice
Computer graduation design PHP campus address book telephone number inquiry system
Win32: dump file analysis of heap corruption