美文网首页
[PyTorch中文文档]-Package参考-torch-创建

[PyTorch中文文档]-Package参考-torch-创建

作者: 六千宛 | 来源:发表于2021-08-04 16:28 被阅读0次

    torch.eye

    torch.eye(n, m=None, out=None)
    

    返回一个2维张量,对角线位置全1,其它位置全0

    参数:

    • n (int ) – 行数
    • m (int, optional) – 列数.如果为None,则默认为n
    • out (Tensor, optinal) - Output tensor

    返回值: 对角线位置全1,其它位置全0的2维张量

    返回值类型: Tensor

    例子:

    >>> torch.eye(3)
     1  0  0
     0  1  0
     0  0  1
    [torch.FloatTensor of size 3x3]
    

    from_numpy

    torch.from_numpy(ndarray) → Tensor
    

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

    例子:

    >>> a = numpy.array([1, 2, 3])
    >>> t = torch.from_numpy(a)
    >>> t
    torch.LongTensor([1, 2, 3])
    >>> t[0] = -1
    >>> a
    array([-1,  2,  3])
    

    torch.linspace

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

    返回一个1维张量,包含在区间start 和 end 上均匀间隔的steps个点。 输出1维张量的长度为steps。

    参数:

    start (float) – 序列的起始点
    end (float) – 序列的最终值
    steps (int) – 在start 和 end间生成的样本数
    out (Tensor, optional) – 结果张量
    

    例子:

    >>> torch.linspace(3, 10, steps=5)
    
      3.0000
      4.7500
      6.5000
      8.2500
     10.0000
    [torch.FloatTensor of size 5]
    
    >>> torch.linspace(-10, 10, steps=5)
    
    -10
     -5
      0
      5
     10
    [torch.FloatTensor of size 5]
    
    >>> torch.linspace(start=-10, end=10, steps=5)
    
    -10
     -5
      0
      5
     10
    [torch.FloatTensor of size 5]
    

    torch.logspace

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

    返回一个1维张量,包含在区间 10start 和 10end上以对数刻度均匀间隔的steps个点。 输出1维张量的长度为steps。

    参数:

    start (float) – 序列的起始点
    end (float) – 序列的最终值
    steps (int) – 在start 和 end间生成的样本数
    out (Tensor, optional) – 结果张量
    

    例子:

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

    torch.ones

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

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

    参数:

    sizes (int...) – 整数序列,定义了输出形状
    out (Tensor, optional) – 结果张量
    

    例子:

    >>> torch.ones(2, 3)
     1  1  1
     1  1  1
    [torch.FloatTensor of size 2x3]
    
    >>> torch.ones(5)
    
     1
     1
     1
     1
     1
    [torch.FloatTensor of size 5]
    

    torch.rand

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

    返回一个张量,包含了从区间[0,1)的均匀分布中抽取的一组随机数,形状由可变参数sizes 定义。

    参数:

    • sizes (int...) – 整数序列,定义了输出形状
    • out (Tensor, optinal) - 结果张量 例子:
    >>> torch.rand(4)
    
     0.9193
     0.3347
     0.3232
     0.7715
    [torch.FloatTensor of size 4]
    
    >>> torch.rand(2, 3)
    
     0.5010  0.5140  0.0719
     0.1435  0.5636  0.0538
    [torch.FloatTensor of size 2x3]
    

    torch.randn

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

    返回一个张量,包含了从标准正态分布(均值为0,方差为 1,即高斯白噪声)中抽取一组随机数,形状由可变参数sizes定义。 参数:

    • sizes (int...) – 整数序列,定义了输出形状
    • out (Tensor, optinal) - 结果张量

    例子::

    >>> torch.randn(4)
    
    -0.1145
     0.0094
    -1.1717
     0.9846
    [torch.FloatTensor of size 4]
    
    >>> torch.randn(2, 3)
    
     1.4339  0.3351 -1.0999
     1.5458 -0.9643 -0.3558
    [torch.FloatTensor of size 2x3]
    

    torch.randperm

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

    给定参数n,返回一个从0 到n -1 的随机整数排列。

    参数:

    n (int) – 上边界(不包含)
    

    例子:

    >>> torch.randperm(4)
    
     2
     1
     3
     0
    [torch.LongTensor of size 4]
    

    torch.arange

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

    返回一个1维张量,长度为 floor((end−start)/step)。包含从start到end,以step为步长的一组序列值(默认步长为1)。

    参数:

    start (float) – 序列的起始点
    end (float) – 序列的终止点
    step (float) – 相邻点的间隔大小
    out (Tensor, optional) – 结果张量
    

    例子:

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

    orch.range

    torch.range(start, end, step=1, out=None) → Tensor
    

    返回一个1维张量,有 floor((end−start)/step)+1 个元素。包含在半开区间[start, end)从start开始,以step为步长的一组值。 step 是两个值之间的间隔,即 xi+1=xi+step

    警告:建议使用函数 torch.arange()

    参数:

    start (float) – 序列的起始点
    end (float) – 序列的最终值
    step (int) – 相邻点的间隔大小
    out (Tensor, optional) – 结果张量
    

    例子:

    >>> torch.range(1, 4)
    
     1
     2
     3
     4
    [torch.FloatTensor of size 4]
    
    >>> torch.range(1, 4, 0.5)
    
     1.0000
     1.5000
     2.0000
     2.5000
     3.0000
     3.5000
     4.0000
    [torch.FloatTensor of size 7]
    

    torch.zeros

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

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

    参数:

    • sizes (int...) – 整数序列,定义了输出形状
    • out (Tensor, optional) – 结果张量

    例子:

    >>> torch.zeros(2, 3)
    
     0  0  0
     0  0  0
    [torch.FloatTensor of size 2x3]
    
    >>> torch.zeros(5)
    
     0
     0
     0
     0
     0
    [torch.FloatTensor of size 5]
    
    

    相关文章

      网友评论

          本文标题:[PyTorch中文文档]-Package参考-torch-创建

          本文链接:https://www.haomeiwen.com/subject/dxmuvltx.html