pytorch - torch matmul between 2D and 3D tensor - Stack Overflow

Hi I have two tensors:a = torch.randn(125, 128)# Shape: (125, 128)b = torch.randn(128, 8, 64)#

Hi I have two tensors:

a = torch.randn(125, 128)    # Shape: (125, 128)
b = torch.randn(128, 8, 64)  # Shape: (128, 8, 64)

I want the result has a shape of (125, 8, 64)

My first observation is: last dimension of a match the first dimension of b then I do:

result = torch.matmul(a,b)

It gave me the error:

Expected size for first two dimensions of batch2 tensor to be: [128, 128] but got: [128, 8].

How can I do this.

Edit: I also dont' want to reshape into 2D and then reshape the result into 3D again.

Hi I have two tensors:

a = torch.randn(125, 128)    # Shape: (125, 128)
b = torch.randn(128, 8, 64)  # Shape: (128, 8, 64)

I want the result has a shape of (125, 8, 64)

My first observation is: last dimension of a match the first dimension of b then I do:

result = torch.matmul(a,b)

It gave me the error:

Expected size for first two dimensions of batch2 tensor to be: [128, 128] but got: [128, 8].

How can I do this.

Edit: I also dont' want to reshape into 2D and then reshape the result into 3D again.

Share Improve this question edited Nov 19, 2024 at 5:42 Dinosaur asked Nov 19, 2024 at 5:37 DinosaurDinosaur 254 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can use an einsum

a = torch.randn(125, 128)    # Shape: (125, 128)
b = torch.randn(128, 8, 64)  # Shape: (128, 8, 64)
c = torch.einsum('ij,jkl->ikl', a, b)
print(c.shape)
> torch.Size([125, 8, 64])

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745580241a4634210.html

相关推荐

  • pytorch - torch matmul between 2D and 3D tensor - Stack Overflow

    Hi I have two tensors:a = torch.randn(125, 128)# Shape: (125, 128)b = torch.randn(128, 8, 64)#

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信