Created
July 30, 2019 09:16
-
-
Save goddoe/381a1cd1bdd66bc541aeda2d89f51bc2 to your computer and use it in GitHub Desktop.
repeat vs expand pytorch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
A = torch.randn([12, 9, 64]) | |
B = torch.randn([12, 9, 64]) | |
Ar = A.repeat(1, 1, 9).view(12, 81, 64) | |
Br = B.repeat(1, 9, 1) | |
C = torch.cat((Ar, Br), dim=2) | |
D = torch.cat([A.unsqueeze(2).expand(-1, -1, 9, -1), | |
B.unsqueeze(1).expand(-1, 9, -1, -1)], dim=-1).view(12, 81, 128) | |
print ((C-D).abs().max().item()) # should be 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment