栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

✨Pytorch 基础 2 -- Autograd

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

✨Pytorch 基础 2 -- Autograd

目录

参考网站

requires_grad

gradient

Prevent tracking gradients

Attention!


参考网站

https://towardsdatascience.com/pytorch-autograd-understanding-the-heart-of-pytorchs-magic-2686cd94ec95https://towardsdatascience.com/pytorch-autograd-understanding-the-heart-of-pytorchs-magic-2686cd94ec95

requires_grad
# set requires_grad = True will allow future calculation of the gradient dy/dx
x = torch.ones(5, requires_grad = True)

gradient
 # when we do any opertion, it will track the movement, calculate and store grad_fn for future backpropagation

y = x + 2


# case 1: z is a scalar
z = y*y*2
z = z.mean() 
z.backward() # this step will calculate gradient dz/dx, now x will have an attribute: grad

# case 2: z is not a scalar  
z = y*y*2
v = torch.tensor([0.1, 1, 0.001], dtype = torch.float32)
z.backward(v)

print(x.grad) 

Prevent tracking gradients
# Three Options: 

## First One 
x.requires_grad_(False)

## Second one 
y = x.detach()

## Third One 
with torch.no_grad():
	y = x +2 

Attention!
weights = torch.ones(4, requires_grad = True)

for epoch in range(3):
	#first iteration
	model_ouput = (weights *3).sum()
	model_output.backward()
	
	# now we can get weights.grad
	print(weights.grad)          # give us [3, 3, 3, 3]
	
	# In the second iteration, weigts.grad will accumulate 
	print(weights.grad)          # give us [6, 6, 6, 6]
	
# Third iteration
	print(weights.grad)          # give us [9, 9, 9, 9]

# What the correct things should be like? 
	# In each epoch, we calculate grad, update weights and calculate new grad without accumulating previous gradient values. Therefore before running into the next iteration, we should set weights.grad to zero
	weight.grad.zero_()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/341850.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号