Take random sample of long tensor, create new subset

I have a long tensor with y dim vector per sample. I want to choose x random dimensions from y. I’m unsure who to do this. I’m new to Pytorch and Python. Thanks.

I have tried this:

random.sample(set(outputs2[0]), 10)

I’m wanting 10 random tensors from a 1000x1024 tensor (outputs2), it it’s giving me ‘10’ of them, but something is not quite correct, because when I take this result and put it back in my code path, it is not working.

[tensor(1.0000, device=‘cuda:0’, grad_fn=<SelectBackward>), tensor(-0.9909, device=‘cuda:0’, grad_fn=<SelectBackward>), tensor(0.9999, device=‘cuda:0’, grad_fn=<SelectBackward>), tensor(-0.9808, device=‘cuda:0’, grad_fn=<SelectBackward>), tensor(-1.0000, device=‘cuda:0’, grad_fn=<SelectBackward>), tensor(-0.9999, device=‘cuda:0’, grad_fn=<SelectBackward>), tensor(0.9999, device=‘cuda:0’, grad_fn=<SelectBackward>), tensor(1.0000, device=‘cuda:0’, grad_fn=<SelectBackward>), tensor(-1., device=‘cuda:0’, grad_fn=<SelectBackward>), tensor(-1.0000, device=‘cuda:0’, grad_fn=<SelectBackward>)]

Do you want to use them as weights in a model? In that case you have to wrap them in

torch.nn.Parameter( # your tensors here)

ouputs are from final layer and are of shape 1000x10. outputs 2 are pulled 2nd from last layer and are of shape 1000x1024. I want to randomly sample out of my outputs2 to get 1000x10.

I tried this, and it looks like tensors wrapped in a tensor…

I see. You could use

rand_columns = torch.randperm(1024)[:10]
random_sample = some_tensor[:, rand_columns]

Note that randperm will select 10 values out of the 1024 without replacement.

4 Likes

Thank you Sir, I will try it out.

David

That looks like it is doing what I was wanting. Thank you very much!