def learn(self): from scipy.optimize import fmin_powell as minimizer switches = np.abs(np.diff(self.obs)).nonzero()[0] try: if len(switches)>5: first_switch = self.tps[switches[0]] last_switch = self.tps[switches[-1]] else: first_switch = self.tps[0] last_switch = self.tps[-1] if first_switch>self.final_pivot_tps[0] and first_switch < self.final_pivot_tps[-1]: first_pivot = max(0, np.where(first_switch<=self.final_pivot_tps)[0][0] - self.extra_pivots) else: first_pivot=0 if last_switch<self.final_pivot_tps[-1] and last_switch>self.final_pivot_tps[0]: last_pivot = min(len(self.final_pivot_tps), np.where(last_switch>self.final_pivot_tps)[0][-1]+self.extra_pivots) else: last_pivot = len(self.final_pivot_tps) tmp_pivots = self.final_pivot_tps[first_pivot:last_pivot] if min(np.diff(tmp_pivots))<0.000001: print pivots self.tps = self.full_tps[(self.full_tps>=tmp_pivots[0])*(self.full_tps<tmp_pivots[-1])] self.obs = self.full_obs[(self.full_tps>=tmp_pivots[0])*(self.full_tps<tmp_pivots[-1])] except: import ipdb; ipdb.set_trace() self.pivot_freq = self.initial_guess(tmp_pivots, ws=2*(min(50,len(self.obs))//2)) self.pivot_freq[0]=self.pivot_freq[1] self.pivot_freq[-1]=self.pivot_freq[-2] self.frequency_estimate = interp1d(tmp_pivots, self.pivot_freq, kind=self.interpolation_type, bounds_error=False) if self.verbose: print "Initial pivots:", tmp_pivots, self.pivot_freq steps= [4,2,1] for si in steps: if len(self.final_pivot_tps)>2*si or si==1: # subset the pivots, if the last point is not included, attach it self.pivot_tps = tmp_pivots[::si] if self.pivot_tps[-1]!=tmp_pivots[-1]: self.pivot_tps = np.concatenate((self.pivot_tps, tmp_pivots[-1:])) self.pivot_freq = self.frequency_estimate(self.pivot_tps) if np.max(np.abs(self.pivot_freq))>20: import ipdb; ipdb.set_trace() # determine the optimal pivot freqquencies self.pivot_freq = minimizer(self.logLH, self.pivot_freq, ftol = self.tol, xtol = self.tol, disp = self.verbose>0) if self.logit: self.pivot_freq = logit_transform(fix_freq(logit_inv(self.pivot_freq), 0.0001)) else: self.pivot_freq = fix_freq(self.pivot_freq, 0.0001) # instantiate an interpolation object based on the optimal frequency pivots self.frequency_estimate = interp1d(self.pivot_tps, self.pivot_freq, kind=self.interpolation_type, bounds_error=False) if min(np.diff(self.pivot_tps))<0.000001: print pivots if self.verbose: print "neg logLH using",len(self.pivot_tps),"pivots:", self.logLH(self.pivot_freq) self.final_pivot_freq=np.zeros_like(self.final_pivot_tps) self.final_pivot_freq[first_pivot:last_pivot]=self.pivot_freq self.final_pivot_freq[:first_pivot] = self.final_pivot_freq[first_pivot] self.final_pivot_freq[last_pivot:] = self.final_pivot_freq[last_pivot-1] self.frequency_estimate = interp1d(self.final_pivot_tps, self.final_pivot_freq, kind=self.interpolation_type, bounds_error=False)
def train(self,X,y): theta0 = self.roll(self.theta) X,self.mean,self.std = normalize(X) self.nTrainingExamples = X.shape[0] results = minimizer(lambda x: self.cost_function(X,y,x),theta0,approx_grad = False) self.theta = self.unroll(self.theta,results[0]) return results
def minimize_af_error(self): from scipy.optimize import fmin as minimizer if self.verbose: print "initial function value:", self.af_fit(self.model_params) print "initial parameters:", self.model_params self.model_params = minimizer(self.af_fit, self.model_params, disp = self.verbose>1) if self.verbose: print "final function value:", self.af_fit(self.model_params) print "final parameters:", self.model_params, '\n'
def learn(self): from scipy.optimize import fmin_powell as minimizer switches = np.abs(np.diff(self.obs)).nonzero()[0] try: if len(switches) > 5: first_switch = self.tps[switches[0]] last_switch = self.tps[switches[-1]] else: first_switch = self.tps[0] last_switch = self.tps[-1] if first_switch > self.final_pivot_tps[ 0] and first_switch < self.final_pivot_tps[-1]: first_pivot = max( 0, np.where(first_switch <= self.final_pivot_tps)[0][0] - self.extra_pivots) else: first_pivot = 0 if last_switch < self.final_pivot_tps[ -1] and last_switch > self.final_pivot_tps[0]: last_pivot = min( len(self.final_pivot_tps), np.where(last_switch > self.final_pivot_tps)[0][-1] + self.extra_pivots) else: last_pivot = len(self.final_pivot_tps) tmp_pivots = self.final_pivot_tps[first_pivot:last_pivot] if min(np.diff(tmp_pivots)) < 0.000001: print pivots self.tps = self.full_tps[(self.full_tps >= tmp_pivots[0]) * (self.full_tps < tmp_pivots[-1])] self.obs = self.full_obs[(self.full_tps >= tmp_pivots[0]) * (self.full_tps < tmp_pivots[-1])] except: import ipdb ipdb.set_trace() self.pivot_freq = self.initial_guess(tmp_pivots, ws=2 * (min(50, len(self.obs)) // 2)) self.pivot_freq[0] = self.pivot_freq[1] self.pivot_freq[-1] = self.pivot_freq[-2] self.frequency_estimate = interp1d(tmp_pivots, self.pivot_freq, kind=self.interpolation_type, bounds_error=False) if self.verbose: print "Initial pivots:", tmp_pivots, self.pivot_freq steps = [4, 2, 1] for si in steps: if len(self.final_pivot_tps) > 2 * si or si == 1: # subset the pivots, if the last point is not included, attach it self.pivot_tps = tmp_pivots[::si] if self.pivot_tps[-1] != tmp_pivots[-1]: self.pivot_tps = np.concatenate( (self.pivot_tps, tmp_pivots[-1:])) self.pivot_freq = self.frequency_estimate(self.pivot_tps) if np.max(np.abs(self.pivot_freq)) > 20: import ipdb ipdb.set_trace() # determine the optimal pivot freqquencies self.pivot_freq = minimizer(self.logLH, self.pivot_freq, ftol=self.tol, xtol=self.tol, disp=self.verbose > 0) if self.logit: self.pivot_freq = logit_transform( fix_freq(logit_inv(self.pivot_freq), 0.0001)) else: self.pivot_freq = fix_freq(self.pivot_freq, 0.0001) # instantiate an interpolation object based on the optimal frequency pivots self.frequency_estimate = interp1d( self.pivot_tps, self.pivot_freq, kind=self.interpolation_type, bounds_error=False) if min(np.diff(self.pivot_tps)) < 0.000001: print pivots if self.verbose: print "neg logLH using", len( self.pivot_tps), "pivots:", self.logLH(self.pivot_freq) self.final_pivot_freq = np.zeros_like(self.final_pivot_tps) self.final_pivot_freq[first_pivot:last_pivot] = self.pivot_freq self.final_pivot_freq[:first_pivot] = self.final_pivot_freq[ first_pivot] self.final_pivot_freq[last_pivot:] = self.final_pivot_freq[last_pivot - 1] self.frequency_estimate = interp1d(self.final_pivot_tps, self.final_pivot_freq, kind=self.interpolation_type, bounds_error=False)
def train(net, A, B): # train against two data sets print (minimizer(loss, [-25, -25, -25, -25, 2, 2], args = (net, A, B), approx_grad = True))