The variable function pytorch

torch.autograd.Variable is Autograd core classes, which encapsulates the Tensor, implement and integrate the relevant back-propagation (in order to reverse the spread of seeking after tensor becomes variable gradient? using variable .backward () to reverse the spread after , var.grad stored in the gradient var)

x = Variable(tensor, requires_grad = True)

Varibale contains three attributes:

  • data: storing Tensor, it is the data body
  • grad: Save the gradient data, the ability is not the Tensor Variable, consistent with the shape data
  • grad_fn: Function object point, for calculating a gradient of a backpropagation

usage:

  1. import torch
  2. from torch.autograd import Variable
  3. x = Variable(torch.one( 2,2), requires_grad = True)
  4. Print (the X-) # In fact, the query is x.data, a tenso

Guess you like

Origin www.cnblogs.com/lyp1010/p/12146519.html