我是靠谱客的博主 缓慢黄蜂,这篇文章主要介绍Pytorch 数学计算torch.pow(input, exponent, *, out=None) → Tensor,现在分享给大家,希望可以做个参考。

1. 加法运算

torch.add(input, other, *, out=None) → Tensor

将标量 other 与input输入的每个元素相加并返回一个新的结果张量。

out=input+other

如果 input 是 Fl​​oatTensor 或 DoubleTensor 类型,则 other 必须是实数,否则应该是整数。

参数

  • input (Tensor) – the input tensor.

  • other (Number) – the number to be added to each element of input

关键字参数

  • out (Tensor, optional) – the output tensor.

举例:

复制代码
1
2
3
4
5
6
7
8
9
10
>>> a = torch.randn(4) >>> a tensor([ 0.0202, 1.0985, 1.3506, -0.6056]) >>> torch.add(a, 20) tensor([ 20.0202, 21.0985, 21.3506, 19.3944])

2. 减法运算

torch.sub(input, other, *, alpha=1, out=None) → Tensor

从输入中减去按 alpha 缩放的 other。

 支持广播(broadcasting)到相同的shape、类型转换以及整数、浮点数和复数输入。

 参数

  •  input (Tensor) – the input tensor.
  • other (Tensor or Scalar) – the tensor or scalar to subtract from input

关键字参数

  • alpha (Scalar) – the scalar multiplier for other

  • out (Tensor, optional) – the output tensor.

举例:

复制代码
1
2
3
4
>>> a = torch.tensor((1, 2)) >>> b = torch.tensor((0, 1)) >>> torch.sub(a, b, alpha=2) tensor([1, 0])

3. 哈达玛积(element wise,对应元素相乘)

torch.mul(input, other, *, out=None) → Tensor

将输入input的每个元素与标量other元素相乘,并返回一个新的结果张量。

如果输入类型为 FloatTensor 或 DoubleTensor,则other应为实数,否则应为整数

参数

  •  input (Tensor) – the input tensor.
  • other (Number) – the number to be multiplied to each element of input

关键字参数

  • out (Tensor, optional) – the output tensor.

举例:

复制代码
1
2
3
4
5
6
7
8
9
>>> a = torch.randn(3) >>> a tensor([ 0.2015, -0.4255, 2.6087]) >>> torch.mul(a, 100) tensor([ 20.1494, -42.5491, 260.8663])

torch.mul(input, other, *, out=None) → Tensor

张量input的每个元素都乘以张量 other 的相应元素。返回运算结果的张量。

Input和other的shape必须是可以自动扩张的(broadcastable)。

参数

  •  input (Tensor) – 第一个被乘数张量
  • other (Tensor) – 第二个被乘数张量

关键字参数

  • out (Tensor, optional) – 输出张量.

举例:

复制代码
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.randn(4, 1) >>> a tensor([[ 1.1207], [-0.3137], [ 0.0700], [ 0.8378]]) >>> b = torch.randn(1, 4) >>> b tensor([[ 0.5146, 0.1216, -0.5244, 2.2382]]) >>> torch.mul(a, b) tensor([[ 0.5767, 0.1363, -0.5877, 2.5083], [-0.1614, -0.0382, 0.1645, -0.7021], [ 0.0360, 0.0085, -0.0367, 0.1567], [ 0.4312, 0.1019, -0.4394, 1.8753]])

4. 除法运算

torch.div(input, other, *, rounding_mode=None, out=None) → Tensor

将输入input的每个元素除以other的对应元素。

支持广播(broadcasting)到相同的shape、类型转换以及整数、浮点数和复数输入。

参数

  •  input (Tensor) – 被除数
  • other (Tensor or Number) – 除数

关键字参数

  • rounding_mode (str, optional) – 应用于结果的舍入类型
复制代码
1
2
3
4
5
1. None 默认行为。不执行舍入,如果输入和其他都是整数类型,则将输入转化为默认标量类型。相当于   Python 中的真正除法(/ 运算符)和 NumPy 的 np.true_divide 2. “trunc” - 将除法结果向零舍入。相当于 C 风格的整数除法。 3. “floor” - 将除法结果向下舍入。相当于 Python 中的楼层除法(// 运算符)和 NumPy 的 np.floor_divide。
  • out (Tensor, optional) – 输出张量.

举例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
>>> x = torch.tensor([ 0.3810, 1.2774, -0.2972, -0.3719, 0.4637]) >>> torch.div(x, 0.5) tensor([ 0.7620, 2.5548, -0.5944, -0.7438, 0.9274]) >>> a = torch.tensor([[-0.3711, -1.9353, -0.4605, -0.2917], ... [ 0.1815, -1.0111, 0.9805, -1.5923], ... [ 0.1062, 1.4581, 0.7759, -1.2344], ... [-0.1830, -0.0313, 1.1908, -1.4757]]) >>> b = torch.tensor([ 0.8032, 0.2930, -0.8113, -0.2308]) >>> torch.div(a, b) tensor([[-0.4620, -6.6051, 0.5676, 1.2639], [ 0.2260, -3.4509, -1.2086, 6.8990], [ 0.1322, 4.9764, -0.9564, 5.3484], [-0.2278, -0.1068, -1.4678, 6.3938]]) >>> torch.div(a, b, rounding_mode='trunc') tensor([[-0., -6., 0., 1.], [ 0., -3., -1., 6.], [ 0., 4., -0., 5.], [-0., -0., -1., 6.]]) >>> torch.div(a, b, rounding_mode='floor') tensor([[-1., -7., 0., 1.], [ 0., -4., -2., 6.], [ 0., 4., -1., 5.], [-1., -1., -2., 6.]])

5. 矩阵乘法

torch.matmul(input, other, *, out=None) → Tensor

两个张量的矩阵乘积,行为取决于张量的维度,如下所示:

  • 如果两个张量都是一维的,则返回点积(对应元素相乘再累加,从而得到一个标量)这种形况需要两个一维矩阵的size是一致的。
  • 如果两个参数都是二维的,则返回矩阵-矩阵乘积。
  • 如果第一个参数是一维的,而第二个参数是二维的,则为了矩阵乘法的目的,在其维度前面加上 1。在矩阵乘法之后,前置维度被删除。
  • 如果第一个参数是二维的,第二个参数是一维的,则返回矩阵向量乘积。
  • 如果两个参数都至少是一维的并且至少一个参数是 N 维的(其中 N > 2),则返回批处理(batched)矩阵乘法。如果第一个参数是一维的,为了批量(batched)矩阵乘法的目的,在它的维度前面加上 1 并在之后删除。如果第二个参数是一维的,则为了批处理(batched)矩阵乘法的目的,将 1 附加到其维度并在之后删除。非矩阵(即批量)维度被广播(因此必须是可广播的)。例如,如果输入是一个 张量,其他是一个 张量,输出将是张量。请注意,广播逻辑在确定输入是否可广播时仅查看批次维度,而不是矩阵维度。例如,如果输入是一个 (j×1×n×m)(j times 1 times n times m)(j×1×n×m) 张量,其他是一个 (k×m×p)( k times m times p)(k×m×p) 张量,即使最后两个维度(即矩阵维度)不同,这些输入对于广播也是有效的。 out 将是一个 (j×k×n×p)(j times k times n times p)(j×k×n×p) 张量。

此运算符支持 TensorFloat32。

参数

  • input (Tensor) – 第一个被乘数张量

  • other (Tensor) – 第二个被乘数张量

关键字参数

out (Tensor, optional) – the output tensor.

举例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
>>> # vector x vector >>> tensor1 = torch.randn(3) >>> tensor2 = torch.randn(3) >>> torch.matmul(tensor1, tensor2).size() torch.Size([]) >>> # matrix x vector >>> tensor1 = torch.randn(3, 4) >>> tensor2 = torch.randn(4) >>> torch.matmul(tensor1, tensor2).size() torch.Size([3]) >>> # batched matrix x broadcasted vector >>> tensor1 = torch.randn(10, 3, 4) >>> tensor2 = torch.randn(4) >>> torch.matmul(tensor1, tensor2).size() torch.Size([10, 3]) >>> # batched matrix x batched matrix >>> tensor1 = torch.randn(10, 3, 4) >>> tensor2 = torch.randn(10, 4, 5) >>> torch.matmul(tensor1, tensor2).size() torch.Size([10, 3, 5]) >>> # batched matrix x broadcasted matrix >>> tensor1 = torch.randn(10, 3, 4) >>> tensor2 = torch.randn(4, 5) >>> torch.matmul(tensor1, tensor2).size() torch.Size([10, 3, 5])

6. 幂运算

torch.pow(input, exponent, *, out=None) → Tensor

复制代码
1
用指数exponent计算输入input中每​​个元素的幂,并返回一个带有计算结果的张量。指数可以是单个浮点数或具有与输入相同数量的元素的张量。
  • 复制代码
    1
    当指数是标量值时,应用的操作是:

  • 复制代码
    1
    当指数是张量时,应用的操作是:

 当指数是张量时,输入和指数的形状必须是可自动扩展(broadcastable)的。

参数

  • input (Tensor) – input 张量.

  • exponent (float or tensor) – 指数值

关键字参数

out (Tensor, optional) – the output tensor.

举例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
>>> a = torch.randn(4) >>> a tensor([ 0.4331, 1.2475, 0.6834, -0.2791]) >>> torch.pow(a, 2) tensor([ 0.1875, 1.5561, 0.4670, 0.0779]) >>> exp = torch.arange(1., 5.) >>> a = torch.arange(1., 5.) >>> a tensor([ 1., 2., 3., 4.]) >>> exp tensor([ 1., 2., 3., 4.]) >>> torch.pow(a, exp) tensor([ 1., 4., 27., 256.])

torch.pow(self, exponent, *, out=None) → Tensor

self 是标量浮点值,指数exponent是张量。返回的张量与指数exponent形状相同

应用的操作是:

参数

  • self (float) – self 是幂运算的标量基值

  • exponent (Tensor) – 指数 exponent 张量

关键字参数

out (Tensor, optional) – the output tensor.

举例:

复制代码
1
2
3
4
5
6
7
8
9
10
>>> exp = torch.arange(1., 5.) >>> exp tensor([1., 2., 3., 4.]) >>> base = 2 >>> torch.pow(base, exp) tensor([ 2., 4., 8., 16.])

7. 开方运算

torch.sqrt(input, *, out=None) → Tensor

返回具有输入input元素平方根的新张量。

参数

  • input (Tensor) – input 张量.

关键字参数

out (Tensor, optional) – the output tensor.

举例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
>>> a = torch.randn(4) >>> a tensor([-2.0755, 1.0226, 0.0831, 0.4806]) >>> torch.sqrt(a) tensor([ nan, 1.0112, 0.2883, 0.6933])

torch.rsqrt(input, *, out=None) → Tensor

返回一个新的张量,它是输入的每个元素的平方根的倒数。

参数

  • input (Tensor) – input 张量.

关键字参数

out (Tensor, optional) – the output tensor.

举例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
>>> a = torch.randn(4) >>> a tensor([-0.0370, 0.2970, 1.5420, -0.9105]) >>> torch.rsqrt(a) tensor([ nan, 1.8351, 0.8053, nan])

8.指数与对数运算

torch.exp(input, *, out=None) → Tensor

返回具有输入张量 input 元素的指数的新张量。

参数

  • input (Tensor) – input 张量.

关键字参数

out (Tensor, optional) – the output tensor.

举例:

复制代码
1
2
3
>>> torch.exp(torch.tensor([0, math.log(2.)])) tensor([ 1., 2.])

torch.log(input, *, out=None) → Tensor

返回具有输入元素的自然对数的新张量。

参数

  • input (Tensor) – input 张量.

关键字参数

out (Tensor, optional) – the output tensor.

举例:

复制代码
1
2
3
4
5
6
import torch a = torch.exp(torch.ones(2, 2)) # 得到2*2的全是e的Tensor print(a) print(torch.log(a)) # 取自然对数

输出结果:

复制代码
1
2
3
4
tensor([[2.7183, 2.7183], [2.7183, 2.7183]]) tensor([[1., 1.], [1., 1.]])

9.近似值运算

torch.round(input, *, out=None) → Tensor

返回一个新的张量,其中输入的每个元素都四舍五入为最接近的整数。

参数

  • input (Tensor) – input 张量.

关键字参数

out (Tensor, optional) – the output tensor.

举例:

复制代码
1
2
3
4
5
6
7
8
9
>>> a = torch.randn(4) >>> a tensor([ 0.9920, 0.6077, 0.9734, -1.0362]) >>> torch.round(a) tensor([ 1., 1., 1., -1.])

最后

以上就是缓慢黄蜂最近收集整理的关于Pytorch 数学计算torch.pow(input, exponent, *, out=None) → Tensor的全部内容,更多相关Pytorch内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(113)

评论列表共有 0 条评论

立即
投稿
返回
顶部