Skip to content

Instantly share code, notes, and snippets.

@minhlab
Last active December 11, 2015 23:51
Show Gist options
  • Select an option

  • Save minhlab/7f989de1cb5e51de23ac to your computer and use it in GitHub Desktop.

Select an option

Save minhlab/7f989de1cb5e51de23ac to your computer and use it in GitHub Desktop.
Use maskedCopy to select elements in a tensor
th> mask=torch.ByteTensor(10, 3):random(2):add(-1)
[0.0001s]
th> mask
1 0 1
0 1 1
1 1 1
1 1 0
0 1 1
1 1 0
1 0 1
0 0 0
0 1 1
1 0 1
[torch.ByteTensor of size 10x3]
[0.0002s]
th> x = torch.Tensor(10,3):uniform()
[0.0001s]
th> x
0.2936 0.3001 0.7200
0.8701 0.9548 0.1079
0.4744 0.9558 0.2796
0.2058 0.2744 0.5256
0.1091 0.9623 0.6479
0.0420 0.7619 0.7688
0.4613 0.8468 0.9257
0.1426 0.7731 0.9501
0.8959 0.2475 0.7301
0.6407 0.2878 0.7172
[torch.DoubleTensor of size 10x3]
[0.0002s]
th> y=torch.Tensor(x:size()):maskedCopy(mask, x[mask])
[0.0001s]
th> y
0.2936 0.0000 0.7200
0.0000 0.9548 0.1079
0.4744 0.9558 0.2796
0.2058 0.2744 0.0000
0.0000 0.9623 0.6479
0.0420 0.7619 0.0000
0.4613 0.0000 0.9257
0.0000 0.0000 0.0000
0.0000 0.2475 0.7301
0.6407 0.0000 0.7172
[torch.DoubleTensor of size 10x3]
[0.0003s]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment