当前位置:网站首页>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
边栏推荐
- 189. Rotation array
- supervisor监控Gearman任务
- Golang string (string) and byte array ([]byte) are converted to each other
- Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
- Fedora 21 installs lamp host server
- 毕业总结
- [combinatorics] exponential generating function (properties of exponential generating function | exponential generating function solving multiple set arrangement)
- Recent learning experience
- Self executing function
- 4. Load balancing and dynamic static separation
猜你喜欢

BFS - topology sort

PHP MySQL inserts multiple pieces of data

Mysql45 lecture learning notes (II)

The vscode code is automatically modified to a compliance code when it is formatted and saved
![[combinatorics] generating function (summation property)](/img/74/e6ef8ee69ed07d62df9f213c015f2c.jpg)
[combinatorics] generating function (summation property)

CTO and programmer were both sentenced for losing control of the crawler

Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing

模块九作业

2022-2028 global solid phase extraction column industry research and trend analysis report

Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
随机推荐
How to expand the capacity of golang slice slice
Self executing function
Usage of laravel conditional array in
[combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
[untitled]
This diversion
Sepconv (separable revolution) code recurrence
Line by line explanation of yolox source code of anchor free series network (6) -- mixup data enhancement
Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding
An academic paper sharing and approval system based on PHP for computer graduation design
The second largest gay dating website in the world was exposed, and the status of programmers in 2022
What problems can cross-border e-commerce sellers solve with multi platform ERP management system
PHP MySQL create database
Redis cache avalanche, penetration, breakdown
Naoqi robot summary 27
Image 24 bit depth to 8 bit depth
2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report
Redis core technology and practice - learning notes (11): why not just string
A. Odd Selection【BruteForce】
supervisor监控Gearman任务