PyTorch基本用法(二)——Variable

客:noahsnail.com  |  CSDN  | 

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

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]
# 计算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]
# 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]
# *表示逐元素点乘,不是矩阵乘法
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]
# 输出variable中的data, data是tensor
print variable.data
 1  2
 3  4
[torch.FloatTensor of size 2x2]

你可能感兴趣的:(PyTorch基本用法(二)——Variable)