PyTorch基础-torch

一、张量数据类型

1.1 pytorch与python数据类型对比

python pytorch
Int IntTensor of size()
float FloatTensor of size()
Int array IntTensor of size [d1,d2,…]
Float array FloatTensor of size [d1,d2,…]
string ont-hot or Embedding(Word2Vec,glove)

由于PyTorch不是一个完备的语言库,它是面向数据计算的GPU加速库,因此没有内建对string的支持,在PyTorch中可用以下两种方式来表示string:

  • One-hot
  • Embedding
    • Word2vec
    • glove

1.2 pytorch内建的数据类型

Data tyoe dtype CPU tensor GPU tensor
32-bit floating point torch.float32 or torch.float torch.FloatTensor torch.cuda.FloatTensor
64-bit floating point torch.float64 or torch.double torch.DoubleTensor torch.cuda.DoubleTensor
16-bit floating point torch.float16 or torch.half N/A torch.cuda.HalfTensor
8-bit integer (unsigned) torch.uint8 torch.ByteTensor torch.cuda.ByteTensor
8-bit integer (signed) torch.int8 torch.CharTensor torch.cuda.CharTensor
16-bit integer (signed) torch.int16 or torch.short torch.ShortTensor torch.cuda.ShortTensor
32-bit integer (signed) torch.int32 or torch.int torch.IntTensor torch.cuda.IntTensor
64-bit integer (signed) torch.int64 or torch.long torch.LongTensor torch.cuda.LongTensor

Tpye check

  • Tensor.type()
  • type(Tensor)
  • isinstance(Tensor,torch.FloatTensor)
    >>> a = torch.randn(2,3)
    >>> a.type()
    'torch.FloatTensor'
    >>> type(a)
    <class 'torch.Tensor'>
    >>> isinstance(a,torch.FloatTensor)
    True
    >>> isinstance(a,torch.cuda.DoubleTensor)
    False
    >>> data = a.cuda() #x.cuda()返回一个gpu的引用
    >>> isinstance(data,torch.cuda.DoubleTensor)
    >>> True

    标量:Dimension 0/rank 0,Deep Learning中常用于loss表示

    >>> torch.tensor(2.2)#.tensor接受的是数据的内容
    tensor(2.2000)
    >>> a = torch.tensor(2.2)
    >>> a.shape
    torch.Size([])
    >>> len(a.shape)
    0
    >>> a.dim()
    0
    >>> a.size()
    torch.Size([])

    Dimension 1/rank 1,Deep Learning中常用于Bias表示

    >>> torch.tensor([1.1])
    tensor([1.1000])
    >>> torch.tensor([1.1,2.2])
    tensor([1.1000, 2.2000])
    >>> torch.FloatTensor(2) #接受的是size,random初始化
    tensor([2.2758e-07, 4.5682e-41])
    >>> data = np.ones(2)
    >>> data
    array([1., 1.])
    >>> torch.from_numpy(data)
    tensor([1., 1.], dtype=torch.float64)

    Dimension 2

    >>> a = torch.randn(2,3) #FloatTensor(2,3)
    >>> a
    tensor([[-0.1680,  0.4534, -0.4045],
            [-1.0437, -0.4634,  0.7432]])
    >>> a.shape
    torch.Size([2, 3])
    >>> a.size(0)
    2
    >>> a.size(1)
    3
    >>> a.shape[0]
    2
    >>> a.shape[1]
    3

    Dim 3:RNN Input with Batch

    >>> a=torch.rand(1,2,3)
    >>> a
    tensor([[[0.2226, 0.0342, 0.1301],
             [0.6371, 0.6930, 0.9356]]])
    >>> a.shape
    torch.Size([1, 2, 3])
    >>> a[0] #[2,3]
    tensor([[0.2226, 0.0342, 0.1301],
            [0.6371, 0.6930, 0.9356]])
    >>> list(a.shape)
    [1, 2, 3]

    Dim4:CNN[b,c,h,w]

    numbel是指tensor占用内存的数量

    >>> a = torch.rand(2,3,28,28)
    >>> a
    tensor([[[[0.7190, 0.8762, 0.3667,  ..., 0.8682, 0.5834, 0.7012],
              [0.4110, 0.5633, 0.1516,  ..., 0.6877, 0.1930, 0.9480],
              [0.0063, 0.8593, 0.4722,  ..., 0.4012, 0.8891, 0.0254],
              ...,
              ...,
              [0.3267, 0.8081, 0.5329,  ..., 0.3658, 0.9325, 0.6759],
              [0.4113, 0.8107, 0.9934,  ..., 0.2609, 0.1763, 0.5233],
              [0.7673, 0.3748, 0.0287,  ..., 0.0348, 0.0529, 0.8054]]]])
    >>> >>> a.shape
    torch.Size([2, 3, 28, 28])
    >>> a.numel()
    4704
    >>> a.dim() #len(a.shape)
    4
    >>> a=torch.tensor(1)
    >>> a.dim()
    0

    创建Tensor

    • Import form

      >>> a=np.array([2,3,3])
      >>> torch.from_numpy(a)
      tensor([2, 3, 3])
      >>> a=np.ones([2,3,3])
      >>> torch.from_numpy(a)
      tensor([[[1., 1., 1.],
               [1., 1., 1.],
               [1., 1., 1.]],
      
              [[1., 1., 1.],
               [1., 1., 1.],
               [1., 1., 1.]]], dtype=torch.float64)
    • Import from List
      torch.tensor([2.,3.2])
      tensor([2.0000, 3.2000])
      torch.FloatTensor([2.,3.2]) #这种方式不推荐使用,接受具体的数据推荐使用torch,tensor
      tensor([2.0000, 3.2000])
      torch.Tensor([2.,3.2])
      tensor([2.0000, 3.2000])
      torch.tensor([[2.,3.2],[1.,22.3]])
      tensor([[ 2.0000,  3.2000],
      [ 1.0000, 22.3000]])

      torch.FloatTensor和torch.Tensor基本是一样的,都是接受书的维度shape创建Tensor,而小写的torch.tensor是接受具体的数据

    uninitialized

    • Torch.empty(2,3)
    • Torch.FloatTensor(d1,d2,d3)
    • Torch.IntTensor(d1,d2,d3)

    注意未初始化的api在使用的时候,若后续没有赋值操作覆盖,将会使得随机初始化的值变的非常大或者非常小(troch.nan/inf)

    随机初始化

    • torch.rand
    • torch.rand_like
    • torch.randint
    • torch.randn:正态分布
    • torch.normal:自定义分布
    >>> torch.rand(3,3)
    tensor([[0.3628, 0.4771, 0.5067],
            [0.6593, 0.6323, 0.9157],
            [0.5882, 0.6289, 0.4311]])
    >>> a=torch.rand(3,3)
    >>> torch.rand_like(a)
    tensor([[0.5168, 0.9998, 0.1509],
            [0.6104, 0.5265, 0.7917],
            [0.3084, 0.9142, 0.0541]])
    >>> torch.randint(1,10,[3,3])
    tensor([[2, 2, 6],
            [3, 9, 1],
            [4, 5, 4]])
    >>> torch.normal(mean=torch.full([10],0),std=torch.arange(1,0,-0.1))
    tensor([-2.1002, -0.2133,  0.9746,  0.6781,  0.3725,  0.6669,  0.4720,  0.7872,
             0.0643,  0.0143])

    set default type

    >>> torch.tensor([1.2,3]).type()
    'torch.FloatTensor' #pytorch默认的数据类型是FloatTensor
    >>> torch.set_default_tensor_type(torch.DoubleTensor)
    >>> torch.tensor([1.2,3]).type()
    'torch.DoubleTensor'  #增强学习一般使用double

    二、torch常用函数

在使用Tensor时,我们首先要掌握如何使用Tensor来定义不同数据类型的变量。和Numpy差不多,PyTorch中的Tensor也有自己的数据类型定义方式,常用的如下:

1.张量Tensors

  • torch.is_tensor(obj) #判断是否为张量,如果是pytorch张量,则返回True
  • torch.is_storage(obj) #判断是否为pytorch Storage,如何是,则返回True
  • torch.set_default_tensor_type(t)
  • torch.numel(input)->int
  • torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None) #设置打印选项。 完全参考自Numpy

2.创建操作 Creation Ops

torch.eye

torch.eye(n, m=None, out=None)->Tensor

返回一个2维张量(单位矩阵),对角线数字为1,其它位置为0

参数说明:

  • n (int) – 行数
  • m (int, 可选) – 列数.如果为None,则默认为n
  • out (Tensor,可选) - 输出张量
>>> torch.eye(4)
tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 1., 0.],
        [0., 0., 0., 1.]])

torch.zero

torch.zeros(*sizes, out=None) → Tensor

返回一个全0的张量,形状由可变参数sizes定义。

>>> torch.zeros(2,3)
tensor([[0., 0., 0.],
        [0., 0., 0.]])
>>> torch.zeros(5)
tensor([0., 0., 0., 0., 0.])

torch.ones

torch.ones(*sizes, out=None) → Tensor #返回一个全为1的张量,形状由可变参数sizes定义。

>>> torch.ones(2,3)
tensor([[1., 1., 1.],
        [1., 1., 1.]])
>>> torch.ones(5)
tensor([1., 1., 1., 1., 1.])

torch.from_numpy

torch.from_numpy(ndarray) → Tensor

numpy.ndarray转换为Tensor。 返回的张量tensor和numpy的ndarray共享同一内存空间。修改一个会导致另外一个也被修改。返回的张量不能调整大小

>>> a= np.array([1,2,4])
>>> t = torch.from_numpy(a)
>>> t
tensor([1, 2, 4])
>>> t[1]=10
>>> a
array([ 1, 10,  4])

torch.linspace

torch.linspace(start, end, steps=100, out=None) → Tensor #返回start和end之间长度为steps的一维张量

参数说明:

  • start (float) – 点集的起始值
  • end (float) – 点集的最终值
  • steps (int) – 在startend间的采样数,即返回多少个数
  • out (Tensor, 可选的) – 结果张量
>>> torch.linspace(1.0,10,steps=5)
tensor([ 1.0000,  3.2500,  5.5000,  7.7500, 10.0000])
>>> torch.linspace(-10,10,steps=5,out=None)
tensor([-10.,  -5.,   0.,   5.,  10.])

torch.logspace

torch.logspace(start, end, steps=100, out=None) → Tensor

返回一个1维张量,包含在区间\(10^{start}\) 和\(10^{end}\)上以对数刻度均匀间隔的steps个点。 输出1维张量的长度为steps,参数说明同torch.linspace

>>> torch.logspace(start=-10,end=10,steps=5)
tensor([1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10])
>>> torch.logspace(start=0.1,end=1.0,steps=5)
tensor([ 1.2589,  2.1135,  3.5481,  5.9566, 10.0000])

torch.FloatTensor

此变量用于生成数据类型为浮点型的Tensor,传递给torch.FloatTensor的参数可以是一个列表,也可以是一个维度值。torch.IntTensor与此类似。

>>> import torch
>>> a = torch.FloatTensor(2,3)
>>> print(a)

-0.1171  0.0000 -0.1171
 0.0000  0.0000  0.0000
[torch.FloatTensor of size 2x3]

>>> b = torch.FloatTensor([2,3,4,5])
>>> print(b)

 2
 3
 4
 5
[torch.FloatTensor of size 4]

torch.rand

用于生成数据类型为浮点型且维度指定的随机Tensor,和在Numpy中使用numpy.rand生成随机数的方法类似,随机生成的浮点数据在0~1区间均匀分布

torch.rand(*sizes, out=None) → Tensor

返回一个张量,填充在[0,1]区间的一组均匀分布随机数。 Tensor的形状由变量sizes定义。

>>> torch.rand(4)
tensor([0.0662, 0.7079, 0.4197, 0.2257])
>>> torch.rand(2,3,out=None)
tensor([[0.8174, 0.8959, 0.2713],
        [0.5343, 0.0275, 0.7712]])
>>> torch.rand(2,3)

torch.randn

torch.randn(*sizes, out=None) → Tensor

用于生成数据类型为浮点型且维度指定的随机Tensor,和在Numpy中使用numpy.randn生成随机数的方法类似,随机生成的浮点数的取值满足均值为0,方差为1的正态分布

>>> torch.randn(4)
tensor([-1.4524,  0.9949, -1.4038,  0.8059])
>>> torch.randn(2,3)
tensor([[ 0.9186,  1.0356,  1.1103],
        [-2.0057, -0.9032,  0.6453]])

torch.randperm

torch.randperm(n, out=None) → LongTensor

输入参数n(int) – 上限(独占),即最大值,返回一个从0n -1的随机整数排列,随机打散。

random.shuffle

>>> torch.randperm(4)
tensor([3, 1, 2, 0])

>>> a=torch.rand(2,3)
>>> b=torch.rand(2,2)
>>> idx=torch.randperm(2)
>>> idx
tensor([1, 0])
>>> idx
tensor([1, 0])
>>> a,b
(tensor([[0.8094, 0.8389, 0.5666],
        [0.6812, 0.5959, 0.4951]]), tensor([[0.4863, 0.5345],
        [0.1246, 0.2468]]))
>>> a[idx]
tensor([[0.6812, 0.5959, 0.4951],
        [0.8094, 0.8389, 0.5666]])
>>> b[idx]
tensor([[0.1246, 0.2468],
        [0.4863, 0.5345]])

torch.arange

torch.arange(start, end, step=1, out=None) → Tensor([start;end))

返回一个1维张量,长度为floor((end?start)/step),floor代表向下取整。包含从startend,以step为步长的一组序列值(默认步长为1)。

>>> torch.arange(1,4)
tensor([1, 2, 3])
>>> torch.arange(1,2.5,0.5)
tensor([1.0000, 1.5000, 2.0000])

torch.range

torch.range(start, end, step=1, out=None) → Tensor([start;end]),现在torch.range将要废弃,

返回一个1维张量,长度为floor((end?start)/step)+1,其中floor代表向下取整数。从start开始,end为结尾,以step为步长的一组值。 step 是两个值之间的间隔,即 Xi+1=Xi+step

>>> import torch
>>> a = torch.range(2,8,1)
__main__:1: UserWarning: torch.range is deprecated in favor of torch.arange and will be removed in 0.5. Note that arange generates values in [start; end), not [start; end].
>>> print(a)
 tensor([2., 3., 4., 5., 6., 7., 8.])

torch.full

>>> torch.full([2,3],7)
tensor([[7., 7., 7.],
        [7., 7., 7.]])
>>> torch.full([],7) #给标量赋值
tensor(7.)
>>> torch.full([1],7)
tensor([7.])
>>> torch.full([2],7)
tensor([7., 7.])

3.Indexing, Slicing, Joining, Mutating Ops

  • dim 0 first
  • select first/last N
  • select by steps
  • select by specific index
  • ...

dim 0 first

>>> a = torch.rand(4,3,28,28)
>>> a[0].shape
torch.Size([3, 28, 28])
>>> a[0,0].shape
torch.Size([28, 28])
>>> a[0,0,2,4]
tensor(0.2409)

select first/last N

>>> a[:2].shape
torch.Size([2, 3, 28, 28])
>>> a[:2,:1,:,:].shape
torch.Size([2, 1, 28, 28])
>>> a[:2,1:,:,:].shape
torch.Size([2, 2, 28, 28])
>>> a[:2,-1:,:,:].shape
torch.Size([2, 1, 28, 28])

select by steps

>>> a[:,:,0:28:2,:].shape
torch.Size([4, 3, 14, 28])
>>> a[:,:,0:28:2,0:14].shape
torch.Size([4, 3, 14, 14])
>>> a[:,:,0:28:2,2:14].shape
torch.Size([4, 3, 14, 12])

通用形式:start : end : step

select by specific index

torch.index_select

torch.index_select(input, dim, index, out=None) → Tensor

参数:

  • input (Tensor) – 输入张量
  • dim (int) – 索引的轴
  • index (LongTensor) – 包含索引下标的一维张量
  • out (Tensor, optional) – 目标张量

沿着指定维度对输入进行切片,取index中指定的相应项(index为一个LongTensor),然后返回到一个新的张量, 返回的张量与原始张量_Tensor_有相同的维度(在指定轴上)。

注意: 返回的张量不与原始张量共享内存空间。

>>> x=torch.randn(3,4)
>>> x
tensor([[ 0.4878, -0.7477, -0.2496,  1.1835],
        [-1.8953,  0.3530,  0.1122,  0.9137],
        [ 0.4686,  0.5230, -1.1191, -1.0911]])
>>> indices = torch.LongTensor([0,2])
>>> indices
tensor([0, 2])
>>> torch.index_select(x,1,indices)
tensor([[ 0.4878, -0.2496],
        [-1.8953,  0.1122],
        [ 0.4686, -1.1191]])
>>> torch.index_select(x,1,indices)
tensor([[ 0.4878, -0.2496],
        [-1.8953,  0.1122],
        [ 0.4686, -1.1191]])
>>> torch.index_select(x,0,indices)
tensor([[ 0.4878, -0.7477, -0.2496,  1.1835],
        [ 0.4686,  0.5230, -1.1191, -1.0911]])

...

>>> a.shape
torch.Size([4, 3, 28, 28])
>>> a[...].shape
torch.Size([4, 3, 28, 28])
>>> a[0,...].shape  #等同于a[0]
torch.Size([3, 28, 28])
>>> a[:,1,...].shape
torch.Size([4, 28, 28])
>>> a[0,...,::2].shape  #等同于a[0,:,:,::2]
torch.Size([3, 28, 14])
>>> a[...,:2].shape
torch.Size([4, 3, 28, 2])

select by mask

torch.masked_select

torch.masked_select(input, mask, out=None) → Tensor

  • input (Tensor) – 输入张量
  • mask (ByteTensor) – 掩码张量,包含了二元索引值
  • out (Tensor, optional) – 目标张量

根据掩码张量mask中的二元值,取输入张量中的指定项( mask为一个 ByteTensor),将取值返回到一个新的1D张量,

张量 mask须跟input张量有相同数量的元素数目,但形状或维度不需要相同。 注意: 返回的张量不与原始张量共享内存空间。

>>> x = torch.randn(3,4)
>>> x
tensor([[ 0.3132, -0.4580, -0.3642,  0.7394],
        [-0.2821,  1.9086, -0.9687,  1.6009],
        [ 0.9800, -0.8546, -0.8855, -0.3807]])
>>> mask = x.ge(0.5)
>>> mask
tensor([[False, False, False,  True],
        [False,  True, False,  True],
        [ True, False, False, False]])
>>> torch.masked_select(x,mask)
tensor([0.7394, 1.9086, 1.6009, 0.9800])
>>> torch.masked_select(x,mask).shape
torch.Size([4])

select by flatten index

>>> src = torch.tensor([[4,3,5],[6,7,8]])
>>> torch.take(src,torch.tensor([0,2,5]))
tensor([4, 5, 8])

torch.nonzero

torch.nonzero(input, out=None) → LongTensor

参数:

  • input (Tensor) – 源张量
  • out (LongTensor, optional) – 包含索引值的结果张量

返回一个包含输入input中非零元素索引的张量。输出张量中的每行包含输入中非零元素的索引。如果输入inputn维,则输出的索引张量output的形状为 z x n, 这里 z 是输入张量input中所有非零元素的个数。

>>> torch.nonzero(torch.Tensor([1,1,1,0,1])) #返回一维张量中非零的索引值
tensor([[0],
        [1],
        [2],
        [4]])
#输入张量input有2(n)维,非零元素有4(z)个,所以输出张量output的shape为4× 2
>>> torch.nonzero(torch.Tensor([[0.6,0.0,0.0,0.0],[0.0,0.4,0.0,0.0],[0.0,0.0,1.2,0.0],[0.0,0.0,5,0.0]]))
tensor([[0, 0],
        [1, 1],
        [2, 2],
        [3, 2]]) #表明input的第4行的index2的为非零数据

维度变换

  • view/reshape(view保证numel不变即可)
  • Squeeze/unsqueeze(删减/增加)
  • Transpose/t/permute
  • Expand/repeat

torch.view/torch.reshape

view操作需保证数据有实际意义,不然会将数据造成破坏,数据的存储、维度顺序非常重要

>>> a=torch.rand(4,1,28,28)  #4张灰度图片
>>> a.view(4,28*28)  #(4,1*28*28)将channel和像素值合并在一起=》[4,784]适合于全连接层
tensor([[0.2301, 0.9408, 0.3547,  ..., 0.9387, 0.0988, 0.9476],
        [0.2724, 0.6440, 0.0037,  ..., 0.7575, 0.1136, 0.7190],
        [0.6347, 0.9259, 0.4316,  ..., 0.8456, 0.2670, 0.6662],
        [0.0801, 0.3157, 0.4126,  ..., 0.4852, 0.2193, 0.8381]])
>>> a.view(4,28*28).shape
torch.Size([4, 784])
>>> a.view(4*28,28).shape
torch.Size([112, 28])
>>> a.view(4*1,28,28).shape
torch.Size([4, 28, 28]) #(4*1,28*28),指关注featuremap
>>> b=a.view(4,784)
>>> b.view(4,28,28,1) #logic Bug,将维度信息丢失掉了
tensor([[[[0.2301],
          [0.9408],
          [0.3547],
          ...,
 [0.4852],
          [0.2193],
          [0.8381]]]])

torch.unsqueeze

torch.unsqueeze(input, dim, out=None)

参数:

  • tensor (Tensor) – 输入张量
  • dim (int) – 插入维度的索引
  • out (Tensor, optional) – 结果张量

返回一个新的张量,对输入的指定位置插入维度。注意: 返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个。如果dim为负,则将会被转化dim+input.dim()+1,指定位置在 1[-a.dim()-1,a.dim()+1]=>[-5,5]之间变化。

>>> a.shape
torch.Size([4, 1, 28, 28])
>>> a.unsqueeze(0).shape
torch.Size([1, 4, 1, 28, 28])
>>> a.unsqueeze(-1).shape
torch.Size([4, 1, 28, 28, 1])
>>> a.unsqueeze(-4).shape
torch.Size([4, 1, 1, 28, 28])
>>>
>>> a.unsqueeze(-5).shape
torch.Size([1, 4, 1, 28, 28])
>>> a.unsqueeze(5).shape
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: Dimension out of range (expected to be in range of [-5, 4], but got 5)

>>> a=torch.tensor([1.2,2.3])
>>> a.unsqueeze(-1)  # [2,1]
tensor([[1.2000],
        [2.3000]])
>>> a.unsqueeze(0) #[1,2]
tensor([[1.2000, 2.3000]])

example:

>>> b=torch.rand(32) #bias相当于给每个channel上的所有像素增加一个偏置
>>> f=torch.rand(4,32,14,14)
>>> b=b.unsqueeze(1).unsqueeze(2).unsqueeze(0)
>>> b.shape
torch.Size([1, 32, 1, 1])

torch.squeeze

>>> b.shape
torch.Size([1, 32, 1, 1])
>>> b.squeeze().shape
torch.Size([32])
>>> b.squeeze(0).shape
torch.Size([32, 1, 1])
>>> b.shape
torch.Size([1, 32, 1, 1])
>>> b.squeeze(-1).shape
torch.Size([1, 32, 1])
>>> b.squeeze(1).shape  #32是不能被挤压的,只能挤压1的维度
torch.Size([1, 32, 1, 1])
>>> b.squeeze(-4).shape
torch.Size([32, 1, 1])

torch.unbind

torch.unbind(tensor, dim=0)[source]

参数:

  • tensor (Tensor) – 输入张量
  • dim (int) – 删除的维度

移除指定维后,返回一个元组,包含了沿着指定维切片后的各个切片

torch.expand/torch.repeat

  • Expand:broadcasting【不会主动复制数据,只在需要的时候,这是推荐的一种方式;进行操作的时候需要前后dim一致,只能从dim1->dimN;-1表示dim不变】
  • Repeat:memory copied(给出的参数p为在相应的维度上拷贝p次)
###expand
>>> a=torch.rand(4,32,14,14)
>>> b.shape
torch.Size([1, 32, 1, 1])
>>> b.expand(4,32,14,14).shape
torch.Size([4, 32, 14, 14])
>>> b.expand(-1,32,-1,-1).shape
torch.Size([1, 32, 1, 1])
>>> b.expand(-1,32,-1,-4).shape  #-4是bug,在最新版的facebook已经被修复了
torch.Size([1, 32, 1, -4])
##repeat
>>> b.shape
torch.Size([1, 32, 1, 1])
>>> b.repeat(4,32,1,1).shape
torch.Size([4, 1024, 1, 1])
>>> b.repeat(4,1,1,1).shape
torch.Size([4, 32, 1, 1])
>>> b.repeat(4,1,32,32).shape
torch.Size([4, 32, 32, 32])

torch.t

torch.t(input, out=None) → Tensor

参数:

  • input (Tensor) – 输入张量
  • out (Tensor, optional) – 结果张量

输入一个矩阵(2维张量),并转置0, 1维。 可以被视为函数transpose(input, 0, 1)的简写函数,但是torch.t只适用于2D。

torch.transpose

torch.transpose(input, dim0, dim1, out=None) → Tensor

  • input (Tensor) – 输入张量
  • dim0 (int) – 转置的第一维
  • dim1 (int) – 转置的第二维

返回输入矩阵input的转置。交换维度dim0dim1。 输出张量与输入张量共享内存,所以改变其中一个会导致另外一个也被修改。高维矩阵的转置常用torch.transpose

>>> a=torch.rand(4,3,32,32)
>>> a1.shape
torch.Size([4, 32, 32, 3])
>>> a1 = a.transpose(1,3).view(4,3*32*32).view(4,3,32,32)#[b,c,h,w]->[b,w,h,c]->[b,c,w,h]维度信息与原来的存储信息不一致,报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.
>>> a1 = a.transpose(1,3).contiguous().view(4,3*32*32).view(4,3,32,32) #contiguous将数据变为连续,同上,维度污染,错误
>>> a2 = a.transpose(1,3).contiguous().view(4,3*32*32).view(4,32,32,3).transpose(1,3)
>>> a1.shape,a2.shape
(torch.Size([4, 3, 32, 32]), torch.Size([4, 3, 32, 32]))
>>> torch.all(torch.eq(a,a1))
tensor(False)
>>> torch.all(torch.eq(a,a2))
tensor(True)

permute

permute()参数信息是原来Tensor的维度信息

[b,h,w,c]是numpy存储图片的格式,只有这个格式才能导出numpy

>>> a=torch.rand(4,3,28,28) #[b,c,h,w]
>>> a.transpose(1,3).shape
torch.Size([4, 28, 28, 3])
>>> b=torch.rand(4,3,28,32)
>>> b.transpose(1,3).shape #[b,c,h,w]->[b,w,h,c]
torch.Size([4, 32, 28, 3])
>>> b.transpose(1,3).shape #[b,w,h,c]->[b,c,h,w]
torch.Size([4, 32, 28, 3])
>>> b.permute(0,2,3,1).shape #[b,c,h,w]->[b,h,w,c]
torch.Size([4, 28, 32, 3])

原文地址:https://www.cnblogs.com/lyszyl/p/12161819.html

时间: 2024-08-30 16:52:10

PyTorch基础-torch的相关文章

Pytorch基础的基本概念

1.什么是Pytorch,为什么选择Pytroch? 2.Pytroch的安装 3.配置Python环境 4.准备Python管理器 5.通过命令行安装PyTorch 6.PyTorch基础概念 GPU云服务器默认提供了pytorch的环境, 7.通用代码实现流程(实现一个深度学习的代码流程) import torch import torch.nn as nn import torch.nn.functional as F class Net(nn.Module): def __init__(

PyTorch基础——词向量(Word Vector)技术

一.介绍 内容 将接触现代 NLP 技术的基础:词向量技术. 第一个是构建一个简单的 N-Gram 语言模型,它可以根据 N 个历史词汇预测下一个单词,从而得到每一个单词的向量表示. 第二个将接触到现代词向量技术常用的模型 Word2Vec.在实验中将以小说<三体>为例,展示了小语料在 Word2Vec 模型中能够取得的效果. 在最后一个将加载已经训练好的一个大规模词向量,并利用这些词向量来做一些简单的运算和测试,以探索词向量中包含的语义信息. 知识点 N-Gram(NPLM) 语言模型 Wo

『Pytorch』torch基本操作

Tensor基础操作 import torch as t Tensor基础操作 # 构建张量空间,不初始化 x = t.Tensor(5,3) x """ -2.4365e-20 -1.4335e-03 -2.4290e+25 -1.0283e-13 -2.8296e-07 -2.0769e+22 -1.3816e-33 -6.4672e-32 1.4497e-32 1.6020e-19 6.2625e+22 4.7428e+30 4.0095e-08 1.1943e-32

Windows版pytorch,torch简明安装

pytorch是facebook开发的深度学习库,其目标是想成为深度学习领域整合gpu加速的numpy.笔者研究的re-id领域最近有不少基于pytoch的代码,跟进一下.因为编程时一直远程到工作站上不太方便,本地开发用的是windows(笔记本对ubuntu的支持不佳),无奈pytoch目前还没有对windows的官方支持(计划0.4版本开始支持,目前是0.3.1),.所幸的是知乎用户蒲嘉宸一直在提供适合windows的包. 首先,我们根据自己的环境下载对应包:https://pan.baid

PyTorch之torch.utils.data.DataLoader解读

torch.utils.data.DataLoader 参数介绍: 1.dataset,这个就是PyTorch已有的数据读取接口(比如torchvision.datasets.ImageFolder)或者自定义的数据接口的输出,该输出要么是torch.utils.data.Dataset类的对象,要么是继承自torch.utils.data.Dataset类的自定义类的对象. 2.batch_size,根据具体情况设置即可. 3.shuffle,一般在训练数据中会采用. 4.collate_fn

Pytorch基础重新巩固

首先是pytorch的机制: Torch 自称为神经网络界的 Numpy, 因为他能将 torch 产生的 tensor 放在 GPU 中加速运算 最常用的数据形式是tensor   ,      torch中的tensor类似tensorflow中的tensor; 在编写代码的时候,注意把自己的数据转为tensor,然后如果需要梯度计算类似的操作再转为variable:若需要cuda运算,再加上cuda 其次是pytorch用于存储不断变化的量的variable: variable计算的时候,

pytorch基础问题

本文将自己在pytorch学习中遇见的各种问题整理起来,并且持续更新. 1:torch.Tensor和torch.tensor的区别 开始使用torch.tensor和torch.Tensor的时候发现结果都是一样的.都能生成新的张量.但根源上是有差别的. import torch n=torch.tensor([[3,4],[1,2]]) x=torch.Tensor([[3,4],[1,2]]) print(n,'|||',x) print(n.shape,'|||',x.shape) pr

pytorch基础2

下面是常见函数的代码例子 1 import torch 2 import numpy as np 3 print("分割线-----------------------------------------") 4 #加减乘除操作 5 a = torch.rand(3,4) 6 b = torch.rand(4) 7 print(a) 8 print(b) 9 print(torch.add(a, b)) 10 print(torch.sub(a, b)) 11 print(torch.

pyTorch进阶-torch

一.Broadcast自动扩展 Expand和unsquee的结合,习惯性行是高维度,列是低维度 example: 小维度指定,大维度随意 二.拼接与拆分 Cat Stack:增加新的维度 Split(按照长度进行拆分) Chunk(按照数量进行拆分) torch.stack torch.stack(sequence, dim=0) 参数: sqequence (Sequence) – 待连接的张量序列 dim (int) – 插入的维度.必须介于 0 与 待连接的张量序列数之间. 沿着一个新维