张量类(Tensor)是深度学习框架必须提供的基础数据结构,神经网络的前向传播和反向传播都是基于 Tensor 类进行的。Tensor 类应具有以下多方面的功能:
数据类型 | CPU | GPU |
---|---|---|
32位浮点数 | torch.FloatTensor | torch.cuda.FloatTensor |
64位浮点数 | torch.DoubleTensor | torch.cuda.DoubleTensor |
8位有符号整型 | torch.CharTensor | torch.cuda.CharTensor |
8位无符号整型 | torch.ByteTensor | torch.cuda.ByteTensor |
布尔类型 | torch.BoolTensor | torch.cuda.BoolTensor |
>>> import torch
>>>
>>> x1=[1,2,3]
>>> x1_tensor=torch.tensor(x1,dtype=torch.int32)
>>> print(x1_tensor)
tensor([1, 2, 3], dtype=torch.int32)
>>>
>>> x1_tensor2=torch.tensor([4,5,6],dtype=torch.int32)
>>> print(x1_tensor2)
tensor([4, 5, 6], dtype=torch.int32)
>>>
>>>
>>> x2 = torch.Tensor([7,8,9])
>>> print(x2,x2.dtype)
tensor([7., 8., 9.]) torch.float32 //默认为torch.float32类型
>>>
>>> import numpy as np
>>> x2_numpy=np.array([1,2,3])
>>> x2.tensor=torch.from_numpy(x2_numpy)
>>> x2_tensor=torch.from_numpy(x2_numpy)
>>> print(x2_tensor)
tensor([1, 2, 3], dtype=torch.int32)
>>>
接着上面的,直接复制Tensor,但是新的Tensor与旧Tensor只是形状相同,数值却是使用特定的数值进行填充的。
>>> x3_tensor=torch.ones_like(x2_tensor)
>>> print(x3_tensor)
tensor([1, 1, 1], dtype=torch.int32)
>>> size=[1,3]
>>> x4_tensor=torch.randn(size)
>>> x5_tensor=torch.zeros(size)
>>> print(x4_tensor)
tensor([[-0.0643, 0.8212, 0.1565]])
>>> print(x5_tensor)
tensor([[0., 0., 0.]])
>>>
>>> x=torch.tensor([1,2,3],dtype=torch.int32)
>>> print(x.dtype)
torch.int32
>>> x=x.half()
>>> print(x.dtype)
torch.float16
>>> x=x.float()
>>> print(x.dtype)
torch.float32
>>> x=x.double()
>>> print(x.dtype)
torch.float64
>>>
>>> x= torch.randn(1,3,4,4)
>>> print("ndimension:",x.ndimension()) //张量的维度
ndimension: 4
>>> print("nelement:",x.nelement()) //张量包含的元素总数
nelement: 48
>>> print("size",x.size()) //张量的尺寸信息
size torch.Size([1, 3, 4, 4])
>>> print("shape:",x.shape) //
shape: torch.Size([1, 3, 4, 4])
>>>
>>> x_view=x.view(1,3,4*4)
>>> print("x_view:",x_view.size())
x_view: torch.Size([1, 3, 16])
>>>
>>> x_view=x.view(1,-1)
>>> print("x_view:",x_view.size()) //view的一个参数是-1,pytorch会根据其他维度自动计算这一维度的大小
x_view: torch.Size([1, 48])
>>>
//====transpose方法,将2行3列的张量变换成3行2列的张量
>>> x= torch.randn(2,3)
>>> print(x)
tensor([[-0.7116, 1.0912, -1.3217],
[ 0.1371, -0.6021, -0.9689]])
>>> x_trans=x.transpose(1,0)
>>> print(x_trans)
tensor([[-0.7116, 0.1371],
[ 1.0912, -0.6021],
[-1.3217, -0.9689]])
>>>
//=======squeeze删除1维度,此时张量的数据内容不变
>>> x=torch.randn(1,3,16,16)
>>> x=x.squeeze(0)
>>> print(x.size())
torch.Size([3, 16, 16])
//=======squeeze增加1维度,此时张量的数据内容不变
>>> x=x.unsqueeze(0)
>>> print(x.size())
torch.Size([1, 3, 16, 16])
>>>
>>> x= torch.tensor([1,2])
>>> y=torch.tensor([1,2])
>>> z=y.clone() //地址不同
>>> q=y.detach() //地址相同
//=====通过clone 方法获得的张量与原始张量的内存地址是不同的,通过detach方法获得的张量与原始张量的内存地址是相同的。
>>> print(x.data_ptr())
5447746589376
>>> print(y.data_ptr())
5447746589312
>>> print(z.data_ptr())
5447746589632
>>> print(q.data_ptr())
5447746589312
>>> x=torch.tensor([1,2,3,4,5])
>>> print(x[1:3]) //获取1~3数值
tensor([2, 3])
>>> print(x[:]) //获取全部数值
tensor([1, 2, 3, 4, 5])
>>> print(x[-1]) //获取最后一个数值
tensor(5)
>>> x1=torch.tensor([1,2])
>>> x2=torch.tensor([3,4])
>>> x3=torch.tensor([5,6])
>>>
>>> x_cat=torch.cat(tensors=(x1,x2,x3),dim=0)
>>> print(x_cat)
tensor([1, 2, 3, 4, 5, 6])
>>> x1=torch.tensor([[1,2],[3,4]])
>>> x2=torch.tensor([[1,2],[3,4]])
>>> print(torch.add(x1,x2))
tensor([[2, 4],
[6, 8]])
>>> print(torch.sub(x1,x2))
tensor([[0, 0],
[0, 0]])
>>>
>>> x1=torch.tensor([[1,2],[3,4]])
>>> x2=torch.tensor([[1,2],[3,4]])
>>> print(x1.add_(x2))
tensor([[2, 4],
[6, 8]])
>>> print(x1.sub_(x2))
tensor([[0, 0],
[0, 0]])
>>> x1=torch.tensor([[1,2],[3,4]])
>>> x2=torch.tensor([[1,2],[3,4]])
>>> print(x1+x2+x1+x2)
tensor([[ 4, 8],
[12, 16]])
>>> x1=torch.tensor([[1,2],[3,4]])
>>> x2=torch.tensor([[1,2],[3,4]])
>>> print(torch.mul(x1,x2))
tensor([[ 1, 4],
[ 9, 16]])
>>> print(torch.mm(x1,x2))
tensor([[ 7, 10],
[15, 22]])