pytorch对tensor的一些基本操作(一)

Tensor张量

import torch
x=torch.Tensor(5)#默认为0
print(x)
tensor([0., 0., 0., 0., 0.])

1.torch.ones默认值为1

x=torch.ones(1,2)
print(x)
tensor([[1., 1.]])

2.torch.zeros默认值为0

x=torch.zeros(1,2)
print(x)
tensor([[0., 0.]])

3.torch.full填充

x=torch.full((1,2),3.14)
print(x)
tensor([[3.1400, 3.1400]])

4.torch.empty为空

x=torch.empty(3,4)
print(x)
tensor([[-9.9029e+20,  4.5825e-41, -1.2786e-34,  3.0943e-41],
        [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00],
        [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00]])

5.torch.rand随机数,均匀分布在0和1之间

x=torch.rand(3,4)
print(x)
tensor([[0.3000, 0.7942, 0.1237, 0.6727],
        [0.9596, 0.7241, 0.0643, 0.2116],
        [0.9158, 0.6850, 0.3265, 0.1381]])

6.torch.randn正态分布,均值为0,方差为1

x=torch.randn(3,4)
print(x)
tensor([[ 1.3331,  0.7957, -0.5624, -1.1328],
        [ 0.4470,  1.2949, -0.2512,  1.8618],
        [ 0.6336, -0.6330,  0.9996, -1.2672]])

7.torch.randint创建随机整数

x=torch.randint(1,99,(3,4))#需要给出范围及tensor的大小
print(x)
tensor([[97, 82, 88, 67],
        [23, 35, 32, 70],
        [61, 50, 90, 44]])

8.torch.randperm选择随机数,下面的例子为输出0到99的100个随机数

x=torch.randperm(99)#需要给出范围
print(x)
tensor([84, 47, 66, 85, 44, 72, 69, 41,  3, 18, 70, 81, 76, 95,  4, 90, 25, 22,
        71, 93, 42, 33, 34, 89, 56, 64, 32, 28, 17, 36, 80, 15, 87, 82, 29,  0,
        77, 19,  1, 60, 31,  6, 73, 88, 78, 12, 27, 49,  5, 43, 11, 40, 45, 96,
         8, 75, 26, 24, 37, 65, 74, 52, 10, 97, 39, 20, 58, 92, 98, 83, 50, 21,
        59,  7, 61, 46,  9, 91, 23, 62, 55, 53, 67, 79,  2, 57, 51, 63, 14, 86,
        94, 13, 30, 68, 16, 38, 54, 35, 48])

9.torch.argmin() 特别的在dim=0表示二维中的列,dim=1在二维矩阵中表示行

x = torch.randint(1,99,(3,3))
print(x)
print(torch.argmin(x, dim=0))#返回每列的最小值的索引
tensor([[66, 89, 26],
        [72, 48, 86],
        [ 4, 62, 80]])
tensor([2, 1, 0])

10.torch.argmax() 同理

x = torch.randint(1,99,(3,3))
print(x)
print(torch.argmax(x, dim=1))#返回每行的最大值的索引
tensor([[89, 82, 24],
        [83, 30, 83],
        [38,  3, 58]])
tensor([0, 2, 2])

11.torch.ones_like 返回与输入tensor形状一样的一个tensor,但是值全都是1

x=torch.empty(2,3)
print(torch.ones_like(x))
y=torch.zeros(3,3)
print(torch.ones_like(y))
tensor([[1., 1., 1.],
        [1., 1., 1.]])
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])

12.torch.randn_like返回与输入tensor形状一样的一个tensor,但是值全都是(0,1)正态分布

x=torch.empty(2,3)
print(torch.randn_like(x))
y=torch.zeros(3,3)
print(torch.randn_like(y))
tensor([[-0.2873,  0.5728, -0.9096],
        [-0.6106, -1.6514, -0.4743]])
tensor([[-0.0768, -0.4954,  0.0630],
        [ 0.5701,  0.6618, -1.1023],
        [ 0.9272, -0.8624, -1.3478]])

13.torch.add加法

x=torch.ones(5)
y=torch.ones(5)
print(torch.add(x,y))
print(torch.add(x,100))
a = torch.randn(4)
b = torch.randn(4, 1)
print(torch.add(a, b, alpha=10))
tensor([2., 2., 2., 2., 2.])
tensor([101., 101., 101., 101., 101.])
tensor([[ 6.8860,  7.0833,  6.0510,  5.4607],
        [ 2.4827,  2.6800,  1.6477,  1.0574],
        [ 8.4214,  8.6187,  7.5865,  6.9961],
        [-5.0263, -4.8290, -5.8612, -6.4515]])

14.torch.tensor创建

print(torch.tensor([[1,2,3],[4,5,6]]))
tensor([[1, 2, 3],
        [4, 5, 6]])

15.torch.as_tensor创建

import numpy as np
x=np.array([1,2,3,4,5,6])
print(torch.as_tensor(x))
y=torch.tensor([1,2,3,4,5,6])
print(torch.as_tensor(y))
z=[1,2,3,4,5,6]
print(torch.as_tensor(z))
tensor([1, 2, 3, 4, 5, 6])
tensor([1, 2, 3, 4, 5, 6])
tensor([1, 2, 3, 4, 5, 6])

16.torch.from_numpy从numpy转为tensor

x=np.array([1,2,3,4,5,6])
print(torch.from_numpy(x))
tensor([1, 2, 3, 4, 5, 6])

17.torch.reshape改变shape

x=torch.zeros(3,4)
print(x.reshape(2,6))
print(x)
tensor([[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.]])
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])

18.torch.numel统计张量里有几个元素

x=torch.randn(3,4)
print(torch.numel(x))
12

19.torch.view视图

x=torch.randn(3,4)
print(x.view(2,6))
print(x)
tensor([[-0.8057, -0.0540, -0.9492,  2.4807, -1.2851,  0.0503],
        [ 1.2207, -0.6910,  0.0278, -0.5718, -1.9288,  0.3790]])
tensor([[-0.8057, -0.0540, -0.9492,  2.4807],
        [-1.2851,  0.0503,  1.2207, -0.6910],
        [ 0.0278, -0.5718, -1.9288,  0.3790]])

torch.Tensor.reshape() vs. torch.Tensor.view()

  • 相同点:从功能上来看,它们的作用是相同的,都是将原张量元素(按顺序)重组为新的shape。
    区别在于:
  • view()方法只能改变连续的(contiguous)张量,否则需要先调用.contiguous()方法,而.reshape()方法不受此限制;
  • view()方法返回的张量与原张量共享基础数据(存储器,注意不是共享内存地址,详见代码 ),而.reshape()方法返回的是原张量的copy还是view(即是否跟原张量共享存储),事先是不知道的,如果可以返回view,那么.reshape()方法返回的就是原张量的view,否则返回的就是copy。

–> 因此,为避免语义冲突:

  1. 如果需要原张量的拷贝(copy),就使用.clone()方法;
  2. 而如果需要原张量的视图(view),就使用.view()方法;
  3. 如果想要原张量的视图(view),但是原张量不连续(contiguous),不过原张量拥有兼容的步长(strides),此时可以考虑使用.reshape()函数。

20.torch.arange生成一个区间的数,下面例题是生成从0开始到10结束之间步长为4的数,左闭右开

x = torch.arange(0,10,4)
print(x)
tensor([0, 4, 8])

21.torch.linspace切分,把2到10切分成5等分

x = torch.linspace(2,10,5)
print(x)
tensor([ 2.,  4.,  6.,  8., 10.])

22.torch.eye对角线为1的tensor

x = torch.eye(3,3)
print(x)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

23.torch.cat连接,把两个张量堆叠起来,dim=0为纵轴堆叠,dim=1为横轴堆叠

x = torch.randint(1,10,(2,3))
print(x)
print(torch.cat((x,x),dim=0))
print(torch.cat((x,x),dim=1))
print(torch.cat((x,x,x),dim=0))
tensor([[5, 8, 8],
        [6, 9, 6]])
tensor([[5, 8, 8],
        [6, 9, 6],
        [5, 8, 8],
        [6, 9, 6]])
tensor([[5, 8, 8, 5, 8, 8],
        [6, 9, 6, 6, 9, 6]])
tensor([[5, 8, 8],
        [6, 9, 6],
        [5, 8, 8],
        [6, 9, 6],
        [5, 8, 8],
        [6, 9, 6]])

24.torch.index_select根据索引选择,.index_select中的参数0为横向选,1为纵向选

x = torch.randint(1,10,(4,4))
print(x)
indices = torch.tensor([0,2])
print(torch.index_select(x,0,indices))
print(torch.index_select(x,1,indices))
indices = torch.tensor([0,1])
print(torch.index_select(x,1,indices))
tensor([[6, 7, 2, 7],
        [9, 5, 2, 1],
        [4, 6, 8, 5],
        [6, 8, 4, 2]])
tensor([[6, 7, 2, 7],
        [4, 6, 8, 5]])
tensor([[6, 2],
        [9, 2],
        [4, 8],
        [6, 4]])
tensor([[6, 7],
        [9, 5],
        [4, 6],
        [6, 8]])

25.torch.narrow缩小

x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(x)
print(torch.narrow(x, 0, 0, 2))#narrow(input, dim(选择维度), start(起始行列数), length(长度表示从起始开始取多少行或者列))
print(torch.narrow(x, 1, 1, 2))
tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
tensor([[1, 2, 3],
        [4, 5, 6]])
tensor([[2, 3],
        [5, 6],
        [8, 9]])

26.torch.t / torch.transpose转置, torch.transpose中0表示行,1表示列,下面例子为行列交换

x = torch.tensor([[1,2,3],[4,5,6]])
y1 = x.t()
y2 = x.transpose(1,0)
print(y1)
print(y2)
tensor([[1, 4],
        [2, 5],
        [3, 6]])
tensor([[1, 4],
        [2, 5],
        [3, 6]])

27.torch.take根据索引获取元素

x = torch.tensor([[1,2,3],[4,5,6]])
print(torch.take(x,torch.tensor([0,2,3])))#将原来的tensor展开成一维,按照顺序索引,索引也必须是个tensor
tensor([1, 3, 4])

28.torch.split分割

x = torch.tensor([[1,2,3,4],[4,5,6,8],[8,9,10,11]])
print(x.split(2))
print(x.split(2,1))#按不同的维度将原tensor分成多个tensor
print(x.split([2,2],1))
(tensor([[1, 2, 3, 4],
        [4, 5, 6, 8]]), tensor([[ 8,  9, 10, 11]]))
(tensor([[1, 2],
        [4, 5],
        [8, 9]]), tensor([[ 3,  4],
        [ 6,  8],
        [10, 11]]))
(tensor([[1, 2],
        [4, 5],
        [8, 9]]), tensor([[ 3,  4],
        [ 6,  8],
        [10, 11]]))

29.torch.is_tensor(x) 检测x是否为张量

x = torch.tensor([[1,2,3,4],[4,5,6,8],[8,9,10,11],[11,12,13,14]])
print(torch.is_tensor(x))
True

30.torch.chunk把张量切块,dim=0为横向切,dim=1为纵向切

x = torch.tensor([[1,2,3,4],[4,5,6,8],[8,9,10,11]])
print(torch.chunk(x,2,dim=0))#将tensor 拆分成相应的组块, 最后一块会小一些如果不能整除的话。
print(torch.chunk(x,2,dim=1))
(tensor([[1, 2, 3, 4],
        [4, 5, 6, 8]]), tensor([[ 8,  9, 10, 11]]))
(tensor([[1, 2],
        [4, 5],
        [8, 9]]), tensor([[ 3,  4],
        [ 6,  8],
        [10, 11]]))

你可能感兴趣的:(pytorch,pytorch)