Issue
I am learning PyTorch for an image classification task, and I ran into code where someone used a PyTorch Variable()
in their function for prediction:
def predict_image(image):
image_tensor = test_transforms(image).float()
image_tensor = image_tensor.unsqueeze_(0)
input = Variable(image_tensor)
input = input.to(device)
output = model(input)
index = output.data.cpu().numpy().argmax()
return index
Why do they use Variable()
here? (even though it works fine without it.)
Solution
You can safely omit it. Variables are a legacy component of PyTorch, now deprecated, that used to be required for autograd:
Variable
(deprecated)WARNING
The
Variable
API has been deprecated: Variables are no longer necessary to use autograd with tensors. Autograd automatically supports Tensors withrequires_grad
set toTrue
. Below please find a quick guide on what has changed:
Variable(tensor)
andVariable(tensor, requires_grad)
still work as expected, but they return Tensors instead of Variables.
Answered By - iacob
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.