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))
|
||||
else:
|
||||
transformed_index = self._raveled_index_for(target_param)
|
||||
if self._has_fixes():
|
||||
if self._has_hidden():
|
||||
indices = np.r_[:self.size]
|
||||
which = (transformed_index[:, None] == indices[self._fixes_][None, :]).nonzero()
|
||||
transformed_index = (indices - (~self._fixes_).cumsum())[transformed_index[which[0]]]
|
||||
which = (transformed_index[:, None] == indices[self._exposed_][None, :]).nonzero()
|
||||
transformed_index = (indices - (~self._exposed_).cumsum())[transformed_index[which[0]]]
|
||||
|
||||
if transformed_index.size == 0:
|
||||
print "No free parameters to check"
|
||||
|
|
@ -324,11 +324,11 @@ class Model(Parameterized):
|
|||
transformed_index = param_index
|
||||
else:
|
||||
param_index = self._raveled_index_for(target_param)
|
||||
if self._has_fixes():
|
||||
if self._has_hidden():
|
||||
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]]
|
||||
transformed_index = (indices - (~self._fixes_).cumsum())[param_index]
|
||||
transformed_index = (indices - (~self._exposed_).cumsum())[param_index]
|
||||
# print param_index, transformed_index
|
||||
else:
|
||||
transformed_index = param_index
|
||||
|
|
|
|||
|
|
@ -515,13 +515,45 @@ class Indexable(Nameable, Observable):
|
|||
#===========================================================================
|
||||
# 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):
|
||||
plist = list(plist)
|
||||
plist.append(self)
|
||||
self._highest_parent_.ties.tie_together(plist)
|
||||
# self._highest_parent_._set_fixed(self,self._raveled_index())
|
||||
self._trigger_params_changed()
|
||||
|
||||
#===========================================================================
|
||||
# Constrain operations -> done
|
||||
|
|
@ -673,13 +705,10 @@ class OptimizationHandlable(Indexable):
|
|||
if not self._optimizer_copy_transformed:
|
||||
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__]
|
||||
if self.has_parent() and (self.constraints[__fixed__].size != 0 or self._has_ties()):
|
||||
fixes = np.ones(self.size).astype(bool)
|
||||
fixes[self.constraints[__fixed__]] = FIXED
|
||||
fixes[self._highest_parent_.ties.label_buf[self._highest_parent_._raveled_index_for(self)]] = FIXED
|
||||
return self._optimizer_copy_[fixes]
|
||||
elif self._has_fixes():
|
||||
return self._optimizer_copy_[np.logical_and(self._fixes_,self.ties.label_buf==0)]
|
||||
if self.has_parent() and self._has_hidden():
|
||||
return self._optimizer_copy_[self._exposed_]
|
||||
elif self._has_hidden():
|
||||
return self._optimizer_copy_[self._exposed_]
|
||||
|
||||
self._optimizer_copy_transformed = True
|
||||
|
||||
|
|
@ -694,11 +723,8 @@ class OptimizationHandlable(Indexable):
|
|||
Also we want to update param_array in here.
|
||||
"""
|
||||
f = None
|
||||
if self.has_parent() and self.constraints[__fixed__].size != 0:
|
||||
f = np.ones(self.size).astype(bool)
|
||||
f[self.constraints[__fixed__]] = FIXED
|
||||
elif self._has_fixes():
|
||||
f = self._fixes_
|
||||
if self._has_hidden():
|
||||
f = self._exposed_
|
||||
if f is None:
|
||||
self.param_array.flat = p
|
||||
[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()
|
||||
[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
|
||||
|
||||
@property
|
||||
|
|
@ -774,8 +800,8 @@ class OptimizationHandlable(Indexable):
|
|||
|
||||
def _get_param_names_transformed(self):
|
||||
n = self._get_param_names()
|
||||
if self._has_fixes():
|
||||
return n[self._fixes_]
|
||||
if self._has_hidden():
|
||||
return n[self._exposed_]
|
||||
return n
|
||||
|
||||
#===========================================================================
|
||||
|
|
|
|||
|
|
@ -68,10 +68,11 @@ class Tie(Parameterized):
|
|||
# The buffer keeps track of tie status
|
||||
self.label_buf = None
|
||||
self.buf_idx = None
|
||||
|
||||
self._untie_ = None
|
||||
|
||||
def mergeTies(self, p):
|
||||
"""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
|
||||
if p.ties.tied_param is not None:
|
||||
tie_labels = self._expand_tie_param(p.ties.tied_param.size)
|
||||
|
|
@ -173,10 +174,13 @@ class Tie(Parameterized):
|
|||
if self.tied_param is None:
|
||||
self.label_buf = None
|
||||
self.buf_idx = None
|
||||
self._untie_ = None
|
||||
else:
|
||||
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.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):
|
||||
"""tie a list of parameters"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue