用火炬随机选择? [英] Random Choice with Pytorch?

查看:63
本文介绍了用火炬随机选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张张量的图片,想从中随机选择.我正在寻找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.

  1. 生成 n 个随机索引
  2. 使用这些索引来索引原始张量
  1. Generate n random indices
  2. Index your original tensor with these indices

pictures[torch.randint(len(pictures), (10,))]  

要做到这一点,无需替换:

To do it without replacement:

  1. 随机播放索引
  2. 采用 n 首个元素
  1. Shuffle the index
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆