mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-10 04:22:38 +02:00
transformations are singletons now, weak refs for memory managment
This commit is contained in:
parent
97787152e9
commit
645aa0f420
1 changed files with 21 additions and 18 deletions
|
|
@ -5,15 +5,15 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from GPy.core.domains import POSITIVE, NEGATIVE, BOUNDED
|
from GPy.core.domains import POSITIVE, NEGATIVE, BOUNDED
|
||||||
import sys
|
import sys
|
||||||
|
import weakref
|
||||||
lim_val = -np.log(sys.float_info.epsilon)
|
lim_val = -np.log(sys.float_info.epsilon)
|
||||||
|
|
||||||
class Transformation(object):
|
class Transformation(object):
|
||||||
domain = None
|
domain = None
|
||||||
_instance = None
|
_instance = None
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
if not cls._instance:
|
if not cls._instance or cls._instance.__class__ is not cls:
|
||||||
cls._instance = super(Transformation, cls).__new__(
|
cls._instance = super(Transformation, cls).__new__(cls, *args, **kwargs)
|
||||||
cls, *args, **kwargs)
|
|
||||||
return cls._instance
|
return cls._instance
|
||||||
def f(self, x):
|
def f(self, x):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
@ -62,14 +62,16 @@ class LogexpClipped(Logexp):
|
||||||
log_max_bound = np.log(max_bound)
|
log_max_bound = np.log(max_bound)
|
||||||
log_min_bound = np.log(min_bound)
|
log_min_bound = np.log(min_bound)
|
||||||
domain = POSITIVE
|
domain = POSITIVE
|
||||||
|
_instances = []
|
||||||
def __new__(cls, lower=1e-6, *args, **kwargs):
|
def __new__(cls, lower=1e-6, *args, **kwargs):
|
||||||
if not cls._instance:
|
if cls._instances:
|
||||||
cls._instance = super(Transformation, cls).__new__(
|
cls._instances[:] = [instance for instance in cls._instances if instance()]
|
||||||
cls, lower, *args, **kwargs)
|
for instance in cls._instances:
|
||||||
elif cls._instance.lower == lower:
|
if instance().lower == lower:
|
||||||
return cls._instance
|
return instance()
|
||||||
else:
|
o = super(Transformation, cls).__new__(cls, lower, *args, **kwargs)
|
||||||
return super(Transformation, cls).__new__(cls, lower, *args, **kwargs)
|
cls._instances.append(weakref.ref(o))
|
||||||
|
return cls._instances[-1]()
|
||||||
def __init__(self, lower=1e-6):
|
def __init__(self, lower=1e-6):
|
||||||
self.lower = lower
|
self.lower = lower
|
||||||
def f(self, x):
|
def f(self, x):
|
||||||
|
|
@ -136,19 +138,20 @@ class Square(Transformation):
|
||||||
|
|
||||||
class Logistic(Transformation):
|
class Logistic(Transformation):
|
||||||
domain = BOUNDED
|
domain = BOUNDED
|
||||||
|
_instances = []
|
||||||
def __new__(cls, lower=1e-6, upper=1e-6, *args, **kwargs):
|
def __new__(cls, lower=1e-6, upper=1e-6, *args, **kwargs):
|
||||||
if not cls._instance:
|
if cls._instances:
|
||||||
cls._instance = super(Transformation, cls).__new__(
|
cls._instances[:] = [instance for instance in cls._instances if instance()]
|
||||||
cls, *args, **kwargs)
|
for instance in cls._instances:
|
||||||
elif cls._instance.lower == lower and cls._instance.upper == upper:
|
if instance().lower == lower and instance().upper == upper:
|
||||||
return cls._instance
|
return instance()
|
||||||
else:
|
o = super(Transformation, cls).__new__(cls, lower, upper, *args, **kwargs)
|
||||||
return super(Transformation, cls).__new__(cls, *args, **kwargs)
|
cls._instances.append(weakref.ref(o))
|
||||||
|
return cls._instances[-1]()
|
||||||
def __init__(self, lower, upper):
|
def __init__(self, lower, upper):
|
||||||
assert lower < upper
|
assert lower < upper
|
||||||
self.lower, self.upper = float(lower), float(upper)
|
self.lower, self.upper = float(lower), float(upper)
|
||||||
self.difference = self.upper - self.lower
|
self.difference = self.upper - self.lower
|
||||||
return self
|
|
||||||
def f(self, x):
|
def f(self, x):
|
||||||
return self.lower + self.difference / (1. + np.exp(-x))
|
return self.lower + self.difference / (1. + np.exp(-x))
|
||||||
def finv(self, f):
|
def finv(self, f):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue