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