def train(epoch): t = time.time() model.train() optimizer.zero_grad() idx = idx_train_lst[epoch] output = model(features, adj).view(-1) loss_train = loss(output, labels, idx) r2_train = R2(output[idx], labels[idx]) loss_train.backward() optimizer.step() if not args.fastmode: # Evaluate validation set performance separately, # deactivates dropout during validation run. model.eval() output = model(features, adj) loss_val = loss(output, labels, idx_val) r2_val = R2(output[idx_val], labels[idx_val]) print('Epoch: {:04d}'.format(epoch + 1), 'loss_train: {:.4f}'.format(loss_train.item()), 'r2_train: {:.4f}'.format(r2_train.item()), 'loss_val: {:.4f}'.format(loss_val.item()), 'r2_val: {:.4f}'.format(r2_val.item()), 'time: {:.4f}s'.format(time.time() - t))
def run_feature_selection(): x_train, x_test, y_train, y_test = get_regression_dataset(0.6) config_table = {} for counter in range(1, 2**len(FEATURE_LIST)): subset_features = get_feature_list(counter) subset_x = x_train[subset_features] lr = LinearRegression() lr.fit(subset_x, y_train) subset_x = x_test[subset_features] y_pred = lr.predict(subset_x) r2 = R2(y_pred, y_test.values) bic = BIC(y_pred, y_test.values, len(subset_features)) if r2 > 0: config_table[tuple(subset_features)] = (r2, bic) iter = 0 for key, value in sorted(config_table.iteritems(), key=lambda (k, v): (v[1], k)): print '%s \t (R2 = %0.3f | BIC = %0.3f)' % (key, value[0], value[1]) if iter < 10: iter += 1 else: break
def evaluate_test(model, g, inputs, labels, test_mask, batch_size, device, lp_dict, coeffs, meta): model.eval() with th.no_grad(): pred = model.inference(g, inputs, batch_size, device).view(-1) output = pred.cuda() labels = labels.cuda() idx_test = lp_dict['idx_test'] idx_train = lp_dict['idx_train'] adj = lp_dict['adj'] labels, output, adj = labels.cpu(), output.cpu(), adj.cpu() loss = F.mse_loss(output[idx_test].squeeze(), labels[idx_test].squeeze()) r2_test = compute_r2(output[test_mask], labels[test_mask]) lp_output = lp_refine(idx_test, idx_train, labels, output, adj, torch.tanh(coeffs[0]).item(), torch.exp(coeffs[1]).item()) lp_r2_test = compute_r2(lp_output, labels[idx_test]) lp_output_raw_conv = lp_refine(idx_test, idx_train, labels, output, adj) lp_r2_test_raw_conv = R2(lp_output_raw_conv, labels[idx_test]) print("------------") print("election year {}".format(meta)) print("loss:", loss.item()) print("raw_r2:", r2_test) print("refined_r2:", lp_r2_test) print("refined_r2_raw_conv:", lp_r2_test_raw_conv) print("------------") model.train() return lp_r2_test
def test(adj, features, labels, test_meta): model.eval() output = model(features, adj).view(-1) loss_test = loss(output, labels, idx_test, adj, coeffs, True) r2_test = R2(output[idx_test], labels[idx_test]) labels, output, adj = labels.cpu(), output.cpu(), adj.cpu() lp_output = lp_refine(idx_test, idx_train, labels, output, adj, torch.tanh(coeffs[0]).item(), torch.exp(coeffs[1]).item()) lp_r2_test = R2(lp_output, labels[idx_test]) lp_output_raw_conv = lp_refine(idx_test, idx_train, labels, output, adj) lp_r2_test_raw_conv = R2(lp_output_raw_conv, labels[idx_test]) print("Test set ({}) results:".format(test_meta), "loss= {:.4f}".format(loss_test.item()), "R2= {:.4f}".format(r2_test.item()), "LP_R2= {:.4f}".format(lp_r2_test.item()), "LP_R2_raw_conv= {:.4f}\n".format(lp_r2_test_raw_conv.item()))
def test(adj, features, labels, test_meta): model.eval() output = model(features, adj).view(-1) ''' np.save("test_lp/output_all.npy", output.cpu().detach().numpy()) np.save("test_lp/labels_all.npy", labels.cpu().detach().numpy()) np.save("test_lp/idx_train.npy", idx_train.cpu().detach().numpy()) np.save("test_lp/idx_val.npy", idx_val.cpu().detach().numpy()) np.save("test_lp/idx_test.npy", idx_test.cpu().detach().numpy()) ''' loss_test = loss(output, labels, idx_test) r2_test = R2(output[idx_test], labels[idx_test]) labels, output, adj = labels.cpu(), output.cpu(), adj.cpu() #adj = torch.FloatTensor(np.load("test_lp/raw_S.npy")) lp_output = lp_refine(idx_test, idx_train, labels, output, adj) lp_r2_test = R2(lp_output, labels[idx_test]) print("Test set ({}) results:".format(test_meta), "loss= {:.4f}".format(loss_test.item()), "R2= {:.4f}".format(r2_test.item()), "LP_R2= {:.4f}\n".format(lp_r2_test.item()))
def train(epoch): t = time.time() # without logdet model.train() optimizer.zero_grad() idx = idx_train_lst[epoch] output = model(features, adj).view(-1) r2_train = R2(output[idx], labels[idx]) loss_train = loss(output, labels, idx, adj, coeffs, False) loss_train.backward() optimizer.step() # with logdet if epoch % 10 == 0: model.train() coeffs_optimizer.zero_grad() output = model(features, adj).view(-1) loss_train = loss(output, labels, idx, adj, coeffs, True) loss_train.backward() coeffs_optimizer.step() if not args.fastmode: # Evaluate validation set performance separately, # deactivates dropout during validation run. model.eval() output = model(features, adj) loss_val = loss(output, labels, idx_val, adj, coeffs, True) r2_val = R2(output[idx_val], labels[idx_val]) print('Epoch: {:04d}'.format(epoch + 1), 'loss_train: {:.4f}'.format(loss_train.item()), 'r2_train: {:.4f}'.format(r2_train.item()), 'loss_val: {:.4f}'.format(loss_val.item()), 'r2_val: {:.4f}'.format(r2_val.item()), 'alpha: {}'.format( torch.tanh(coeffs[0])), 'beta: {}'.format(torch.exp(coeffs[1])), 'time: {:.4f}s'.format(time.time() - t))
def treeRegression(tree_depth, x_train, x_test, y_train, y_test): regr = DecisionTreeRegressor(max_depth=tree_depth) regr.fit(x_train, y_train) return R2(regr.predict(x_test), y_test)