Advertisement

Python deep learning with einsum and bmm in PyTorch

阅读量:

【python深度学习】——torch.einsum|torch.bmm

  • 1. 基础应用及实例说明
    • 2. torch.bmm 函数的使用

1. 基本用法与示例

基础应用方式:

复制代码
    torch.einsum(equation, *operands)
    
    
      
    
  • equation: 一种字符串形式,用于描述张量运算的结构。
    通过逗号将输入张量的索引进行分隔,随后使用箭头符号(->)连接,再标明输出张量的索引。

    • operands: 所需参与运算的张量对象。
      示例代码:
复制代码
    import torch
    A = torch.randn(2, 3)
    
    B = torch.einsum('ij->ji', A)
    # 等价于 B = A.transpose(0, 1)
    
    C = torch.einsum('ik,kj->ij', A, B)
    # 等价于 C = torch.matmul(A, B)
    
    a = torch.randn(3)
    b = torch.randn(3)
    c = torch.einsum('i,i->', a, b)
    # 等价于 c = torch

全部评论 (0)

还没有任何评论哟~