KansaiRobot
KansaiRobot

Reputation: 9912

What does "Variable" means in python? Is it a standard function?

I have some python code that I have to read and understand.

In one line I find

 img = Variable(torch.from_numpy(img.transpose(2, 0, 1)[np.newaxis,:,:,:]).cuda().float(), volatile=True)

what is this Variable I am seeing? When I use the IDE to find the definition it says 'No definition found for Variable' which makes me suspect it is a standard function in python. I Obviously can not google "Variable" for python because I will get countless definitions of what a variable is in python.

Has anyone seen a line like this before? Where Variable is being used as a function? t

Upvotes: 0

Views: 95

Answers (1)

tourist
tourist

Reputation: 4333

Variable is not an inbuilt class. It is under module torch.autograd

A Variable wraps a Tensor. It supports nearly all the API’s defined by a Tensor. Variable also provides a backward method to perform backpropagation. For example, to backpropagate a loss function to train model parameter x, we use a variable loss to store the value computed by a loss function. Then, we call loss.backward which computes the gradients ∂loss/∂x for all trainable parameters. PyTorch will store the gradient results back in the corresponding variable x.

Source

Upvotes: 3

Related Questions