mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-30 14:35:15 +02:00
replace _fixes_ _has_fixes_ for support of tie
This commit is contained in:
parent
59939bd50c
commit
a506f4a605
3 changed files with 55 additions and 25 deletions
|
|
@ -275,10 +275,10 @@ class Model(Parameterized):
|
||||||
transformed_index = range(len(x))
|
transformed_index = range(len(x))
|
||||||
else:
|
else:
|
||||||
transformed_index = self._raveled_index_for(target_param)
|
transformed_index = self._raveled_index_for(target_param)
|
||||||
if self._has_fixes():
|
if self._has_hidden():
|
||||||
indices = np.r_[:self.size]
|
indices = np.r_[:self.size]
|
||||||
which = (transformed_index[:, None] == indices[self._fixes_][None, :]).nonzero()
|
which = (transformed_index[:, None] == indices[self._exposed_][None, :]).nonzero()
|
||||||
transformed_index = (indices - (~self._fixes_).cumsum())[transformed_index[which[0]]]
|
transformed_index = (indices - (~self._exposed_).cumsum())[transformed_index[which[0]]]
|
||||||
|
|
||||||
if transformed_index.size == 0:
|
if transformed_index.size == 0:
|
||||||
print "No free parameters to check"
|
print "No free parameters to check"
|
||||||
|
|
@ -324,11 +324,11 @@ class Model(Parameterized):
|
||||||
transformed_index = param_index
|
transformed_index = param_index
|
||||||
else:
|
else:
|
||||||
param_index = self._raveled_index_for(target_param)
|
param_index = self._raveled_index_for(target_param)
|
||||||
if self._has_fixes():
|
if self._has_hidden():
|
||||||
indices = np.r_[:self.size]
|
indices = np.r_[:self.size]
|
||||||
which = (param_index[:, None] == indices[self._fixes_][None, :]).nonzero()
|
which = (param_index[:, None] == indices[self._exposed_][None, :]).nonzero()
|
||||||
param_index = param_index[which[0]]
|
param_index = param_index[which[0]]
|
||||||
transformed_index = (indices - (~self._fixes_).cumsum())[param_index]
|
transformed_index = (indices - (~self._exposed_).cumsum())[param_index]
|
||||||
# print param_index, transformed_index
|
# print param_index, transformed_index
|
||||||
else:
|
else:
|
||||||
transformed_index = param_index
|
transformed_index = param_index
|
||||||
|
|
|
||||||
|
|
@ -515,13 +515,45 @@ class Indexable(Nameable, Observable):
|
||||||
#===========================================================================
|
#===========================================================================
|
||||||
# Tie parameters together
|
# Tie parameters together
|
||||||
#===========================================================================
|
#===========================================================================
|
||||||
|
|
||||||
|
def _has_ties(self):
|
||||||
|
if self._highest_parent_.ties.label_buf is None:
|
||||||
|
return False
|
||||||
|
if self.has_parent():
|
||||||
|
return not np.all(self._highest_parent_.ties._untie_[self._highest_parent_._raveled_index_for(self)])
|
||||||
|
else:
|
||||||
|
return not np.all(self._highest_parent_.ties._untie_)
|
||||||
|
|
||||||
|
|
||||||
|
def _has_hidden(self):
|
||||||
|
if self.has_parent():
|
||||||
|
return self.constraints[__fixed__].size != 0 or self._has_ties()
|
||||||
|
else:
|
||||||
|
return self._has_fixes() or self._has_ties()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _exposed_(self):
|
||||||
|
if self.has_parent():
|
||||||
|
fixes = np.ones(self.size).astype(bool)
|
||||||
|
fixes[self.constraints[__fixed__]] = FIXED
|
||||||
|
fixes[np.logical_not(self._highest_parent_.ties._untie_[self._highest_parent_._raveled_index_for(self)])] = FIXED
|
||||||
|
return fixes
|
||||||
|
else:
|
||||||
|
hf = self._has_fixes()
|
||||||
|
ht = self._has_ties()
|
||||||
|
if hf and ht:
|
||||||
|
return np.logical_and(self._fixes_,self.ties._untie_)
|
||||||
|
elif hf and not ht:
|
||||||
|
return self._fixes_
|
||||||
|
elif not hf and ht:
|
||||||
|
return self.ties._untie_
|
||||||
|
else:
|
||||||
|
return np.ones((self._highest_parent_.param_array.size,),dtype=np.bool)
|
||||||
|
|
||||||
def tie_together(self, *plist):
|
def tie_together(self, *plist):
|
||||||
plist = list(plist)
|
plist = list(plist)
|
||||||
plist.append(self)
|
plist.append(self)
|
||||||
self._highest_parent_.ties.tie_together(plist)
|
self._highest_parent_.ties.tie_together(plist)
|
||||||
# self._highest_parent_._set_fixed(self,self._raveled_index())
|
|
||||||
self._trigger_params_changed()
|
|
||||||
|
|
||||||
#===========================================================================
|
#===========================================================================
|
||||||
# Constrain operations -> done
|
# Constrain operations -> done
|
||||||
|
|
@ -673,13 +705,10 @@ class OptimizationHandlable(Indexable):
|
||||||
if not self._optimizer_copy_transformed:
|
if not self._optimizer_copy_transformed:
|
||||||
self._optimizer_copy_.flat = self.param_array.flat
|
self._optimizer_copy_.flat = self.param_array.flat
|
||||||
[np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.iteritems() if c != __fixed__]
|
[np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.iteritems() if c != __fixed__]
|
||||||
if self.has_parent() and (self.constraints[__fixed__].size != 0 or self._has_ties()):
|
if self.has_parent() and self._has_hidden():
|
||||||
fixes = np.ones(self.size).astype(bool)
|
return self._optimizer_copy_[self._exposed_]
|
||||||
fixes[self.constraints[__fixed__]] = FIXED
|
elif self._has_hidden():
|
||||||
fixes[self._highest_parent_.ties.label_buf[self._highest_parent_._raveled_index_for(self)]] = FIXED
|
return self._optimizer_copy_[self._exposed_]
|
||||||
return self._optimizer_copy_[fixes]
|
|
||||||
elif self._has_fixes():
|
|
||||||
return self._optimizer_copy_[np.logical_and(self._fixes_,self.ties.label_buf==0)]
|
|
||||||
|
|
||||||
self._optimizer_copy_transformed = True
|
self._optimizer_copy_transformed = True
|
||||||
|
|
||||||
|
|
@ -694,11 +723,8 @@ class OptimizationHandlable(Indexable):
|
||||||
Also we want to update param_array in here.
|
Also we want to update param_array in here.
|
||||||
"""
|
"""
|
||||||
f = None
|
f = None
|
||||||
if self.has_parent() and self.constraints[__fixed__].size != 0:
|
if self._has_hidden():
|
||||||
f = np.ones(self.size).astype(bool)
|
f = self._exposed_
|
||||||
f[self.constraints[__fixed__]] = FIXED
|
|
||||||
elif self._has_fixes():
|
|
||||||
f = self._fixes_
|
|
||||||
if f is None:
|
if f is None:
|
||||||
self.param_array.flat = p
|
self.param_array.flat = p
|
||||||
[np.put(self.param_array, ind, c.f(self.param_array.flat[ind]))
|
[np.put(self.param_array, ind, c.f(self.param_array.flat[ind]))
|
||||||
|
|
@ -742,7 +768,7 @@ class OptimizationHandlable(Indexable):
|
||||||
"""
|
"""
|
||||||
self._highest_parent_.ties.collate_gradient()
|
self._highest_parent_.ties.collate_gradient()
|
||||||
[np.put(g, i, g[i] * c.gradfactor(self.param_array[i])) for c, i in self.constraints.iteritems() if c != __fixed__]
|
[np.put(g, i, g[i] * c.gradfactor(self.param_array[i])) for c, i in self.constraints.iteritems() if c != __fixed__]
|
||||||
if self._has_fixes(): return g[self._fixes_]
|
if self._has_hidden(): return g[self._exposed_]
|
||||||
return g
|
return g
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -774,8 +800,8 @@ class OptimizationHandlable(Indexable):
|
||||||
|
|
||||||
def _get_param_names_transformed(self):
|
def _get_param_names_transformed(self):
|
||||||
n = self._get_param_names()
|
n = self._get_param_names()
|
||||||
if self._has_fixes():
|
if self._has_hidden():
|
||||||
return n[self._fixes_]
|
return n[self._exposed_]
|
||||||
return n
|
return n
|
||||||
|
|
||||||
#===========================================================================
|
#===========================================================================
|
||||||
|
|
|
||||||
|
|
@ -68,10 +68,11 @@ class Tie(Parameterized):
|
||||||
# The buffer keeps track of tie status
|
# The buffer keeps track of tie status
|
||||||
self.label_buf = None
|
self.label_buf = None
|
||||||
self.buf_idx = None
|
self.buf_idx = None
|
||||||
|
self._untie_ = None
|
||||||
|
|
||||||
def mergeTies(self, p):
|
def mergeTies(self, p):
|
||||||
"""Merge the tie tree with another tie tree"""
|
"""Merge the tie tree with another tie tree"""
|
||||||
assert hasattr(p,'ties') and isinstance(p.ties,Tie)
|
assert hasattr(p,'ties') and isinstance(p.ties,Tie), str(type(p))
|
||||||
self.updates = False
|
self.updates = False
|
||||||
if p.ties.tied_param is not None:
|
if p.ties.tied_param is not None:
|
||||||
tie_labels = self._expand_tie_param(p.ties.tied_param.size)
|
tie_labels = self._expand_tie_param(p.ties.tied_param.size)
|
||||||
|
|
@ -173,10 +174,13 @@ class Tie(Parameterized):
|
||||||
if self.tied_param is None:
|
if self.tied_param is None:
|
||||||
self.label_buf = None
|
self.label_buf = None
|
||||||
self.buf_idx = None
|
self.buf_idx = None
|
||||||
|
self._untie_ = None
|
||||||
else:
|
else:
|
||||||
self.label_buf = np.zeros((self._highest_parent_.param_array.size,),dtype=np.uint32)
|
self.label_buf = np.zeros((self._highest_parent_.param_array.size,),dtype=np.uint32)
|
||||||
self._traverse_param(lambda x:np.put(self.label_buf,self._highest_parent_._raveled_index_for(x),x.tie), self._highest_parent_, [])
|
self._traverse_param(lambda x:np.put(self.label_buf,self._highest_parent_._raveled_index_for(x),x.tie), self._highest_parent_, [])
|
||||||
self.buf_idx = self._highest_parent_._raveled_index_for(self.tied_param)
|
self.buf_idx = self._highest_parent_._raveled_index_for(self.tied_param)
|
||||||
|
self._untie_ = self.label_buf==0
|
||||||
|
self._untie_[self.buf_idx] = True
|
||||||
|
|
||||||
def tie_together(self,plist):
|
def tie_together(self,plist):
|
||||||
"""tie a list of parameters"""
|
"""tie a list of parameters"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue