def __str__(self): strs = ['#p\t#q\t#f\t#dp\t#y'] for i, Dy in enumerate(self.support_): strs.append('{0}\t{1}\t{2}\t{3}\t{4}'.format( self.p(i), self.q(i), npvec2str(self.f(i)), npvec2str(self.dtheta(i)), Dy.projection)) strs.append('#\t#\t#<f>\t#\t#') strs.append(' \t \t%s\t \t ' % npvec2str(self.uf())) return '\n'.join(strs)
def __str__(self): strs = ['#p\t#q\t#f\t#dp\t#y'] for i, Dy in enumerate(self.support_): strs.append('{0}\t{1}\t{2}\t{3}\t{4}'.format(self.p(i), self.q(i), npvec2str(self.f(i)), npvec2str(self.dtheta(i)), Dy.projection)) strs.append('#\t#\t#<f>\t#\t#') strs.append(' \t \t%s\t \t ' % npvec2str(self.uf())) return '\n'.join(strs)
def f(w): self.iteration += 1 # the joint update is no longer supported: self.wmap.update(w) self.history.append(w) self.wmap.proxy.update(w) logging.info('[%d/%d] lambda=%s', self.parent_iteration, self.iteration, npvec2str(w)) #logging.info('[%d/%d] sampling ...', self.parent_iteration, self.iteration) self.sample() #logging.info('[%d/%d] KL ...', self.parent_iteration, self.iteration) divergences, derivatives = self.KL() kl = divergences.mean(0) dkl = derivatives.mean(0) # r_weight r_weight = 0 # regularised regulariser = LA.norm(w, 2) r_obj = kl + r_weight * regulariser r_jac = dkl + 2 * r_weight * w logging.info('[%d/%d] KL=%s regularised=%f', self.parent_iteration, self.iteration, kl, r_obj) return r_obj, r_jac
def callback(theta): logging.info('[%d] new theta: %s', self.iteration, npvec2str(theta)) # register the theta self.wmap.target.update(theta) self.selected_thetas.append(theta) self.last_callback = self.iteration
def f(theta): run.next_proxysgd() self.wmap.proxy.update(theta) logging.info('[%s] lambda=%s', run, npvec2str(theta)) OBJ = [] JAC = [] H = [] dH = [] for i, seg in enumerate(devset): derivations = S[i] lmap = L[i] local_obj, local_jac, local_H, local_dH = get_obj_and_jac( derivations, q_wmap=self.wmap.proxy, p_wmap=self.wmap.target, lmap=lmap, empirical_q=False) OBJ.append(local_obj) JAC.append(local_jac) H.append(local_H) dH.append(local_dH) obj, jac = np.mean(OBJ, 0), np.mean(JAC, 0) if polarity != 1: obj *= polarity jac *= polarity if Tq == 0.0 and l2_weight == 0.0: logging.info('[%s] O=%f', run, obj) return obj, jac else: r_obj = obj r_jac = jac.copy() if Tq != 0.0: r_obj -= Tq * np.mean(H, 0) r_jac -= Tq * np.mean(dH, 0) logging.info('[%s] O=%f H-regularised=%f', run, obj, r_obj) if l2_weight != 0.0: # regularised regulariser = LA.norm(theta, 2)**2 r_obj += l2_weight * regulariser r_jac += 2 * l2_weight * theta logging.info('[%s] O=%f L2-regularised=%f', run, obj, r_obj) return r_obj, r_jac
def f(theta): run.next_targetsgd() self.wmap.target.update(theta) logging.info('[%s] theta=%s', run, npvec2str(theta)) R = [] dR = [] H = [] dH = [] for i, seg in enumerate(devset): derivations = S[i] lmap = L[i] local_R, local_dR, local_H, local_dH = optalg.minrisk( derivations, q_wmap=self.wmap.proxy, p_wmap=self.wmap.target, lmap=lmap, empirical_q=True) R.append(local_R) dR.append(local_dR) H.append(local_H) dH.append(local_dH) obj, jac = np.mean(R, 0), np.mean(dR, 0) if Tp == 0.0 and l2_weight == 0.0: logging.info('[%s] Risk=%f', run, obj) return obj, jac else: r_obj = obj r_jac = jac.copy() if Tp != 0.0: # H-regularised r_obj -= Tp * np.mean(H, 0) r_jac -= Tp * np.mean(dH, 0) logging.info('[%s] Risk=%f H-regularised=%f', run, obj, r_obj) if l2_weight != 0.0: # L2-regularised regulariser = LA.norm(theta, 2)**2 r_obj += l2_weight * regulariser r_jac += 2 * l2_weight * theta logging.info('[%s] Risk=%f L2-regularised=%f', run, obj, r_obj) return r_obj, r_jac
def save_estimates(sid, empdist, losses, posterior, risk, dR, stems): """ """ with open('{0}/{1}'.format(stems[0], sid), 'w') as fo: print >> fo, empdist with open('{0}/{1}'.format(stems[1], sid), 'w') as fo: print >> fo, '#loss' print >> fo, '\n'.join('%.4f' % l for l in losses) with open('{0}/{1}'.format(stems[2], sid), 'w') as fo: print >> fo, '#risk\t#dR' print >> fo, '{0}\t{1}'.format(risk, npvec2str(dR))
def f(theta): self.iteration += 1 # the joint update is no longer supported: self.wmap.update(w) self.wmap.target.update(theta) logging.info('[%d] theta=%s', self.iteration, npvec2str(theta)) # KL here? # optimises KL if necessary if self.kl_current > self.kl_min * self.kl_coeff or self.iteration > self.kl_lastit + 10: lambdas, self.kl_min = self.optimise_proxy(target=theta) self.kl_lastit = self.iteration # register the lambda self.wmap.proxy.update(lambdas) self.selected_lambdas.append(lambdas) #logging.info('[%d] sampling ...', self.iteration) self.sample() # logging.info('[%d] decision rules ...', self.iteration) # self.decide() # compute loss #logging.info('[%d] assessing risk ...', self.iteration) risks, jacs, kls = self.risk() risk = risks.mean(0) jac = jacs.mean(0) kl = kls.mean(0) if kl < self.kl_min: self.kl_min = kl self.kl_current = kl # r_weight r_weight = 0 # regularised regulariser = LA.norm(theta, 2) r_obj = risk + r_weight * regulariser r_jac = jac + 2 * r_weight * theta logging.info('[%d] RISK=%f regularised=%f KL=%f', self.iteration, risk, r_obj, kl) #logging.info('[%d] jac=%s', self.iteration, jac) return r_obj, r_jac
def callback(w): self.selected.append(w) logging.info('[%d/%d] new lambda: %s', self.parent_iteration, self.iteration, npvec2str(w))
def optimise(self): self.iteration = -1 self.history = [] self.selected = [] def f(w): self.iteration += 1 # the joint update is no longer supported: self.wmap.update(w) self.history.append(w) self.wmap.proxy.update(w) logging.info('[%d/%d] lambda=%s', self.parent_iteration, self.iteration, npvec2str(w)) #logging.info('[%d/%d] sampling ...', self.parent_iteration, self.iteration) self.sample() #logging.info('[%d/%d] KL ...', self.parent_iteration, self.iteration) divergences, derivatives = self.KL() kl = divergences.mean(0) dkl = derivatives.mean(0) # r_weight r_weight = 0 # regularised regulariser = LA.norm(w, 2) r_obj = kl + r_weight * regulariser r_jac = dkl + 2 * r_weight * w logging.info('[%d/%d] KL=%s regularised=%f', self.parent_iteration, self.iteration, kl, r_obj) return r_obj, r_jac def callback(w): self.selected.append(w) logging.info('[%d/%d] new lambda: %s', self.parent_iteration, self.iteration, npvec2str(w)) self.result_ = minimize( f, self.wmap.proxy.asarray, method='L-BFGS-B', jac=True, callback=callback, options={ 'maxfun': 10, 'ftol': 1e-6, 'gtol': 1e-3, 'disp': False }) # TODO find a way to stop the search earlier 'maxfun' logging.info('[%d/%d] final KL=%s lambda=%s', self.parent_iteration, self.iteration, self.result_.fun, npvec2str(self.result_.x)) return self.result_.x, self.result_.fun
def callback(theta): logging.info('[%s] new lambda: %s', run, npvec2str(theta))