def _build_model(self, data, feats, states): self._state0 = np.mean(states, 0) # Statistics for clamping self._max_state_norm2 = np.max(np.sum(states * states, 0)) self._max_state_norm = np.sqrt(self._max_state_norm2) self._max_state_coord = np.max(states, axis=1).reshape((-1, 1)) self._min_state_coord = np.min(states, axis=1).reshape((-1, 1)) # Horizon Prediction s2_h_in = ula.khatri_rao_rowwise(states, feats.fut_act) W_h = ridge(s2_h_in, feats.fut_obs, self._lambda['pred']) W_rff2fo = ridge(feats.fut_obs, data.fut_obs, self._lambda['pred']) self._W_h = np.dot(W_h, W_rff2fo) # 1-Step Prediction s2_1s_in = ula.khatri_rao_rowwise(states, feats.act) W_1s = ridge(s2_1s_in, feats.obs, self._lambda['pred']) W_rff2obs = ridge(feats.obs, data.obs, self._lambda['pred']) self._W_1s = np.dot(W_1s, W_rff2obs)
def _estimate_condop(self, A, B, C, reg_lambda, div_lambda, importance_weights): N,da = A.shape db = B.shape[1] dab = da*db dbb = db*db reg_out = np.empty((N,dab+dbb)) reg_out[:,:dab] = ula.khatri_rao_rowwise(A,B) reg_out[:,dab:] = ula.khatri_rao_rowwise(B,B) W = ridge(C, reg_out, reg_lambda, importance_weights) est_reg_out = np.dot(C,W) output = np.empty((N,dab)) for i in xrange(N): C_ab = est_reg_out[i,:dab].reshape((da,db)) C_bb = est_reg_out[i,dab:].reshape((db,db)) C_a_b = ula.reg_rdivide_nopsd(C_ab, C_bb, div_lambda) output[i,:] = C_a_b.reshape(-1) return output,W
def _s1_regression_cond(self, data, feats, imp_weights): print('Stage 1A Regression') s1a_in = ula.khatri_rao_rowwise(feats.past, feats.fut_act) s1a_out = feats.fut_obs W_s1a = ridge(s1a_in, s1a_out, self._lambda['s1a'], imp_weights[0]) W_s1a = W_s1a.reshape((self._feat_dim.past, self._feat_dim.fut_act, self._feat_dim.fut_obs)) W_s1a = W_s1a.transpose((0, 2, 1)) W_s1a = W_s1a.reshape((self._feat_dim.past, -1)) states = np.dot(feats.past, W_s1a) print('Stage 1B Regression') s1b_in = ula.khatri_rao_rowwise(feats.past, feats.exfut_act) s1b_out = feats.exfut_obs W_s1b = ridge(s1b_in, s1b_out, self._lambda['s1b'], imp_weights[1]) W_s1b = W_s1b.reshape((self._feat_dim.past, self._feat_dim.exfut_act, self._feat_dim.exfut_obs)) W_s1b = W_s1b.transpose((0, 2, 1)) W_s1b = W_s1b.reshape((self._feat_dim.past, -1)) ex_states = np.dot(feats.past, W_s1b) print('Stage 1C Regression') s1c_in = ula.khatri_rao_rowwise(feats.past, feats.act) s1c_out = feats.oo W_s1c = ridge(s1c_in, s1c_out, self._lambda['s1c'], None) W_s1c = W_s1c.reshape( (self._feat_dim.past, self._feat_dim.act, self._feat_dim.oo)) W_s1c = W_s1c.transpose((0, 2, 1)) W_s1c = W_s1c.reshape((self._feat_dim.past, -1)) im_states = np.dot(feats.past, W_s1c) self._dbg_W_s1a = W_s1a self._dbg_W_s1b = W_s1b self._dbg_W_s1c = W_s1c return states, ex_states, im_states
def _extract_feats(self, data, U_old = None): # Past print('Past') feats = structtype() self._fext_past = self._feature_set['past'] feats.past = self._fext_past.build(data.past).process(data.past) # Immediate print('Immediate') self._fext_obs = self._feature_set['obs'] feats.obs = self._fext_obs.build(data.obs).process(data.obs) self._fext_act = self._feature_set['act'] feats.act = self._fext_act.build(data.act).process(data.act) # Future print('Future') self._fext_fut_obs = self._feature_set['fut_obs'] feats.fut_obs = self._fext_fut_obs.build(data.fut_obs).process(data.fut_obs) self._fext_fut_act = self._feature_set['fut_act'] feats.fut_act = self._fext_fut_act.build(data.fut_act).process(data.fut_act) ''' Project future into subspace predictable by history ''' if self._past_projection == 'svd': self._fext_fut_obs, feats.fut_obs = self._project_on_past(self._fext_fut_obs, feats.fut_obs, feats.past) self._fext_fut_act, feats.fut_act = self._project_on_past(self._fext_fut_act, feats.fut_act, feats.past) else: assert self._past_projection is None, 'past projection is not None' # Shifted Future print('Shifted Future') feats.shfut_obs = self._fext_fut_obs.process(data.shfut_obs) feats.shfut_act = self._fext_fut_act.process(data.shfut_act) print('Derived Features') # Derived Features: # Extended Future # Note that for exteded future observation, the current observation is # the "lower order" factor. This makes filtering easier. feats.exfut_obs = ula.khatri_rao_rowwise(feats.shfut_obs, feats.obs) feats.exfut_act = ula.khatri_rao_rowwise(feats.act, feats.shfut_act) self._U_efo, _, exfut_obs = ula.rand_svd_f(feats.exfut_obs.T, k=self._p, rng=self.rng) feats.exfut_obs = exfut_obs.T self._U_efa, _, exfut_act = ula.rand_svd_f(feats.exfut_act.T, k=self._p, rng=self.rng) feats.exfut_act = exfut_act.T # Observation Covariance feats.oo = ula.khatri_rao_rowwise(feats.obs, feats.obs) # Project lower triangle part of observation covariance d_obs = feats.obs.shape[1] lt_idx = np.array([i for i in xrange(d_obs**2) if (i//d_obs) >= (i%d_obs)]) ut_idx = (lt_idx%d_obs)*d_obs + lt_idx//d_obs feats.oo = feats.oo[:,lt_idx] self._U_oo,_,oo = ula.rand_svd_f(feats.oo.T, k=self._p, rng=self.rng) feats.oo = oo.T # Convert singular vectors back to represent symmetric matrices Usym = np.empty((d_obs**2, self._U_oo.shape[1])) Usym[lt_idx,:] = self._U_oo Usym[ut_idx,:] = self._U_oo self._U_oo = Usym K = structtype() K.obs = feats.obs.shape[1] K.act = feats.act.shape[1] K.past = feats.past.shape[1] K.fut_obs = feats.fut_obs.shape[1] K.fut_act = feats.fut_act.shape[1] K.exfut_obs = feats.exfut_obs.shape[1] K.exfut_act = feats.exfut_act.shape[1] K.oo = feats.oo.shape[1] self._feat_dim = K for (k,v) in K.__dict__.items(): print('%s:%d' % (k,v)) return feats