Calculating logarithm of non-zero values in pytorch

I have a tensor which contains zero and non-zero values. I want replace every non-zero value with its logarithm. Is there any simple way?
Thanks

This code should work:

x = torch.arange(0, 10, dtype=torch.float)
idx = x!=0
x[idx] = torch.log(x[idx])
1 Like

Thanks @ptrblck :slight_smile: