import from Numpy

>>> a=np.array([2,3.3])
>>> torch.from_numpy(a)
tensor([2.0000, 3.3000], dtype=torch.float64)
>>> a=np.ones([2,3])
>>> torch.from_numpy(a)
tensor([[1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)

import from List

>>> torch.tensor([2,3.2])#torch.tensor接受现有数据,Tensor和FloatTensor接受一个shape(数据的维度)
tensor([2.0000, 3.2000])
>>> torch.FloatTensor([2.,3.2])#尽量不要使用
tensor([2.0000, 3.2000])
>>> torch.tensor([2,3.2])
tensor([2.0000, 3.2000])
>>> torch.tensor([[2,3.2],[3.2,1.]])
tensor([[2.0000, 3.2000],
        [3.2000, 1.0000]])

uninitialized

  • Torch.empty()
  • Torch.FloatTensor(d1,d2,d3)
    • Not torch.FloatTensor([1,2])=torch.tensor([1,2])
  • Torch.IntTensor(d1,d2,d3)
>>> torch.empty(1)
tensor([nan])
>>> torch.Tensor(2,3)
tensor([[0., 0., 0.],
        [0., 0., 0.]])
>>> torch.IntTensor(2,3)
tensor([[1064135033, 1056173720, 1064928610],
        [1038606400, 1060693140, 1040603820]], dtype=torch.int32)
>>> torch.FloatTensor(2,3)
tensor([[0., 0., 0.],
        [0., 0., 0.]])
  • 后续要使用其他数据覆盖掉未初始化数据

set default type

>>> torch.tensor([1.2,3]).type()
'torch.FloatTensor'
>>> torch.set_default_tensor_type(torch.DoubleTensor)
>>> torch.tensor([1.2,3]).type()
'torch.DoubleTensor'

rand/rand_like,randint

  • rand产生不包括1

    >>> a=torch.rand(3,3)
    >>> a
    tensor([[0.4744, 0.7030, 0.8309],
            [0.7480, 0.0447, 0.8336],
            [0.7141, 0.0262, 0.5663]])
    >>> torch.rand_like(a)
    tensor([[0.4703, 0.5290, 0.2008],
            [0.7135, 0.9063, 0.4426],
            [0.0830, 0.7966, 0.8128]])
    >>> torch.randint(1,10,[3,3])
    tensor([[8, 2, 4],
            [2, 5, 1],
            [3, 5, 2]])

randn

>>> torch.randn(3,3)#均值为0,方差为1
tensor([[ 0.4364,  0.0172,  1.0050],
        [ 0.2373, -2.0858, -0.9249],
        [ 0.8681,  1.2555, -1.0074]])

full

>>> torch.full([2,3],7)
tensor([[7, 7, 7],
        [7, 7, 7]])
>>> torch.full([],7)
tensor(7)
>>> torch.full([1],7)
tensor([7])

arange/range

>>> torch.arange(0,10)
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> torch.arange(0,10,2)
tensor([0, 2, 4, 6, 8])

linspace/logspace

>>> torch.linspace(0,10,steps=4)
tensor([ 0.0000,  3.3333,  6.6667, 10.0000])
>>> torch.linspace(0,10,steps=10)
tensor([ 0.0000,  1.1111,  2.2222,  3.3333,  4.4444,  5.5556,  6.6667,  7.7778,
         8.8889, 10.0000])
>>> torch.linspace(0,10,steps=11)
tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
>>> torch.linspace(0,-1,steps=10)
tensor([ 0.0000, -0.1111, -0.2222, -0.3333, -0.4444, -0.5556, -0.6667, -0.7778,
        -0.8889, -1.0000])
>>> #生成10的0次方为起始值,10的-1次方为终止值的8个数构成的等比数列
... c = torch.logspace(0,-1,steps=8)
>>> print(c)
tensor([1.0000, 0.7197, 0.5179, 0.3728, 0.2683, 0.1931, 0.1389, 0.1000])

Ones/zeros/eye

>>> torch.ones(3,3)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
>>> torch.zeros(3,3)
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
>>> torch.eye(3,3)#对角矩阵
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

randperm

  • random.shuffle nmpuy
>>> a=torch.rand(2,3)
>>> b=torch.rand(2,2)
>>> idx=torch.randperm(2)
>>> idx
tensor([0, 1])
>>> idx
tensor([0, 1])
>>> a[idx]
tensor([[0.9459, 0.4927, 0.4129],
        [0.3428, 0.3058, 0.5658]])
>>> b[idx]
tensor([[0.1292, 0.6434],
        [0.9975, 0.4396]])
>>> a,b
(tensor([[0.9459, 0.4927, 0.4129],
        [0.3428, 0.3058, 0.5658]]), tensor([[0.1292, 0.6434],
        [0.9975, 0.4396]]))