import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
# autograd
# fn1:torch.autograd.backward()自动求取梯度
# 参数:tensors:用于求导的tensor;retain_graph:保存计算图;create_graph:创建导数计算图,用于高阶求导;grad_tensors:多梯度权重
# fn2:torch.autograd.grad()自动求取梯度
# 参数:outputs:用于求导的tensor;input:需要求grad的tensor;create_graph:创建导数计算图,用于高阶求导;retain_graph:保存计算图;grad_outputs:多提取权重
# 案例一:backward()应用
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.]) # 设置权重
loss.backward(gradient=grad_tensors)
# y0.backward(retain_graph=True) # 保留计算图
print(w.grad)
# 案例二:grad()应用
x = torch.tensor([3.], requires_grad=True)
y = torch.pow(x, 2)
grad_1 = torch.autograd.grad(y, x, create_graph=True) # y对x求梯度,6
print(grad_1)
grad_2 = torch.autograd.grad(grad_1[0], x) # y对x的梯度求梯度,2
print(grad_2)
# autograd使用注意事项
# 1、grad不会自动释放
# 2、requires_grad默认True
# 3、leaf不可以执行in-place
# 案例三:grad不释放后果
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默认True
print(b.requires_grad)
# w.grad.zero_() # grad清零,name_表示in-place(原位)操作
# in-place操作
# 常规操作会开辟新的地址
a = torch.ones((1,))
print(id(a), a)
a = a + torch.ones((1,))
print(id(a), a)
# in-place操作不会开辟新的地址
a += torch.ones((1,))
print(id(a), a)
# leaf结点不能执行in-place操作
torch学习笔记(5)——autograd
最新推荐文章于 2023-03-12 19:35:24 发布