pytorch tensor内所有元素相乘实例

 更新时间:2022年07月16日 16:32:49   作者:某C姓工程师傅  
这篇文章主要介绍了pytorch tensor内所有元素相乘实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

tensor内所有元素相乘

a = torch.Tensor([1,2,3])
print(torch.prod(a))

输出 

tensor(6.)

tensor乘法运算汇总与解析

元素一一相乘

该操作又称作 “哈达玛积”, 简单来说就是 tensor 元素逐个相乘。这个操作,是通过 * 也就是常规的乘号操作符定义的操作结果。torch.mul 是等价的。

import torch
def element_by_element():
    
    x = torch.tensor([1, 2, 3])
    y = torch.tensor([4, 5, 6])
    
    return x * y, torch.mul(x, y)
element_by_element()
(tensor([ 4, 10, 18]), tensor([ 4, 10, 18]))

这个操作是可以 broad cast 的。

def element_by_element_broadcast():
    
    x = torch.tensor([1, 2, 3])
    y = 2
    
    return x * y
element_by_element_broadcast()
tensor([2, 4, 6])

向量点乘

torch.matmul: If both tensors are 1-dimensional, the dot product (scalar) is returned.

如果都是1维的,返回的就是 dot product 结果

def vec_dot_product():
    
    x = torch.tensor([1, 2, 3])
    y = torch.tensor([4, 5, 6])
    
    return torch.matmul(x, y)
vec_dot_product()
tensor(32)

矩阵乘法

torch.matmul: If both arguments are 2-dimensional, the matrix-matrix product is returned.

如果都是2维,那么就是矩阵乘法的结果返回。与 torch.mm 是等价的,torch.mm 仅仅能处理的是矩阵乘法。

def matrix_multiple():
    
    x = torch.tensor([
        [1, 2, 3],
        [4, 5, 6]
    ])
    y = torch.tensor([
        [7, 8],
        [9, 10],
        [11, 12]
    ])
    
    return torch.matmul(x, y), torch.mm(x, y)
matrix_multiple()
(tensor([[ 58,  64],
         [139, 154]]), tensor([[ 58,  64],
         [139, 154]]))

vector 与 matrix 相乘

torch.matmul: If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed.

如果第一个是 vector, 第二个是 matrix, 会在 vector 中增加一个维度。也就是 vector 变成了 与 matrix 相乘之后,变成 , 在结果中将 维 再去掉。

def vec_matrix():
    x = torch.tensor([1, 2, 3])
    y = torch.tensor([
        [7, 8],
        [9, 10],
        [11, 12]
    ])
    
    return torch.matmul(x, y)
vec_matrix()
tensor([58, 64])

matrix 与 vector 相乘

同样的道理, vector会被扩充一个维度。

def matrix_vec():
    x = torch.tensor([
        [1, 2, 3],
        [4, 5, 6]
    ])
    y = torch.tensor([
        7, 8, 9
    ])
    
    return torch.matmul(x, y)
matrix_vec()
tensor([ 50, 122])

带有batch_size 的 broad cast乘法

def batched_matrix_broadcasted_vector():
    x = torch.tensor([
        [
            [1, 2], [3, 4]
        ],
        [
            [5, 6], [7, 8]
        ]
    ])
    
    print(f"x shape: {x.size()} \n {x}")
    y = torch.tensor([1, 3])
    
    return torch.matmul(x, y)
batched_matrix_broadcasted_vector()
x shape: torch.Size([2, 2, 2]) 
 tensor([[[1, 2],
         [3, 4]],
        [[5, 6],
         [7, 8]]])
tensor([[ 7, 15],
        [23, 31]])
batched matrix x batched matrix
def batched_matrix_batched_matrix():
    x = torch.tensor([
        [
            [1, 2, 1], [3, 4, 4]
        ],
        [
            [5, 6, 2], [7, 8, 0]
        ]
    ])
    
    y = torch.tensor([
        [
            [1, 2], 
            [3, 4], 
            [5, 6]
        ],
        [
            [7, 8], 
            [9, 10], 
            [1, 2]
        ]
    ])
    
    print(f"x shape: {x.size()} \n y shape: {y.size()}")
    return torch.matmul(x, y)
xy = batched_matrix_batched_matrix()
print(f"xy shape: {xy.size()} \n {xy}")
x shape: torch.Size([2, 2, 3]) 
 y shape: torch.Size([2, 3, 2])
xy shape: torch.Size([2, 2, 2]) 
 tensor([[[ 12,  16],
         [ 35,  46]],
        [[ 91, 104],
         [121, 136]]])

上面的效果与 torch.bmm 是一样的。matmul 比 bmm 功能更加强大,但是 bmm 的语义非常明确, bmm 处理的只能是 3维的。

def batched_matrix_batched_matrix_bmm():
    x = torch.tensor([
        [
            [1, 2, 1], [3, 4, 4]
        ],
        [
            [5, 6, 2], [7, 8, 0]
        ]
    ])
    
    y = torch.tensor([
        [
            [1, 2], 
            [3, 4], 
            [5, 6]
        ],
        [
            [7, 8], 
            [9, 10], 
            [1, 2]
        ]
    ])
    
    print(f"x shape: {x.size()} \n y shape: {y.size()}")
    return torch.bmm(x, y)
xy = batched_matrix_batched_matrix()
print(f"xy shape: {xy.size()} \n {xy}")
x shape: torch.Size([2, 2, 3]) 
 y shape: torch.Size([2, 3, 2])
xy shape: torch.Size([2, 2, 2]) 
 tensor([[[ 12,  16],
         [ 35,  46]],
        [[ 91, 104],
         [121, 136]]])
tensordot
def tesnordot():
    x = torch.tensor([
        [1, 2, 1], 
        [3, 4, 4]])
    y = torch.tensor([
        [7, 8], 
        [9, 10], 
        [1, 2]])
    print(f"x shape: {x.size()}, y shape: {y.size()}")
    return torch.tensordot(x, y, dims=([0], [1]))
tesnordot()
x shape: torch.Size([2, 3]), y shape: torch.Size([3, 2])
tensor([[31, 39,  7],
        [46, 58, 10],
        [39, 49,  9]])

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python定义一个Actor任务

    Python定义一个Actor任务

    这篇文章主要介绍了Python定义一个Actor任务,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • pycharm 使用心得(三)Hello world!

    pycharm 使用心得(三)Hello world!

    作为PyCharm编辑器的起步,我们理所当然的先写一个Hello word,并运行它。(此文献给对IDE不熟悉的初学者)
    2014-06-06
  • pytorch中的卷积和池化计算方式详解

    pytorch中的卷积和池化计算方式详解

    今天小编就为大家分享一篇pytorch中的卷积和池化计算方式详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • Python中偏函数用法示例

    Python中偏函数用法示例

    这篇文章主要介绍了Python中偏函数用法,结合实例形式分析了Python基于functools模块创建和使用偏函数的相关操作技巧与注意事项,需要的朋友可以参考下
    2018-06-06
  • python3 Scrapy爬虫框架ip代理配置的方法

    python3 Scrapy爬虫框架ip代理配置的方法

    Scrapy是用python实现的一个为了爬取网站数据,提取结构性数据而编写的应用框架。使用Twisted高效异步网络框架来处理网络通信。这篇文章主要介绍了python3 Scrapy爬虫框架ip代理配置,需要的朋友可以参考下
    2020-01-01
  • python3 简单实现组合设计模式

    python3 简单实现组合设计模式

    这篇文章主要介绍了python3 简单实现组合设计模式的方法,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • 深入理解python虚拟机生成器停止背后原理

    深入理解python虚拟机生成器停止背后原理

    这篇文章主要介绍了python虚拟机生成器停止背后原理深入详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • python正则表达式查找和替换内容的实例详解

    python正则表达式查找和替换内容的实例详解

    在本篇文章里小编给大家整理的是一篇关于python正则表达式查找和替换内容的实例详解内容,有兴趣的朋友们可以跟着学习参考下。
    2021-10-10
  • Python 下载及安装详细步骤

    Python 下载及安装详细步骤

    这篇文章主要介绍了载及安装Python详细步骤,安装python分三个步骤,具体安装方法本文给大家介绍的非常详细,需要的朋友可以参考下
    2019-11-11
  • Python运算符的使用简单介绍

    Python运算符的使用简单介绍

    这篇文章主要介绍了Python运算符的使用简单介绍,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
    2022-08-08

最新评论