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
【pytorch】variable 和 tensor
最新推荐文章于 2024-08-19 22:28:10 发布