admin管理员组文章数量:1023782
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 badges1 Answer
Reset to default 1You 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])
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 badges1 Answer
Reset to default 1You 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])
本文标签: pytorchtorch matmul between 2D and 3D tensorStack Overflow
版权声明:本文标题:pytorch - torch matmul between 2D and 3D tensor - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745580239a2157265.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论