pytorch.autograd使用示例

2021-01-14 11:06:32 浏览数 (1)

pytorch.autograd使用示例

代码语言:javascript复制
# -*- coding:utf-8 -*-
# /usr/bin/python
'''
-------------------------------------------------
   File Name   : autograd_demo
   Description :  
   Envs        :  
   Author      :  yanerrol
   Date        : 2019/12/22  13:08
-------------------------------------------------
   Change Activity:
                  2019/12/22  13:08:
-------------------------------------------------
'''
__author__ = 'yanerrol'

import  torch
from    torch import autograd


x = torch.tensor(1.)
a = torch.tensor(1., requires_grad=True)
b = torch.tensor(2., requires_grad=True)
c = torch.tensor(3., requires_grad=True)

y = a**2 * x   b * x   c

print('before:', a.grad, b.grad, c.grad)
grads = autograd.grad(y, [a, b, c])
print('after :', grads[0], grads[1], grads[2])

0 人点赞