PyTorch 笔记(05)

您所在的位置:网站首页 如何算矩阵的模 PyTorch 笔记(05)

PyTorch 笔记(05)

2024-07-12 04:54| 来源: 网络整理| 查看: 265

1. 函数汇总

Tensor 的基本运算会对 tensor 的每一个元素进行操作,此类操作的输入与输出形状一致,常用操作见下表所示。 基本运算 对于很多操作,例如 div、mul、pow、fmod、等, PyTorch 都实现了运算符重载,所以可以直接使用运算符,如 a ** 2 等价于 torch.pow(a, 2), a *2 等价于 torch.mul(a, 2)。

2. 函数功能 2.1 torch.abs

将参数传递到 torch.abs 后返回输入参数的绝对值作为输出,输入参数必须是一个 Tensor 数据类型的变量。

import torch a = torch.randn(2, 3) print a b = torch.abs(a) print b

输出结果:

tensor([[ 0.4873, 0.8172, 0.2003], [ 0.6815, 1.5656, -0.8303]]) tensor([[0.4873, 0.8172, 0.2003], [0.6815, 1.5656, 0.8303]]) 2.2 torch.add

将参数传递到 torch.add 后返回输入参数的求和结果作为输出,输入参数既可以全部是 Tensor 数据类型的变量,也可以一个是 Tensor 数据类型的变量,另一个是标量。

a = torch.ones([2, 3]) print a b = torch.add(a, 10) print b c = torch.randn([2, 3]) print c d = torch.add(a, c) print d

输出结果:

tensor([[1., 1., 1.], [1., 1., 1.]]) tensor([[11., 11., 11.], [11., 11., 11.]]) tensor([[ 0.4276, 0.4618, -0.9449], [-1.4758, -1.3118, -0.2859]]) tensor([[ 1.4276, 1.4618, 0.0551], [-0.4758, -0.3118, 0.7141]]) 2.3 torch.clamp

对输入参数按照自定义的范围进行裁剪,最后将参数裁剪的结果作为输出。所以输入参数一共有三个,分别是需要进行裁剪的 Tensor 数据类型的变量、裁剪的上边界和裁剪的下边界。

具体的裁剪过程是:使用变量中的每个元素分别和裁剪的上边界及裁剪的下边界的值进行比较,如果元素的值小于裁剪的下边界的值,该元素就被重写成裁剪的下边界的值;同理,如果元素的值大于裁剪的上边界的值,该元素就被重写成裁剪的上边界的值。

a = torch.randn(2,3) print a b = torch.clamp(a, -0.1, 0.1) print b

输出结果:

tensor([[-1.0129, -1.3094, 0.4644], [ 1.0542, -0.5259, -0.3575]]) tensor([[-0.1000, -0.1000, 0.1000], [ 0.1000, -0.1000, -0.1000]]) 2.4 torch.div

将参数传递到 torch.div 后返回输入参数的求商结果作为输出,同样,参与运算的参数可以全部是 Tensor 数据类型的变量,也可以是 Tensor 数据类型的变量和标量的组合。

a = torch.IntTensor([2,4,6,8,10]) print a b = torch.div(a, 2) print b c = torch.IntTensor([2,4,6,8,10]) d = torch.div(a, c) print d

输出结果:

tensor([ 2, 4, 6, 8, 10], dtype=torch.int32) tensor([1, 2, 3, 4, 5], dtype=torch.int32) tensor([1, 1, 1, 1, 1], dtype=torch.int32) 2.5 torch.mul

将参数传递到 torch.mul 后返回输入参数求积的结果作为输出,参与运算的参数可以全部是 Tensor 数据类型的变量,也可以是 Tensor 数据类型的变量和标量的组合。

a = torch.IntTensor([2,4,6,8,10]) print a b = torch.mul(a, 2) print b c = torch.IntTensor([2,4,6,8,10]) d = torch.mul(a, c) print d

输出结果:

tensor([ 2, 4, 6, 8, 10], dtype=torch.int32) tensor([ 4, 8, 12, 16, 20], dtype=torch.int32) tensor([ 4, 16, 36, 64, 100], dtype=torch.int32) 2.6 torch.pow

将参数传递到 torch.pow 后返回输入参数的求幂结果作为输出,参与运算的参数可以全部是 Tensor 数据类型的变量,也可以是 Tensor 数据类型的变量和标量的组合。

a = torch.IntTensor([2,4,6,8,10]) print a b = torch.pow(a, 2) print b c = torch.IntTensor([3,3,3,3,3]) d = torch.pow(a, c) print d

输出结果:

tensor([ 2, 4, 6, 8, 10], dtype=torch.int32) tensor([ 4, 16, 36, 64, 100], dtype=torch.int32) tensor([ 8, 64, 216, 512, 1000], dtype=torch.int32) 2.7 torch.mm

将参数传递到 torch.mm 后返回输入参数的求积结果作为输出,不过这个求积的方式和之前的 torch.mul 运算方式不太一样, torch.mm 运用矩阵之间的乘法规则进行计算,所以被传入的参数会被当作矩阵进行处理,参数的维度自然也要满足矩阵乘法的前提条件,即前一个矩阵的行数必须和后一个矩阵的列数相等,否则不能进行计算。

a = torch.IntTensor([[1,2,3], [4,5,6]]) print a b = torch.IntTensor([[1,2], [4,5], [7, 8]]) print b c = torch.mm(a, b) print c

输出结果:

tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.int32) tensor([[1, 2], [4, 5], [7, 8]], dtype=torch.int32) tensor([[30, 36], [66, 81]], dtype=torch.int32) 2.8 torch.mv

将参数传递到 torch.mv 后返回输入参数的求积结果作为输出,torch.mv 运用矩阵与向量之间的乘法规则进行计算,被传入的参数中的第 1 个参数代表矩阵,第 2 个参数代表向量,顺序不能颠倒。

a = torch.IntTensor([[1,0,0], [0,0,1]]) print a b = torch.IntTensor([3,3,3]) print b c = torch.mv(a, b) print c

输出结果:

tensor([[1, 0, 0], [0, 0, 1]], dtype=torch.int32) tensor([3, 3, 3], dtype=torch.int32) tensor([3, 3], dtype=torch.int32)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3