3.创建Tensor

1.从numpy引入:

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

2.从list引入:

>>> torch.tensor([2,3.3])
tensor([2.0000, 3.3000])
>>> torch.tensor([[2,3.3],[5,6.6]])
tensor([[2.0000, 3.3000],
        [5.0000, 6.6000]])
# 这里接收的是现有的数据,但是建议少用,因为此种情况容易产生混淆
>>> torch.FloatTensor([2,3.3])
tensor([2.0000, 3.3000])
# 这里接收的是shape
>>> torch.FloatTensor(2,3)
tensor([[9.6281e-31, 4.5914e-41, 1.4013e-45],
        [0.0000e+00, 1.4013e-45, 0.0000e+00]])

这里需要注意的是.tensor()方法是接受现有的数据,从而创建tensor。
而.Tensor()和.FloatTensor()两者是类似的,接收的是数据的维度(shape),当然也可以接收现有的数据,从而创建tensor。

3.在内存空间中生成一个未初始化的数据:

# .empty()方法接收的是shape
>>> torch.empty(2,3)
tensor([[1.4013e-45, 0.0000e+00, 1.4013e-45],
        [0.0000e+00, 1.4013e-45, 0.0000e+00]])
# .FloatTensor()方法接收的也是shape
>>> torch.FloatTensor(2,3)
tensor([[9.6281e-31, 4.5914e-41, 1.4013e-45],
        [0.0000e+00, 1.4013e-45, 0.0000e+00]])
# .IntTensor()方法接收的也是shape
>>> torch.IntTensor(2,3)
tensor([[1668444020, 1835347560,  679048304],
        [ 691219506,       2573,          0]], dtype=torch.int32)

这里需要注意的是,生成的未初始化的数据要么非常大,要么非常小,直接使用的话会出现类似torch.nan或者torch.inf这样的错误。
因为这些未初始化的api方法,只是作为载体,后续需要将数据写进来,覆盖掉随机生成的数据。

4.默认数据类型:

# .tensor()默认生成的数据类型为FloatTensor
>>> torch.tensor([1.1,2]).type()
'torch.FloatTensor'
# 通过.set_default_tensor_type()设置自定义的数据类型
# DoubleTensor在增强学习中会比较常用,因为DoubleTensor是64字节长,有更高的精度
>>> torch.set_default_tensor_type(torch.DoubleTensor)
>>> torch.tensor([1.1,2]).type()
'torch.DoubleTensor'

5.随机分布:

前面第三部分中的未初始化的数据存在问题,推荐使用以下这些随机初始化方法生成数据。

# .rand()方法随机的使用[0,1)的均值分布进行随机生成
>>> torch.rand(3,3)
tensor([[0.1038, 0.5749, 0.5616],
        [0.5033, 0.2669, 0.6490],
        [0.1425, 0.8941, 0.3171]])
>>> a = torch.rand(3,3)
# .rand_like()方法接收的是一个tensor,它是将tensor的shape读出来之后在未给rand()
>>> torch.rand_like(a)
tensor([[0.6234, 0.3106, 0.3739],
        [0.5397, 0.1455, 0.3968],
        [0.4068, 0.7172, 0.7629]])
# .randint(min,max,shape)方法使用自定义的数据空间,其中取值范围为[min,max),shape接收的是一个列表
>>> torch.randint(1,10,[3,3])
tensor([[3, 1, 4],
        [5, 5, 1],
        [1, 6, 1]])

6.正态分布:

# .randn()为N(0,1),即均值为0,方差为1
>>> torch.randn(3,3)
tensor([[-0.1667, -0.7161,  1.3121],
        [-2.1634,  0.3914, -0.1786],
        [-1.1079, -0.0960,  0.1424]])
# .normal(u,std)方法可以自定义取值和方差,但是使用起来不是很方便
>>> torch.normal(mean=torch.full([10],0),std=torch.arange(1,0,-0.1))
tensor([-8.4386e-01, -8.0530e-01,  1.1376e+00, -4.5776e-01,  5.8030e-01,
         2.3419e-01, -6.8687e-02,  5.8278e-01, -1.1780e-04,  1.3083e-01])
>>> torch.normal(mean=torch.full([10],0),std=torch.arange(1,0,-0.1))
tensor([ 0.7604, -1.6153, -0.3487,  0.4423, -0.0799, -0.2394, -0.4222,  0.3900,
        -0.1942, -0.2073])

7.将tensor赋值为同一个元素:

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

8.生成等差数列的tensor:

>>> 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])
# 因为.range()方法可以使用.arange()方法替代,不推荐使用
>>> torch.range(0,10)
__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].
tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])

9.生成等分数据(linspace,logspace)的tensor:

# linspace(min,max,steps)方法将[min,max]等分为step份
>>> 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.])
# logspace(min,max,steps)方法将[min,max]等分为step份,然后返回的是10的相应次方
# base参数可以设置底数,例如2、10、e等
>>> torch.logspace(0,-1,steps=10)
tensor([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292,
        0.1000])
>>> torch.logspace(0,1,steps=10)
tensor([ 1.0000,  1.2915,  1.6681,  2.1544,  2.7826,  3.5938,  4.6416,  5.9948,
         7.7426, 10.0000])

10.生成全部为0或者1或者对眼的tensor:

>>> 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.]])
# .eye()方法当给定的shape不对应时,多余的会以0填充
>>> torch.eye(3,4)
tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 1., 0.]])
# .eye()方法只能接受一个或两个数据,也就是说不能生产更高维的数据
>>> torch.eye(3)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

11.生成随机打散的tensor:

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

欢迎留下您的宝贵建议

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments