def get_gradient(self, data, params): (samples_and_r_scales, actions) = unzip(data) (samples, r_scales) = unzip(samples_and_r_scales) gradient = self._get_gradient(samples, r_scales, actions) self._compute_M_step(params, actions) return gradient
def get_objective(self, data, params): (samples, actions) = unzip(data) (rewards, r_scales) = unzip(samples) r_scales = np.array(r_scales)[:, np.newaxis] actions = np.array(actions)[:, np.newaxis] shifted_samples = np.array(rewards) + \ np.hstack([r_scales, -r_scales]) densities = self._get_densities(shifted_samples) terms = np.dot(densities, self.ps) return -np.mean(np.log(terms), axis=0)[0]
def _update_history(self, Xs, missing): normed = unzip(self.basis_pairs_t)[1] if self.filtering_history is None: self.filtering_history = [ np.dot(X[-1, :], Phi) for (X, Phi) in zip(Xs, normed) ] if self.keep_basis_history: self.basis_history = [[ Phi[:, i][:, np.newaxis].T for i in xrange(self.k) ] for Phi in normed] else: for i in xrange(self.num_views): current = self.filtering_history[i] # TODO: put meaningful filler here; zeroes are not meaningful new = np.zeros((1, self.k)) if not missing[i]: new = np.dot(Xs[i][-1, :], normed[i]) self.filtering_history[i] = np.vstack([current, new]) # Update basis history if self.keep_basis_history: for j in xrange(self.k): current = self.basis_history[i][j] new = np.copy(current[-1, :]) if not missing[i]: new = normed[i][:, j] self.basis_history[i][j] = np.vstack([current, new])
def run_things_all_day_bb(data_dir, num_data, k, ds, pi_factors, lazy): ds = [int(d) for d in ds.split()] angles = [float(pf) * np.pi for pf in pi_factors.split()] pre_As = [np.random.randn(2 * d, d) for d in ds] As = [np.dot(pre_A.T, pre_A) for pre_A in pre_As] (_, Qs) = unzip([np.linalg.eig(A) for A in As]) zipped = zip(ds, angles, Qs) dynamics = [get_rotation(d, a, Q, P_inv=Q.T) for (d, a, Q) in zipped] loaders = get_easy_DCCAPMLs(dynamics, num_data, k, ds, lazy=lazy) names = ['N', 'k', 'ds', 'pi_factors'] vals = [ str(num_data), str(k), '-'.join([str(d) for d in ds]), '-'.join([str(pf) for pf in pi_factors]) ] dirname = get_ts('easy_cca' + '_' + '_'.join([n + '-' + v for (n, v) in zip(names, vals)])) dirpath = os.path.join(data_dir, dirname) os.mkdir(dirpath) for (i, l) in enumerate(loaders): filename = 'view_' + str(i) + '.csv' filepath = os.path.join(dirpath, filename) np.savetxt(filepath, l.get_data(), delimiter=',')
def run(self): printerval = self.num_data / 10 for t in range(self.num_data): zipped = zip(self.agvs, self.servers) for (agv, ds) in zipped: agv.set_data(ds.get_data()) if self.delay is None or t % self.delay == 0: XPhis = [agv.get_projected() for agv in self.agvs] for agv in self.agvs: for (n, XPhi) in enumerate(XPhis): agv.update_neighbor_state(n, XPhi) (self.Phis, new_tccs) = unzip([agv.get_update() for agv in self.agvs]) self.tccs.append(new_tccs) if False: #t % printerval == 0: print('tccs', new_tccs) zipped = zip(self.Phis, self.loaders) XPhis = [np.dot(get_zm_uv(dl.get_data()), Phi) for (Phi, dl) in zipped] tcc_mats = [ np.dot(XPhis[n].T, XPhis[m]) for n in range(self.num_views) for m in range(n, self.num_views) ] tccs = [np.trace(tccm) / self.k for tccm in tcc_mats] print(tccs)
def update(self, Xs, Sxs, missing, etas): if self.basis_pairs_t is None: #print 'Initializing basis_pairs_t' # Initialization of optimization variables self.basis_pairs_t = agu.get_init_basis_pairs(Sxs, self.k) self.num_rounds += 1 #print 'Updating history' self._update_history(Xs, missing) if self.verbose: print "\tGetting updated basis estimates" # Get updated canonical bases #print 'Setting basis_pairs_t1' self.basis_pairs_t1 = self._get_basis_updates(Xs, Sxs, missing, etas) (unn, normed) = unzip(self.basis_pairs_t1) #print 'Computing loss' loss = gu.misc.get_objective(Xs, normed) if self.verbose: print "\tObjective:", loss self.loss_history.append(loss) # Check for convergence unnormed_pairs = zip(unzip(self.basis_pairs_t)[0], unn) #print 'Checking for convergence' pre_converged = gu.misc.is_converged(unnormed_pairs, self.epsilons, self.verbose) # This is because bases are unchanged for missing data self.converged = [ False if missing[i] else c for (i, c) in enumerate(pre_converged) ] # Update iterates self.basis_pairs_t = [(np.copy(unn_Phi), np.copy(Phi)) for unn_Phi, Phi in self.basis_pairs_t1] return (self.basis_pairs_t, loss)
def get_coordinate_counts(self, data): actions = unzip(data)[1] total = len(actions) acted = sum(actions) coord_counts = np.zeros(self.get_parameter_shape()) coord_counts[0:2, :] = total coord_counts[2:, :] = acted return coord_counts
def _get_S_and_A_servers(self): (njks, phi_jks, self.nks) = unzip([n.get_fsvrg_params() for n in self.nodes]) self.n = sum(self.nks) njs = sum(njks) phi_js = njs / self.n sjks = [(phi_js / phi_jk) for phi_jk in phi_jks] S_servers = [SDS(sjk) for sjk in sjks] omega_js = np.vstack((njk != 0).astype(float) for njk in njks) ajs = get_sp(omega_js / self.num_nodes, -1) A_server = SDS(ajs) return (S_servers, A_server)
def get_gradients(Xs, basis_pairs): m = len(Xs) X_transforms = [(np.dot(X, unnormed), np.dot(X, normed)) for (X, (unnormed, normed)) in zip(Xs, basis_pairs)] info = zip(Xs, basis_pairs, X_transforms) for thang in info: X = thang[0] (unnormed_Phi, normed_Phi) = thang[1] (unnormed_Phi_X, normed_Phi_X) = thang[2] #print 'X', X #print 'unnormed_Phi', unnormed_Phi #print 'normed_Phi', normed_Phi #print 'unnormed_Phi_X', unnormed_Phi_X #print 'normed_Phi_X', normed_Phi_X drdb.check_for_large_numbers(unnormed_Phi_X, 'appgrad.utils get_gradients', 'unnormed_Phi_X') drdb.check_for_large_numbers(normed_Phi_X, 'appgrad.utils get_gradients', 'normed_Phi_X') minus_term = sum(unzip(X_transforms)[1]) drdb.check_for_large_numbers(minus_term, 'appgrad.utils get_gradients', 'minus_term') diffs = [(m-1)*unnormed - \ (minus_term - normed) for (unnormed, normed) in X_transforms] for diff in diffs: #print 'Diff', diff drdb.check_for_large_numbers(diff, 'appgrad.utils get_gradients', 'diff') gradients = [ np.dot(X.T, diff) / X.shape[0] for (X, diff) in zip(Xs, diffs) ] for g in gradients: drdb.check_for_large_numbers(g, 'appgrad.utils get_gradients', 'gradient') return gradients
def get_status(self): return { 'k': self.k, 'converged': self.converged, 'bases': unzip(self.basis_pairs_t)[1], 'num_views': self.num_views, 'epsilons': self.epsilons, 'num_rounds': self.num_rounds, 'num_iters': self.num_iters, 'has_been_fit': self.has_been_fit, 'basis_pairs': self.basis_pairs_t, 'basis_history': self.basis_history, 'loss_history': self.loss_history, 'filtering_history': self.filtering_history }
def _compute_S_and_A_servers(self): (njks, phi_jks, self.nks) = unzip([n.get_fsvrg_params() for n in self.nodes]) self.n = sum(self.nks) njs = sum(njks) phi_js = njs / self.n sjks = [(phi_js / phi_jk)[:, np.newaxis] for phi_jk in phi_jks] self.S_servers = [SDS(sjk) for sjk in sjks] for (n, S_s) in zip(self.nodes, self.S_servers): n.set_S_server(S_s) omega_js = np.vstack( (njk[:, np.newaxis] != 0).astype(float) for njk in njks) ajs = get_sp(omega_js / self.num_nodes, -1) self.A_server = SDS(ajs)
def compute_parameters(self): w_t = np.copy(self.init_params) for t in xrange(self.num_rounds): local_grads = [n.get_gradient(np.copy(w_t)) for n in self.nodes] grad_t = sum(local_grads) / self.num_nodes updates = [n.get_update( np.copy(w_t), np.copy(grad_t)) for n in self.nodes] (ws, objectives) = unzip(updates) # TODO: Make sure this is a good place to project w_t = sum(ws) / self.num_nodes self.objectives.append(sum(objectives)) self.w = w_t
def compute_parameters(self): w_t = np.copy(self.init_params) for i in xrange(self.max_rounds): local_grads = [ n.get_gradient(n.get_local(w_t)) for n in self.nodes ] grad_t = np.vstack(local_grads) / self.num_nodes updates = [ n.get_update(np.copy(w_t), np.copy(grad_t)) for n in self.nodes ] (ws, objectives) = unzip(updates) agg = self._get_aggregate_grad(ws, w_t) w_t = w_t + agg self.objectives.append([o[-1] for o in objectives]) self.w = w_t
def compute_parameters(self): w_t = np.copy(self.init_params) for i in xrange(self.max_rounds): grad_t = np.zeros_like(w_t) if i > 0: local_grads = [ n.get_gradient(n.get_local(w_t)) for n in self.nodes ] grad_t = np.vstack(local_grads) / self.num_nodes (S_servers, A_server) = self._get_S_and_A_servers() for (S_s, n) in zip(S_servers, self.nodes): n.set_global_info(w_t, grad_t) n.set_S_server(S_s) for j in xrange(2**i): self.num_rounds += 1 for n in self.nodes: n.set_local_action() for n in self.nodes: n.compute_local_update() updates = [n.get_update() for n in self.nodes] (ws, objectives) = unzip(updates) agg = self._get_aggregate_grad(ws, w_t, A_server) w_t = w_t + agg for (i, o) in enumerate(objectives): self.objectives[i].append(o) #for n in self.nodes: # print n.objectives self.w = w_t
def get_bases(self): if not self.has_been_fit: raise Exception('Model has not yet been fit.') return unzip(self.basis_pairs_t)[1]