def test_cross_val(self,trl_type='validation'): import EM import copy as cp CV_params = cp.deepcopy(self.params) CV_params['nTrials'] = len(self.dsets[trl_type]) CV_params['latent_trajctory'] =[_np.zeros([self.nDims,self.n_timePoints]) for i in range(len(CV_params['latent_traj']))] lapinfres = EM.E_step(self.dsets[trl_type],CV_params) self.CV_inf[trl_type] = lapinfres self.CV_inf[trl_type]['latent_traj'] = lapinfres['post_mean']
def fit(self,nIter=20,lookahead=True,metric='LL',verbose=True,lNo=5): import time import EM from _gpinf import precompute_gp, GP_timescale_Cost import scipy.optimize as op import copy as cp st = time.time() self.hists = {'lapinf':[], 'Cdinf':[], 'tavinf':[] } self.was_fit = True to_break = False for iterN in range(nIter): if verbose: print "Running EM iteration %s" %iterN, ####### E-step ########### lapinfres = EM.E_step(self.train_data,self.params) self._update_params_E(lapinfres) self.hists['lapinf'].append(lapinfres) ####### M-step ########### Cdinf, tavInf = EM.M_step(self.train_data,self.params) self.hists['Cdinf'].append(Cdinf); self.hists['tavinf'].append(tavInf) self._update_params_M(Cdinf,tavInf) # inf_rates = _np.exp(self.params['C'].dot(x[trl_idx]) + self.params['d'][:,None]) # LLi = _np.sum(self.train_data*_np.log(infRates) - infRates) if verbose: print "|| log(L): %s || total time: %ss" %(_np.round(Cdinf['logL'],decimals=2),_np.round(time.time()-st,decimals=1)), self.params['logL_store'].append(Cdinf['logL']) if lookahead: loo_res = self.leave_N_out_CV(N=lNo) #One Step look ahead prediction to monitor convergence if iterN>=1: if _np.median(loo_res['LL_poiss'])<pLL: self._update_params_E(self.hists['lapinf'][-2]) self._update_params_M(self.hists['Cdinf'][-2],self.hists['tavinf'][-2]) print "\n\nLeave one out error on validation set increased, stopping early" to_break = True pLL = _np.median(loo_res['LL_poiss']) if verbose: print " || cc %s ||" %_np.round(_np.median(loo_res['pearsonr']),decimals=2), print " || Validation LL %s" %_np.round(pLL/(_np.sum(self.dsets['validation'])),decimals=4) if to_break: break