GPy/GPy/core/parameterization/transformations.py

184 lines
6.3 KiB
Python
Raw Normal View History

2013-05-01 17:11:13 +01:00
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
# Licensed under the BSD 3-clause license (see LICENSE.txt)
import numpy as np
from domains import _POSITIVE,_NEGATIVE, _BOUNDED
2014-02-07 15:16:05 +00:00
import sys
import weakref
2014-02-07 15:16:05 +00:00
_lim_val = -np.log(sys.float_info.epsilon)
2013-05-01 17:11:13 +01:00
#===============================================================================
# Fixing constants
__fixed__ = "fixed"
FIXED = False
UNFIXED = True
#===============================================================================
2013-09-04 09:30:47 +01:00
class Transformation(object):
2013-06-04 17:03:29 +01:00
domain = None
2013-09-20 17:21:28 +01:00
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance or cls._instance.__class__ is not cls:
cls._instance = super(Transformation, cls).__new__(cls, *args, **kwargs)
2013-09-20 17:21:28 +01:00
return cls._instance
2013-05-01 17:11:13 +01:00
def f(self, x):
raise NotImplementedError
2013-05-08 15:34:25 +01:00
def finv(self, x):
2013-05-01 17:11:13 +01:00
raise NotImplementedError
2013-05-08 15:34:25 +01:00
def gradfactor(self, f):
2013-05-01 17:11:13 +01:00
""" df_dx evaluated at self.f(x)=f"""
raise NotImplementedError
2013-05-08 15:34:25 +01:00
def initialize(self, f):
""" produce a sensible initial value for f(x)"""
2013-05-01 17:11:13 +01:00
raise NotImplementedError
def __str__(self):
raise NotImplementedError
2014-02-12 17:11:55 +00:00
def __repr__(self):
return self.__class__.__name__
2013-05-01 17:11:13 +01:00
2013-09-04 09:30:47 +01:00
class Logexp(Transformation):
domain = _POSITIVE
2013-05-08 15:34:25 +01:00
def f(self, x):
2014-02-13 13:53:49 +00:00
return np.where(x>_lim_val, x, np.log(1. + np.exp(np.clip(x, -np.inf, _lim_val))))
#raises overflow warning: return np.where(x>_lim_val, x, np.log(1. + np.exp(x)))
2013-05-08 15:34:25 +01:00
def finv(self, f):
return np.where(f>_lim_val, f, np.log(np.exp(f) - 1.))
2013-05-08 15:34:25 +01:00
def gradfactor(self, f):
return np.where(f>_lim_val, 1., 1 - np.exp(-f))
2013-05-08 15:34:25 +01:00
def initialize(self, f):
if np.any(f < 0.):
print "Warning: changing parameters to satisfy constraints"
2013-05-08 15:34:25 +01:00
return np.abs(f)
def __str__(self):
2013-10-11 16:46:12 +01:00
return '+ve'
2013-05-08 15:34:25 +01:00
2013-10-02 19:14:35 +01:00
class NegativeLogexp(Transformation):
domain = _NEGATIVE
2013-10-11 16:46:12 +01:00
logexp = Logexp()
def f(self, x):
2013-10-11 16:46:12 +01:00
return -self.logexp.f(x) # np.log(1. + np.exp(x))
def finv(self, f):
2013-10-11 16:46:12 +01:00
return self.logexp.finv(-f) # np.log(np.exp(-f) - 1.)
def gradfactor(self, f):
2013-10-11 16:46:12 +01:00
return -self.logexp.gradfactor(-f)
def initialize(self, f):
2013-10-11 16:46:12 +01:00
return -self.logexp.initialize(f) # np.abs(f)
def __str__(self):
2013-10-11 16:46:12 +01:00
return '-ve'
2014-02-12 17:11:55 +00:00
2013-09-04 09:30:47 +01:00
class LogexpClipped(Logexp):
max_bound = 1e100
2013-05-22 17:39:27 +01:00
min_bound = 1e-10
log_max_bound = np.log(max_bound)
log_min_bound = np.log(min_bound)
domain = _POSITIVE
_instances = []
2013-09-20 17:21:28 +01:00
def __new__(cls, lower=1e-6, *args, **kwargs):
if cls._instances:
cls._instances[:] = [instance for instance in cls._instances if instance()]
for instance in cls._instances:
if instance().lower == lower:
return instance()
o = super(Transformation, cls).__new__(cls, lower, *args, **kwargs)
cls._instances.append(weakref.ref(o))
return cls._instances[-1]()
def __init__(self, lower=1e-6):
2013-05-10 17:32:26 +01:00
self.lower = lower
2013-05-08 15:34:25 +01:00
def f(self, x):
exp = np.exp(np.clip(x, self.log_min_bound, self.log_max_bound))
f = np.log(1. + exp)
2013-05-22 17:16:10 +01:00
# if np.isnan(f).any():
# import ipdb;ipdb.set_trace()
2013-05-22 17:39:27 +01:00
return np.clip(f, self.min_bound, self.max_bound)
2013-05-08 15:34:25 +01:00
def finv(self, f):
return np.log(np.exp(f - 1.))
2013-05-08 15:34:25 +01:00
def gradfactor(self, f):
2013-05-20 10:11:27 +01:00
ef = np.exp(f) # np.clip(f, self.min_bound, self.max_bound))
2013-05-10 16:49:37 +01:00
gf = (ef - 1.) / ef
return gf # np.where(f < self.lower, 0, gf)
def initialize(self, f):
if np.any(f < 0.):
print "Warning: changing parameters to satisfy constraints"
2013-05-01 17:11:13 +01:00
return np.abs(f)
def __str__(self):
2013-10-11 16:46:12 +01:00
return '+ve_c'
2013-05-01 17:11:13 +01:00
2013-09-04 09:30:47 +01:00
class Exponent(Transformation):
# TODO: can't allow this to go to zero, need to set a lower bound. Similar with negative Exponent below. See old MATLAB code.
domain = _POSITIVE
2013-05-08 15:34:25 +01:00
def f(self, x):
return np.where(x<_lim_val, np.where(x>-_lim_val, np.exp(x), np.exp(-_lim_val)), np.exp(_lim_val))
2013-05-08 15:34:25 +01:00
def finv(self, x):
2013-05-01 17:11:13 +01:00
return np.log(x)
2013-05-08 15:34:25 +01:00
def gradfactor(self, f):
2013-05-01 17:11:13 +01:00
return f
2013-05-08 15:34:25 +01:00
def initialize(self, f):
if np.any(f < 0.):
print "Warning: changing parameters to satisfy constraints"
2013-05-01 17:11:13 +01:00
return np.abs(f)
def __str__(self):
2013-10-11 16:46:12 +01:00
return '+ve'
2013-05-01 17:11:13 +01:00
2013-09-04 09:30:47 +01:00
class NegativeExponent(Exponent):
domain = _NEGATIVE
2013-05-08 15:34:25 +01:00
def f(self, x):
2013-09-04 09:30:47 +01:00
return -Exponent.f(x)
def finv(self, f):
2013-09-04 09:30:47 +01:00
return Exponent.finv(-f)
2013-05-08 15:34:25 +01:00
def gradfactor(self, f):
2013-05-01 17:11:13 +01:00
return f
2013-05-08 15:34:25 +01:00
def initialize(self, f):
2013-09-04 09:30:47 +01:00
return -Exponent.initialize(f) #np.abs(f)
2013-05-01 17:11:13 +01:00
def __str__(self):
2013-10-11 16:46:12 +01:00
return '-ve'
2013-05-01 17:11:13 +01:00
2013-09-04 09:30:47 +01:00
class Square(Transformation):
domain = _POSITIVE
2013-05-08 15:34:25 +01:00
def f(self, x):
return x ** 2
def finv(self, x):
return np.sqrt(x)
def gradfactor(self, f):
return 2 * np.sqrt(f)
def initialize(self, f):
return np.abs(f)
def __str__(self):
2013-10-11 16:46:12 +01:00
return '+sq'
2013-05-08 15:34:25 +01:00
2013-09-04 09:30:47 +01:00
class Logistic(Transformation):
domain = _BOUNDED
_instances = []
2013-09-20 17:21:28 +01:00
def __new__(cls, lower=1e-6, upper=1e-6, *args, **kwargs):
if cls._instances:
cls._instances[:] = [instance for instance in cls._instances if instance()]
for instance in cls._instances:
if instance().lower == lower and instance().upper == upper:
return instance()
o = super(Transformation, cls).__new__(cls, lower, upper, *args, **kwargs)
cls._instances.append(weakref.ref(o))
return cls._instances[-1]()
2013-05-08 15:34:25 +01:00
def __init__(self, lower, upper):
2013-05-01 17:11:13 +01:00
assert lower < upper
self.lower, self.upper = float(lower), float(upper)
self.difference = self.upper - self.lower
2013-05-08 15:34:25 +01:00
def f(self, x):
return self.lower + self.difference / (1. + np.exp(-x))
def finv(self, f):
2013-05-01 17:11:13 +01:00
return np.log(np.clip(f - self.lower, 1e-10, np.inf) / np.clip(self.upper - f, 1e-10, np.inf))
2013-05-08 15:34:25 +01:00
def gradfactor(self, f):
return (f - self.lower) * (self.upper - f) / self.difference
def initialize(self, f):
if np.any(np.logical_or(f < self.lower, f > self.upper)):
print "Warning: changing parameters to satisfy constraints"
2014-02-07 15:16:05 +00:00
#return np.where(np.logical_or(f < self.lower, f > self.upper), self.f(f * 0.), f)
#FIXME: Max, zeros_like right?
return np.where(np.logical_or(f < self.lower, f > self.upper), self.f(np.zeros_like(f)), f)
2013-05-01 17:11:13 +01:00
def __str__(self):
2013-10-11 16:46:12 +01:00
return '{},{}'.format(self.lower, self.upper)
2013-05-01 17:11:13 +01:00
2014-02-07 15:16:05 +00:00
2013-09-20 17:21:28 +01:00