YDOOK:Pytorch教程:tensor 张量变量 requires_grad=True 设置 运算后因变量保留自变量该属性
© YDOOK Jinwei Lin, shiye.work
Code:
import numpy
import torch
import numpy as np
x = torch.ones([3, 3]) + 2
print('x = ',x)
print('type(x) = ', type(x))
print()
print('x.grad_fn = ', x.grad_fn)
print('x.requires_grad = ', x.requires_grad)
print()
y = x*x
print('y = ', y)
print('type(y) = ', type(y))
print()
print('y.grad_fn = ', y.grad_fn)
print('y.requires_grad = ', y.requires_grad)
print('\n===================================================\n')
x = torch.ones([3, 3], requires_grad=True) + 2
print('x = ',x)
print('type(x) = ', type(x))
print()
print('x.grad_fn = ', x.grad_fn)
print('x.requires_grad = ', x.requires_grad)
print()
y = x*x
print('y = ', y)
print('type(y) = ', type(y))
print()
print('y.grad_fn = ', y.grad_fn)
print('y.requires_grad = ', y.requires_grad)
print('\n===================================================\n')
x = torch.ones([3, 3], requires_grad=True)
y = torch.rand([3, 3], requires_grad=False)
print('x = ', x)
print('type(x) = ', type(x))
print('x.grad_fn = ', x.grad_fn)
print('x.requires_grad = ', x.requires_grad)
print()
print('y = ', y)
print('type(y) = ', type(y))
print('y.grad_fn = ', y.grad_fn)
print('y.requires_grad = ', y.requires_grad)
print()
z = x*y + 1
print('z = ', z)
print('type(z) = ', type(z))
print()
print('z = ', y)
print('type(z) = ', type(y))
print('z.grad_fn = ', z.grad_fn)
print('z.requires_grad = ', z.requires_grad)
print('\n===================================================\n')
Result:
x = tensor([[3., 3., 3.],
[3., 3., 3.],
[3., 3., 3.]])
type(x) = <class 'torch.Tensor'>
x.grad_fn = None
x.requires_grad = False
y = tensor([[9., 9., 9.],
[9., 9., 9.],
[9., 9., 9.]])
type(y) = <class 'torch.Tensor'>
y.grad_fn = None
y.requires_grad = False
===================================================
x = tensor([[3., 3., 3.],
[3., 3., 3.],
[3., 3., 3.]], grad_fn=<AddBackward0>)
type(x) = <class 'torch.Tensor'>
x.grad_fn = <AddBackward0 object at 0x0000014EEDAD7700>
x.requires_grad = True
y = tensor([[9., 9., 9.],
[9., 9., 9.],
[9., 9., 9.]], grad_fn=<MulBackward0>)
type(y) = <class 'torch.Tensor'>
y.grad_fn = <MulBackward0 object at 0x0000014EEDAD7700>
y.requires_grad = True
===================================================
x = tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], requires_grad=True)
type(x) = <class 'torch.Tensor'>
x.grad_fn = None
x.requires_grad = True
y = tensor([[0.9294, 0.8381, 0.4715],
[0.5006, 0.0962, 0.4807],
[0.3705, 0.8838, 0.9043]])
type(y) = <class 'torch.Tensor'>
y.grad_fn = None
y.requires_grad = False
z = tensor([[1.9294, 1.8381, 1.4715],
[1.5006, 1.0962, 1.4807],
[1.3705, 1.8838, 1.9043]], grad_fn=<AddBackward0>)
type(z) = <class 'torch.Tensor'>
z = tensor([[0.9294, 0.8381, 0.4715],
[0.5006, 0.0962, 0.4807],
[0.3705, 0.8838, 0.9043]])
type(z) = <class 'torch.Tensor'>
z.grad_fn = <AddBackward0 object at 0x0000014EEDAD7700>
z.requires_grad = True
===================================================
Process finished with exit code 0