用火炬随机选择? [英] Random Choice with Pytorch?
本文介绍了用火炬随机选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张张量的图片,想从中随机选择.我正在寻找np.random.choice()
的等效项.
I have a tensor of pictures, and would like to randomly select from it. I'm looking for the equivalent of np.random.choice()
.
import torch
pictures = torch.randint(0, 256, (1000, 28, 28, 3))
假设我要这些照片中的10张.
Let's say I want 10 of these pictures.
推荐答案
torch
没有等效的np.random.choice()
实现.另一种方法是使用改组后的索引或随机整数进行索引.
torch
has no equivalent implementation of np.random.choice()
. The alternative is indexing with a shuffled index or random integers.
- 生成 n 个随机索引
- 使用这些索引来索引原始张量
- Generate n random indices
- Index your original tensor with these indices
pictures[torch.randint(len(pictures), (10,))]
要做到这一点,无需替换:
To do it without replacement:
- 随机播放索引
- 采用 n 首个元素
- Shuffle the index
- Take the n first elements
indices = torch.randperm(len(pictures))[:10]
pictures[indices]
详细了解 torch.randint
和 torch.randperm
.
Read more about torch.randint
and torch.randperm
.
这篇关于用火炬随机选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文