【Pytorch】张量1

在torch中张量的创建

从列表中创建张量

>>> a = [1,2,3.]
>>> type(a)
<class 'list'>
>>> b = torch.tensor(a)
>>> b
tensor([1., 2., 3.])
>>> type(b)
<class 'torch.Tensor'>
>>> b.dtype
torch.float32

numpy中创建张量

>>> import numpy as np
>>> import torch
>>> np.random.normal(2)
2.1885829330398607
>>> np.random.normal((2,3))
array([3.1159779 , 3.46278534])
>>> a=np.random.normal((2,3))
>>> torch.tensor(a)
tensor([3.0776, 3.3629], dtype=torch.float64)
>>> b = torch.tensor(a)
>>> b
tensor([3.0776, 3.3629], dtype=torch.float64)
>>> c = torch.ones_like(b)# 产生和张量b大小一样的全是1的张量
>>> c
tensor([1., 1.], dtype=torch.float64)
>>> c = torch.zeros_like(b)#全是0
>>> c
tensor([0., 0.], dtype=torch.float64)
>>> c = torch.rand_like(b)#随机数
>>> c
tensor([0.8036, 0.6563], dtype=torch.float64)
>>> torch.rand((2,2))
tensor([[0.1414, 0.3805],
        [0.1663, 0.6417]])
>>> torch.rand([2,2])
tensor([[0.1943, 0.0680],
        [0.3675, 0.4552]])
>>> torch.rand((2,2,))
tensor([[0.8623, 0.1806],
        [0.8660, 0.9106]])
>>> torch.rand((2,2,)).dtype
torch.float32

torch属性

>>> a = torch.rand([2,2,])
>>> a
tensor([[0.6406, 0.7986],
        [0.8093, 0.2699]])
>>> a.dtype
torch.float32
>>> a.shape
torch.Size([2, 2])
>>> a.device
device(type='cpu')

Tensors的操作

# 移动到gpu上运行
if torch.cuda.is_available():
	tensor = tensor.to('cuda')
  • 100中张量的操作
>>> a
tensor([[0.6406, 0.7986],
        [0.8093, 0.2699]])
>>> torch.is_tensor(a)
True
>>> torch.is_complex(a)
False
>>> torch.is_floating_point(a)
True
# 非零的标量张量
>>> a = torch.tensor(1.0)
>>> torch.is_nonzero(a)
True
>>> a = torch.tensor(0)
>>> torch.is_nonzero(a)
False
# 返回张量中所有元素数目
>>> a = torch.rand([2,2,])
>>> a
tensor([[0.9897, 0.1273],
        [0.2993, 0.4886]])
>>> torch.numel(a)
4
  • 返回全0
    【Pytorch】张量1
>>> torch.zeros([5,5])
tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])

  • 设置默认数据类型
    【Pytorch】张量1
>>> torch.zeros([5,5],dtype=torch.int32)
tensor([[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]], dtype=torch.int32)
>>> torch.zeros([5,5]).dtype
torch.float32# 默认是float32类型
  • 全1
>>> a = torch.zeros([5,5],dtype=torch.int32)
>>> b = torch.ones_like(a)
>>> b
tensor([[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]], dtype=torch.int32)
  • arange
>>> torch.arange(5)
tensor([0, 1, 2, 3, 4])

【Pytorch】张量1
默认开始为0
【Pytorch】张量1

>>> torch.arange(0,5,2)
tensor([0, 2, 4])
  • range(现在已经不再使用了)
    【Pytorch】张量1
    比arange长一个单位
>>> for i in torch.arange(10):
...    print("epoch:",i)
>>>    
epoch: tensor(0)
epoch: tensor(1)
epoch: tensor(2)
epoch: tensor(3)
epoch: tensor(4)
epoch: tensor(5)
epoch: tensor(6)
epoch: tensor(7)
epoch: tensor(8)
epoch: tensor(9)
  • eye:创建一个2D的张量,对角线上全为1,其他为0
>>> torch.eye(3)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
  • full:创建一个大小为Size的张量,用full_value填充
>>> torch.full([2,2],5)
tensor([[5, 5],
        [5, 5]])
  • full_like:用已有tensor构建新的tensor,size和数据类型都和原来相同

索引、切片、聚合、旋转

  • cat:相当于concat
    【Pytorch】张量1
    保证除了concat的那个维度以外,其他维度都是一样的
>>> a = torch.rand([2,2])
>>> b = torch.rand([2,3])
>>> a
tensor([[0.6999, 0.9184],
        [0.2796, 0.1800]])
>>> b
tensor([[0.7243, 0.3705, 0.4654],
        [0.8309, 0.4728, 0.7327]])
>>> torch.cat([a,b],dim=1)
tensor([[0.6999, 0.9184, 0.7243, 0.3705, 0.4654],
        [0.2796, 0.1800, 0.8309, 0.4728, 0.7327]])

>>> a = torch.rand([2,2])
>>> b = torch.rand([3,2])
>>> a
tensor([[0.1727, 0.4451],
        [0.4587, 0.9845]])
>>> b
tensor([[0.5461, 0.9072],
        [0.2116, 0.9429],
        [0.9225, 0.1885]])
>>> torch.cat([a,b],dim=0)
tensor([[0.1727, 0.4451],
        [0.4587, 0.9845],
        [0.5461, 0.9072],
  • chunk:将一个张量分割成特定数目的张量,如果维度不能被整除,最后一个会比较小。
>>> b
tensor([[0.4976, 0.0441, 0.7566],
        [0.8283, 0.6617, 0.0814],
        [0.7360, 0.8517, 0.4190]])
>>> torch.chunk(b,chunks=2)
(tensor([[0.4976, 0.0441, 0.7566],
        [0.8283, 0.6617, 0.0814]]), tensor([[0.7360, 0.8517, 0.4190]]))
>>> c,d = torch.chunk(b,chunks=2)
>>> c
tensor([[0.4976, 0.0441, 0.7566],
        [0.8283, 0.6617, 0.0814]])
>>> d
tensor([[0.7360, 0.8517, 0.4190]])

>>> b = torch.rand([3,2])
>>> b
tensor([[0.1250, 0.2930],
        [0.7466, 0.3966],
        [0.9748, 0.5332]])
>>> c,d = torch.chunk(b,chunks=2,dim=1)
>>> c
tensor([[0.1250],
        [0.7466],
        [0.9748]])
>>> d
tensor([[0.2930],
        [0.3966],
        [0.5332]])

  • gather: 沿着某一维取变量
    【Pytorch】张量1
>>> t = torch.tensor([[1,2],[3,4]])
>>> t
tensor([[1, 2],
        [3, 4]])
>>> torch.gather(t,1,torch.tensor([[0,0],[1,0]]))
tensor([[1, 1],
        [4, 3]])
>>> torch.gather(t,1,torch.tensor([[0,0],[0,1]]))
tensor([[1, 1],
        [3, 4]])
  • torch.reshape(input,shape) → Tensor
    不改变顺序,只要产品相同
>>> a = torch.arange(4.)
>>> torch.reshape(a,(2,2))
tensor([[0., 1.],
        [2., 3.]])
>>> a = torch.arange(4.)
>>> a
tensor([0., 1., 2., 3.])
>>> torch.reshape(a,(2,2))
tensor([[0., 1.],
        [2., 3.]])
>>> b = torch.tensor([[0,1],[2,3]])
>>> torch.reshape(b,(-1,))# -1默认总的长度
tensor([0, 1, 2, 3])
>>> b
tensor([[0, 1],
        [2, 3]])

  • scatter_,当从src函数写入张量中,indices指名是哪个索引
    inplace 加了下划线:内存位置没有发生改变
    【Pytorch】张量1
    Tensor.scatter_(dim,index,src,reduce=None)→Tensor

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2022年3月28日
下一篇 2022年3月28日

相关推荐