YDOOK:Pytorch教程:tensor 张量变量 requires_grad=True 设置 运算保留特征

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')

# 设置变量 requires_grad=True
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 是由 requires_grad=True 的x变量来的,所以有用自变量的requires_grad=True属性
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')


# 设置变量 requires_grad=True
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()

# 由于 y 是由 requires_grad=True 的x变量来的,所以有用自变量的requires_grad=True属性
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值