PyTorch基本用法(二)——Variable

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

本文主要是PyTorch中Variable变量的一些用法。

1
2
3
4
5
6
7
8
9
10
import torch
from torch.autograd import Variable
tensor = torch.FloatTensor([[1, 2], [3, 4]])
# 定义Variable, requires_grad用来指定是否需要计算梯度
variable = Variable(tensor, requires_grad = True)
print tensor
print variable
 1  2
 3  4
[torch.FloatTensor of size 2x2]

Variable containing:
 1  2
 3  4
[torch.FloatTensor of size 2x2]
1
2
3
4
5
# 计算x^2的均值
tensor_mean = torch.mean(tensor * tensor)
variable_mean = torch.mean(variable * variable)
print tensor_mean
print variable_mean
7.5
Variable containing:
 7.5000
[torch.FloatTensor of size 1]
1
2
3
4
5
6
7
8
# variable进行反向传播
# 梯度计算如下:
# variable_mean = 1/4 * sum(variable * variable)
# d(variable_mean)/d(variable) = 1/4 * 2 * variable = 1/2 * variable
variable_mean.backward()
# 输出variable中的梯度
print variable.grad
Variable containing:
 0.5000  1.0000
 1.5000  2.0000
[torch.FloatTensor of size 2x2]
1
2
3
# *表示逐元素点乘,不是矩阵乘法
print tensor * tensor
print variable * variable
  1   4
  9  16
[torch.FloatTensor of size 2x2]

Variable containing:
  1   4
  9  16
[torch.FloatTensor of size 2x2]
1
2
# 输出variable中的data, data是tensor
print variable.data
 1  2
 3  4
[torch.FloatTensor of size 2x2]
坚持技术分享,如果觉得有收获就打赏吧!