File size: 832 Bytes
347b44e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import math
import torch
## [-1,1]
def tensor2log(x):
a = (math.e - 1.) / 2.
b = (math.e + 1.) / 2.
x = a * x + b
return torch.log(x).float()
def log2tensor(x):
a = 2. / (math.e - 1.)
b = (math.e + 1.) / (1. - math.e)
x = torch.exp(x)
x = a * x + b
return x.float()
## [0,1]
def _tensor2log(x):
a = math.e - 1.
b = 1.
x = a * x + b
return torch.log(x).float()
def _log2tensor(x):
a = 1. / (math.e - 1.)
b = -a
x = torch.exp(x)
x = a * x + b
return x.float()
if __name__ == '__main__':
inputx = torch.rand(1, 3, 64, 64)
print(torch.min(inputx), torch.max(inputx))
out = _tensor2log(inputx)
print(torch.min(out), torch.max(out))
out = _log2tensor(out)
print(torch.min(out), torch.max(out))
print(torch.mean(out - inputx))
|