【pytorch】variable 和 tensor

import torch
from torch.autograd import Variable

tensor = torch.FloatTensor([[1,2],[3,4]])
variable = Variable(tensor,requires_grad=True)

print(tensor)
print(variable)

t_out = torch.mean(tensor * tensor)
v_out = torch.mean(variable * variable)

v_out.backward()
print('反向传播之后',variable) #反向传播的时候variable 是受影响的,因为反向传播了

print('变量的data是tensor类型--',variable.data)
print('variable转为numpy--',variable.data.numpy())# 必须借助于tensor转换为numpy

猜你喜欢

转载自blog.csdn.net/acbattle/article/details/80641548