In place operation causes RuntimeError

The following code causes a RuntimeError ’ unsupported operation: more than one element of the written-to tensor refers to a single memory location. Please clone() the tensor before performing the operation’. Could anyone help me?

import torch
kx = torch.linspace(-64, 63, 128)
kY0, kX0 = torch.meshgrid(kx, kx)
kX0 *= 0.001
kY0 *= 0.001

kY0 and kX0 are expanded tensors and as the error message suggest, you could need to clone them before applying inplace operations.
Did you try to use the suggested operation?

kx = torch.linspace(-64, 63, 128)
kY0, kX0 = torch.meshgrid(kx, kx)
kX0 = kX0.contiguous()
kX0 *= 0.001
kY0 = kY0.clone()
kY0 *= 0.001

Thanks for your suggestion. I thought the meshgrid function is the same as the one in numpy, so I didn’t figure out why I has to clone the tensor first. Now I get it.