Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normal distribution sampling in pytorch-lightning

In Pytorch-Lightning you usually never have to specify cuda or gpu. But when I want to create a gaussian sampled Tensor using torch.normal I get

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

so, how do I have to change torch.normal such that pytorch-lightning works properly? Since I use the code on different machines on cpu and on gpu

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

lights = torch.normal(0, 1, size=[100, 3])
lights += centers
like image 881
cagcoach Avatar asked Jun 27 '26 15:06

cagcoach


1 Answers

The recommended way is to do lights = torch.normal(0, 1, size=[100, 3], device=self.device) if this is inside lightning class. You could also do: lights = torch.normal(0, 1, size=[100, 3]).type_as(tensor), where tensor is some tensor which is on cuda.

like image 66
Andrey Lukyanenko Avatar answered Jul 01 '26 03:07

Andrey Lukyanenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!