版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shaoyou223/article/details/84936443
#int型tensor
from torch.autograd import Variable
from torch import IntTensor
var = Variable(IntTensor([[1,0],[0,1]]))
#查看size
var.size() #torch.Size([2, 2])
#将var.size()转换为list
list(var.size()) #[2,2]
#查看元素总个数
z.numel()
#torch 增加维度
x = torch.Tensor(2,4).zero_() #2*4维的全0 tensor
y = x.unsqueeze(0) #y的维度为1,2,4
y = x.unsqueeze(1) #y的维度为 2,1,4
y = x.unsqueeze(2) #y的维度为2,4,1
#view()函数的使用
a = torch.Tensor(2,3)
a.view(1,-1) #变成1*6维
a.view(6,1) #变成6*1维
a.view(3,2) #变成3*2维
#矩阵相乘
tensor = torch.FloatTensor([[1,2],[3,4]])
torch.mm(tensor,tensor) #结果为 tensor([[7.,10.],[15.,22.]])
#点乘
tensor_a.dot(tensor_b) #[1,2,3,4] [1,2,3,5] 输出为34