pytorch-学习笔记1

pytorch-学习笔记1

linspace

作用

将区间[start,end]按照固定的间隔均分成若点,返回一个一维tensor(包含这些线段的起点)

参数

torch.linspace(start, end, steps=100, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
start:线段起点
end:线段终点
steps:线段均分后的点数,默认为100(均分成100个点)
out:输出tensor

代码

1
2
3
4
5
6
7
8
9
10
11
torch.linspace(3, 10, 5)
tensor([3.0000, 4.7500, 6.5000, 8.2500, 10.0000])
//
torch.linspace(-10, 10, steps=5)
tensor([-10., -5., 0., 5., 10.])
//
torch.linspace(start=-10, end=10, steps=5)
tensor([-10., -5., 0., 5., 10.])
//
torch.linspace(start=-10, end=10, steps=1)
tensor([-10.])

norm

作用

返回tensor的p范数
p范数:\(||x||_p: (|x_1|^p + |x_2|^p + ···+|x_n|^p)^{\frac{1}{p}}\)
当P=1(1-范数):所有数字的绝对值之和
当P=2(2-范数):所有数字平方求和后开平方根,代表所有数字距离0点的距离

参数

torch.norm(input, p=’fro’, dim=None, keepdim=False, out=None, dtype=None)
input:输入tensor
p:范数值(int,float,inf,-inf),默认值为2。若p=float(‘inf’),返回的是最大数的绝对值,若若p=float(‘-inf’),返回的是最小数的绝对值
dim:若dim为1维,计算vector norm。dim=0代表计算矩阵每一列的p范数,dim=1代表计算矩阵每一行的p范数
若dim为2维,计算matrix norm
若dim为None,输入为一维,计算vector norm,输入为二维,计算matrix norm,超过二维,在最后一维度计算vector norm

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
a = torch.arange(9, dtype= torch.float) - 4
b = a.view((3, 3))
torch.norm(a)
tensor(7.7460)
\\
torch.norm(b)
tensor(7.7460)
\\
torch.norm(a, float('inf'))
tensor(4.)
\\
torch.norm(b, float('inf'))
tensor(4.)
\\
c = torch.tensor([[ 1, 2, 3],[-1, 1, 4]] , dtype= torch.float)
torch.norm(c, dim=0)
tensor([1.4142, 2.2361, 5.0000])
\\
torch.norm(c, dim=1)
tensor([3.7417, 4.2426])
\\
torch.norm(c, p=1, dim=1)
tensor([6., 6.])

numel

作用

返回tensor的元素个数

代码

1
2
3
4
5
6
7
a = torch.randn(1,2,3,4,5)
torch.numel(a)
120
\\
a = torch.zeros(4,4)
torch.numel(a)
16

eye

作用

返回对角线为1,其余为0的矩阵

参数

torch.eye(n, m, out=None)
若m为None,默认为n

ones,zeros,empty

作用

返回全1的tensor,返回全0的tensor,返回没有初始化的tensor

参数

torch.ones、zeros、empty(n, m, out=None)

from_numpy

作用

将np.ndarray转换为pytorch的tensor,返回后的tensor不能改变尺寸

代码

1
2
3
4
5
6
7
8
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])

rand

作用

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

参数

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

randn

作用

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

参数

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

randperm

作用

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

参数

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

range

作用

包含在半开区间[start, end),从start开始,以step为步长的一组值,step代表间隔。不包含末尾元素。

参数

torch.range(start=0, end, step=1, out=None) → Tensor
start默认为0,step默认为1

arange

作用

包含在区间[start, end],从start开始,以step为步长的一组值,step代表间隔。s包含末尾元素。

参数

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

文章目录
  1. 1. pytorch-学习笔记1
    1. 1.1. linspace
      1. 1.1.1. 作用
      2. 1.1.2. 参数
      3. 1.1.3. 代码
    2. 1.2. norm
      1. 1.2.1. 作用
      2. 1.2.2. 参数
      3. 1.2.3. 代码
    3. 1.3. numel
      1. 1.3.1. 作用
      2. 1.3.2. 代码
    4. 1.4. eye
      1. 1.4.1. 作用
      2. 1.4.2. 参数
    5. 1.5. ones,zeros,empty
      1. 1.5.1. 作用
      2. 1.5.2. 参数
    6. 1.6. from_numpy
      1. 1.6.1. 作用
      2. 1.6.2. 代码
    7. 1.7. rand
      1. 1.7.1. 作用
      2. 1.7.2. 参数
    8. 1.8. randn
      1. 1.8.1. 作用
      2. 1.8.2. 参数
    9. 1.9. randperm
      1. 1.9.1. 作用
      2. 1.9.2. 参数
    10. 1.10. range
      1. 1.10.1. 作用
      2. 1.10.2. 参数
    11. 1.11. arange
      1. 1.11.1. 作用
      2. 1.11.2. 参数
|