我是靠谱客的博主 凶狠抽屉,最近开发中收集的这篇文章主要介绍einsum和matmul的结果对比实验,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

程序:

import torch
import torch.nn as nn

t=torch.randn((2,3,2))
w=torch.randn((3,2))
einsum = torch.einsum('ijk,js->isk',t,w)
print(einsum.shape,'einsum shape')
print(einsum)

t_T=t.transpose(2,1)
result = torch.matmul(t_T,w).transpose(2,1)
print(result.shape,'matmul shape')
print(result)

运行结果:

torch.Size([2, 2, 2]) einsum shape
tensor([[[-2.2031, -1.1478],
         [ 0.7595,  1.3023]],

        [[ 1.1135,  1.0124],
         [-0.3292, -0.3777]]])
         
torch.Size([2, 2, 2]) matmul shape
tensor([[[-2.2031, -1.1478],
         [ 0.7595,  1.3023]],

        [[ 1.1135,  1.0124],
         [-0.3292, -0.3777]]])

程序:

t = torch.rand((2, 3, 4, 5))
c = nn.Parameter(torch.rand(3, 2))
h = nn.Parameter(torch.rand(4, 2))
w = nn.Parameter(torch.rand(5, 2))

einsum = torch.einsum('bchw,cm,hi,wj->bmij', t, c, h, w)
print(einsum.shape, 'einsum shape')
print(einsum)

t_T = t.permute(0, 2, 3, 1)
t_c = torch.matmul(t_T, c).permute(0,3, 2, 1)
t_h = torch.matmul(t_c, h).transpose(3,2)
t_w = torch.matmul(t_h,w)
print(t_w.shape,'matmul shape')
print(t_w)

运行结果:

torch.Size([2, 2, 2, 2]) einsum shape
tensor([[[[3.1744, 3.2492],
          [2.5228, 2.5705]],

         [[1.4115, 1.5763],
          [1.0781, 1.2177]]],


        [[[3.5836, 4.1190],
          [3.0216, 3.4974]],

         [[1.9285, 2.0623],
          [1.6165, 1.7142]]]], grad_fn=<ViewBackward>)
torch.Size([2, 2, 2, 2]) matmul shape
tensor([[[[3.1744, 3.2492],
          [2.5228, 2.5705]],

         [[1.4115, 1.5763],
          [1.0781, 1.2177]]],


        [[[3.5836, 4.1190],
          [3.0216, 3.4974]],

         [[1.9285, 2.0623],
          [1.6165, 1.7142]]]], grad_fn=<UnsafeViewBackward>)

最后

以上就是凶狠抽屉为你收集整理的einsum和matmul的结果对比实验的全部内容,希望文章能够帮你解决einsum和matmul的结果对比实验所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部