我有一个图片的张量,想从中随机 Select .我在找相当于np.random.choice()
美元的.
import torch
pictures = torch.randint(0, 256, (1000, 28, 28, 3))
假设我想要10张这样的照片.
我有一个图片的张量,想从中随机 Select .我在找相当于np.random.choice()
美元的.
import torch
pictures = torch.randint(0, 256, (1000, 28, 28, 3))
假设我想要10张这样的照片.
torch
没有与np.random.choice()
等效的实现,参见讨论here.另一种方法是使用混合索引或随机整数进行索引.
pictures[torch.randint(len(pictures), (10,))]
indices = torch.randperm(len(pictures))[:10]
pictures[indices]
阅读更多关于torch.randint
和torch.randperm
的内容.第二个代码片段的灵感来自PyTorch论坛中的post.