当前位置:网站首页>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
边栏推荐
- ES7 - Optimization of promise
- [combinatorics] generating function (example of using generating function to solve the number of solutions of indefinite equation)
- 毕业总结
- Prototype inheritance..
- A. Berland Poker & 1000 [simple mathematical thinking]
- [combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
- Use of unsafe class
- [combinatorics] exponential generating function (concept of exponential generating function | permutation number exponential generating function = combinatorial number ordinary generating function | e
- [LINUX]CentOS 7 安装MYSQL时报错“No package mysql-server available“No package zabbix-server-mysql availabl
- Change the single node of Postgres database into master-slave
猜你喜欢
[combinatorics] generating function (summation property)
Golang string (string) and byte array ([]byte) are converted to each other
An academic paper sharing and approval system based on PHP for computer graduation design
Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
BFS - topology sort
Valentine's day, send you a little red flower~
The vscode code is automatically modified to a compliance code when it is formatted and saved
Redis core technology and practice - learning notes (11): why not just string
Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing
(9) Opencv Canny edge detection
随机推荐
CTO and programmer were both sentenced for losing control of the crawler
[combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
2022-2028 global marking ink industry research and trend analysis report
Line by line explanation of yolox source code of anchor free series network (6) -- mixup data enhancement
What is SQL get connection
Three gradient descent methods and code implementation
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
Graduation summary
Self executing function
2022-2028 global physiotherapy clinic industry research and trend analysis report
Prototype inheritance..
Closure and closure function
Module 9 operation
PHP MySQL preprocessing statement
AcWing 271. Teacher Yang's photographic arrangement [multidimensional DP]
How do microservices aggregate API documents? This wave of operation is too good
Mysql45 lecture learning notes (II)
As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
Computer graduation project PHP library book borrowing management system
What kind of experience is it when the Institute earns 20000 yuan a month?