def run_project_experiment(X_train, X_test, X_gender_train, X_gender_test, y_train, y_test, y_gender_train, y_gender_test, directory, i): _, proj_compl = get_sensitive_directions_and_projection_matrix(X_gender_train, y_gender_train, X_gender_test, y_gender_test) np.save(directory+'proj_compl_'+str(i), proj_compl) X_train_proj = X_train@proj_compl X_test_proj = X_test@proj_compl weights, train_logits, test_logits = SenSR.train_nn( X_train_proj, y_train, X_test = X_test_proj, y_test = y_test, n_units=[100], l2_reg=0., lr = .00001, batch_size=1000, epoch=60000, verbose=False) return weights, train_logits, test_logits, proj_compl
def get_sensitive_directions_and_projection_matrix(X_gender_train, y_gender_train, X_gender_test, y_gender_test, gender_race_features_idx = [39, 40], gender_idx = 39 ): ''' Description: Get the sensitive directions and projection matrix. he sensitive directions include the race and gender direction as well as the learned hyperplane that predicts gender (without using gender as a predictive feature of course). ''' weights, train_logits, test_logits = SenSR.train_nn(X_gender_train, y_gender_train, X_test = X_gender_test, y_test = y_gender_test, n_units=[], l2_reg=.1, batch_size=5000, epoch=5000, verbose=False) # all_mean = X_gender_train.mean(axis=0) # m0 = X_gender_train[y_gender_train[:,0]==1].mean(axis=0) - all_mean # m1 = X_gender_train[y_gender_train[:,1]==1].mean(axis=0) - all_mean # weights = np.vstack((m0,m1)).T n, d = weights[0].shape print(n,d) sensitive_directions = [] # transform the n-dimensional weights back into the full n+1 dimensional space where the gender coordinate is zeroed out full_weights = np.zeros((n+1,d)) #before the gender coordinate, the coordinates of the full_weights and learned weights correspond to the same features for i in range(gender_idx): full_weights[i,:] = weights[0][i,:] #after the gender coordinate, the i-th coordinate of the full_weights correspond to the (i-1)-st coordinate of the learned weights for i in range(gender_idx+1, n+1): full_weights[i, :] = weights[0][i-1,:] sensitive_directions.append(full_weights.T) for idx in gender_race_features_idx: temp_direction = np.zeros((n+1,1)).reshape(1,-1) temp_direction[0, idx] = 1 sensitive_directions.append(np.copy(temp_direction)) sensitive_directions = np.vstack(sensitive_directions) # tSVD = TruncatedSVD(n_components= 2 + len(gender_race_features_idx)) # tSVD.fit(sensitive_directions) # sensitive_directions = tSVD.components_ return sensitive_directions, SenSR.compl_svd_projector(sensitive_directions)
def run_baseline_experiment(X_train, y_train, X_test, y_test): return SenSR.train_nn(X_train, y_train, X_test = X_test, y_test = y_test, n_units=[100], l2_reg=0., lr = .00001, batch_size=1000, epoch=60000, verbose=False)