pytorch-lightning 中的正态分布采样

Normal distribution sampling in pytorch-lightning

Pytorch-Lightning 中,您通常不必指定 cuda 或 gpu。但是当我想使用 torch.normal 创建高斯采样张量时,我得到

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

那么,我该如何更改 torch.normal 才能使 pytorch-lightning 正常工作?由于我在 cpu 上的 cpu 和 上的不同机器上使用代码

centers = data["centers"] #already on GPU... sometimes...

lights = torch.normal(0, 1, size=[100, 3])
lights += centers

如果这是在闪电 class 内,推荐的方法是 lights = torch.normal(0, 1, size=[100, 3], device=self.device)。 您也可以这样做:lights = torch.normal(0, 1, size=[100, 3]).type_as(tensor),其中 tensor 是 cuda 上的一些张量。