def train(self, rand_seed: int = 42, verbose: int = 0) -> float: """ Parameters ---------- rand_seed: int random seed verbose : int Determine the level of logging Returns ------- oos_result : float The performance of model evaluated by oos """ train_data = generate_train_data(rand_seed=rand_seed, **self.data_config) test_data = generate_test_data(**self.data_config) train_1st_t, train_2nd_t = self.split_train_data(train_data) test_data_t = TestDataSetTorch.from_numpy(test_data) if self.gpu_flg: train_1st_t = train_1st_t.to_gpu() train_2nd_t = train_2nd_t.to_gpu() test_data_t = test_data_t.to_gpu() if self.monitor is not None: new_rand_seed = np.random.randint(1e5) new_data_config = copy.copy(self.data_config) new_data_config["data_size"] = 5000 validation_data = generate_train_data(rand_seed=new_rand_seed, **self.data_config) validation_data_t = TrainDataSetTorch.from_numpy(validation_data) if self.gpu_flg: validation_data_t = validation_data_t.to_gpu() self.monitor.configure_data(train_1st_t, train_2nd_t, test_data_t, validation_data_t) self.lam1 *= train_1st_t[0].size()[0] self.lam2 *= train_2nd_t[0].size()[0] for t in range(self.n_epoch): self.stage1_update(train_1st_t, verbose) if self.covariate_net: self.update_covariate_net(train_1st_t, train_2nd_t, verbose) self.stage2_update(train_1st_t, train_2nd_t, verbose) if self.monitor is not None: self.monitor.record(verbose) if verbose >= 1: logger.info(f"Epoch {t} ended") mdl = DFIVModel(self.treatment_net, self.instrumental_net, self.covariate_net, self.add_stage1_intercept, self.add_stage2_intercept) mdl.fit_t(train_1st_t, train_2nd_t, self.lam1, self.lam2) if self.gpu_flg: torch.cuda.empty_cache() oos_loss: float = mdl.evaluate_t(test_data_t).data.item() return oos_loss
def train(self, rand_seed: int = 42, verbose: int = 0) -> float: """ Parameters ---------- rand_seed: int random seed verbose : int Determine the level of logging Returns ------- oos_result : float The performance of model evaluated by oos """ train_data = generate_train_data(rand_seed=rand_seed, **self.data_config) test_data = generate_test_data(**self.data_config) train_1st_data, train_2nd_data = self.split_train_data(train_data) treatment_sigma = get_median(train_1st_data.treatment) treatment_feature_ext = RFFKernelIVTrainer.get_kernel_approx( self.kernel_approx_name, 1.0 / treatment_sigma, self.n_feature, train_1st_data.treatment) whole_instrumental = train_data.instrumental instrument_sigma = get_median(whole_instrumental) instrument_feature_ext = RFFKernelIVTrainer.get_kernel_approx( self.kernel_approx_name, 1.0 / instrument_sigma, self.n_feature, whole_instrumental) covariate_feature_ext = None if train_data.covariate is not None: covariate_sigma = get_median(train_2nd_data.covariate) covariate_feature_ext = RFFKernelIVTrainer.get_kernel_approx( self.kernel_approx_name, 1.0 / covariate_sigma, self.n_feature, train_2nd_data.covariate) lam1_arr = 10**np.linspace(self.lambda1[0], self.lambda1[1], 50) lam2_arr = 10**np.linspace(self.lambda2[0], self.lambda2[1], 50) best_lam1, stage1_weight = self.tune_lambda1(treatment_feature_ext, instrument_feature_ext, train_1st_data, train_2nd_data, lam1_arr) best_lam2 = self.tune_lambda2(treatment_feature_ext, instrument_feature_ext, covariate_feature_ext, train_1st_data, train_2nd_data, stage1_weight, lam2_arr) mdl = RFFKernelIVModel(treatment_feature_ext, instrument_feature_ext, covariate_feature_ext) mdl.fit(train_1st_data, train_2nd_data, best_lam1, best_lam2) return mdl.evaluate(test_data)
def train(self, rand_seed: int = 42, verbose: int = 0) -> float: """ Parameters ---------- rand_seed: int random seed verbose : int Determine the level of logging Returns ------- oos_result : float The performance of model evaluated by oos """ train_data = generate_train_data(rand_seed=rand_seed, **self.data_config) test_data = generate_test_data(**self.data_config) if train_data.covariate is not None: train_data = TrainDataSet(treatment=np.concatenate([train_data.treatment, train_data.covariate], axis=1), structural=train_data.structural, covariate=None, instrumental=train_data.instrumental, outcome=train_data.outcome) test_data = TestDataSet(treatment=np.concatenate([test_data.treatment, test_data.covariate], axis=1), covariate=None, structural=test_data.structural) train_data_t = TrainDataSetTorch.from_numpy(train_data) test_data_t = TestDataSetTorch.from_numpy(test_data) if self.gpu_flg: train_data_t = train_data_t.to_gpu() test_data_t = test_data_t.to_gpu() for t in range(self.n_epoch): self.dual_update(train_data_t, verbose) self.primal_update(train_data_t, verbose) if verbose >= 1: logger.info(f"Epoch {t} ended") mdl = DeepGMMModel(self.primal_net, self.dual_net) logger.info(f"test error {mdl.evaluate_t(test_data_t).data.item()}") mdl = DeepGMMModel(self.primal_net, self.dual_net) oos_loss: float = mdl.evaluate_t(test_data_t).data.item() logger.info(f"target variance: {np.var(train_data.outcome)}") return oos_loss
def train(self, rand_seed: int = 42, verbose: int = 0) -> float: train_data = generate_train_data(rand_seed=rand_seed, **self.data_config) test_data = generate_test_data(**self.data_config) train_data_t = TrainDataSetTorch.from_numpy(train_data) test_data_t = TestDataSetTorch.from_numpy(test_data) if self.gpu_flg: train_data_t = train_data_t.to_gpu() test_data_t = test_data_t.to_gpu() try: self.update_stage1(train_data_t, verbose) self.update_stage2(train_data_t, verbose) except RuntimeError: return np.nan self.response_net.train(False) self.instrumental_net.train(False) mdl = DeepIVModel(self.response_net, self.instrumental_net, self.data_config["data_name"]) if self.gpu_flg: torch.cuda.empty_cache() oos_loss: float = mdl.evaluate_t(test_data_t).data.item() return oos_loss
def train(self, rand_seed: int = 42, verbose: int = 0) -> float: """ Parameters ---------- rand_seed: int random seed verbose : int Determine the level of logging Returns ------- oos_result : float The performance of model evaluated by oos """ train_data = generate_train_data(rand_seed=rand_seed, **self.data_config) test_data = generate_test_data(**self.data_config) train_1st_data, train_2nd_data = self.split_train_data(train_data) # get stage1 data X1 = train_1st_data.treatment if train_1st_data.covariate is not None: X1 = np.concatenate([X1, train_1st_data.covariate], axis=-1) Z1 = train_1st_data.instrumental Y1 = train_1st_data.outcome N = X1.shape[0] # get stage2 data X2 = train_2nd_data.treatment if train_2nd_data.covariate is not None: X2 = np.concatenate([X2, train_2nd_data.covariate], axis=-1) Z2 = train_2nd_data.instrumental Y2 = train_2nd_data.outcome M = X2.shape[0] if verbose > 0: logger.info("start stage1") sigmaX = get_median(X1) sigmaZ = get_median(Z1) KX1X1 = KernelIVModel.cal_gauss(X1, X1, sigmaX) KZ1Z1 = KernelIVModel.cal_gauss(Z1, Z1, sigmaZ) KZ1Z2 = KernelIVModel.cal_gauss(Z1, Z2, sigmaZ) KX1X2 = KernelIVModel.cal_gauss(X1, X2, sigmaX) if isinstance(self.lambda1, list): self.lambda1 = 10**np.linspace(self.lambda1[0], self.lambda1[1], 50) gamma = self.stage1_tuning(KX1X1, KX1X2, KZ1Z1, KZ1Z2) else: gamma = np.linalg.solve(KZ1Z1 + N * self.lambda1 * np.eye(N), KZ1Z2) W = KX1X1.dot(gamma) if verbose > 0: logger.info("end stage1") logger.info("start stage2") if isinstance(self.lambda2, list): self.lambda2 = 10**np.linspace(self.lambda2[0], self.lambda2[1], 50) alpha = self.stage2_tuning(W, KX1X1, Y1, Y2) else: alpha = np.linalg.solve( W.dot(W.T) + M * self.lambda2 * KX1X1, W.dot(Y2)) if verbose > 0: logger.info("end stage2") mdl = KernelIVModel(X1, alpha, sigmaX) return mdl.evaluate(test_data)