import torch
from torch.autograd import Variable
tensor = torch.FloatTensor([[1, 2], [3, 4]])
variable = Variable(tensor, requires_grad=True)
v_out = torch.mean(variable * variable)
v_out.backward()
print(variable.grad)
改写为:
import torch
tensor = torch.FloatTensor([[1, 2], [3, 4]])
tensor.requires_grad = True # 这个尤其重要!!!
t_out = torch.mean(tensor * tensor)
t_out.backward()
print(tensor.grad)
只是进行学习记录!!!
转自:(28条消息) PyTorch的Variable已经不需要用了!!!_pytorch variable弃用_龙哥每天都要学的博客-CSDN博客