-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_nfcf_career_recommend.py
192 lines (151 loc) · 8.02 KB
/
run_nfcf_career_recommend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
"""
Created on Fri May 1 02:13:41 2020
@author: Anonymous Authors
"""
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score, roc_auc_score,f1_score,recall_score
import heapq # for retrieval topK
from utilities import get_instances_with_random_neg_samples, get_test_instances_with_random_samples
from performance_and_fairness_measures import getHitRatio, getNDCG, differentialFairnessMultiClass, computeEDF, computeAbsoluteUnfairness
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torch.nn.functional as F
from collaborative_models import neuralCollabFilter
#%%The function below ensures that we seed all random generators with the same value to get reproducible results
def set_random_seed(state=1):
gens = (np.random.seed, torch.manual_seed, torch.cuda.manual_seed)
for set_state in gens:
set_state(state)
RANDOM_STATE = 1
set_random_seed(RANDOM_STATE)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#%% loss function for differential fairness
def criterionHinge(epsilonClass, epsilonBase):
zeroTerm = torch.tensor(0.0).to(device)
return torch.max(zeroTerm, (epsilonClass-epsilonBase))
#%% fine-tuning pre-trained model with user-career pairs
def fair_fine_tune_model(model,df_train, epochs, lr,batch_size,num_negatives,num_items,protectedAttributes,lamda,epsilonBase,unsqueeze=False):
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=1e-6)
model.train()
all_user_input = torch.LongTensor(df_train['user_id'].values).to(device)
all_item_input = torch.LongTensor(df_train['like_id'].values).to(device)
for i in range(epochs):
j = 0
for batch_i in range(0,np.int64(np.floor(len(df_train)/batch_size))*batch_size,batch_size):
data_batch = (df_train[batch_i:(batch_i+batch_size)]).reset_index(drop=True)
train_user_input, train_item_input, train_ratings = get_instances_with_random_neg_samples(data_batch, num_items, num_negatives,device)
if unsqueeze:
train_ratings = train_ratings.unsqueeze(1)
y_hat = model(train_user_input, train_item_input)
loss1 = criterion(y_hat, train_ratings)
predicted_probs = model(all_user_input, all_item_input)
avg_epsilon = computeEDF(protectedAttributes,predicted_probs,num_items,all_item_input,device)
loss2 = criterionHinge(avg_epsilon, epsilonBase)
loss = loss1 + lamda*loss2
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('epoch: ', i, 'batch: ', j, 'out of: ',np.int64(np.floor(len(df_train)/batch_size)), 'average loss: ',loss.item())
j = j+1
#%% model evaluation: hit rate and NDCG
def evaluate_fine_tune(model,df_val,top_K,random_samples, num_items):
model.eval()
avg_HR = np.zeros((len(df_val),top_K))
avg_NDCG = np.zeros((len(df_val),top_K))
for i in range(len(df_val)):
test_user_input, test_item_input = get_test_instances_with_random_samples(df_val[i], random_samples,num_items,device)
y_hat = model(test_user_input, test_item_input)
y_hat = y_hat.cpu().detach().numpy().reshape((-1,))
test_item_input = test_item_input.cpu().detach().numpy().reshape((-1,))
map_item_score = {}
for j in range(len(y_hat)):
map_item_score[test_item_input[j]] = y_hat[j]
for k in range(top_K):
# Evaluate top rank list
ranklist = heapq.nlargest(k, map_item_score, key=map_item_score.get)
gtItem = test_item_input[0]
avg_HR[i,k] = getHitRatio(ranklist, gtItem)
avg_NDCG[i,k] = getNDCG(ranklist, gtItem)
avg_HR = np.mean(avg_HR, axis = 0)
avg_NDCG = np.mean(avg_NDCG, axis = 0)
return avg_HR, avg_NDCG
#%%
def fairness_measures(model,df_val,num_items,protectedAttributes):
model.eval()
user_input = torch.LongTensor(df_val['user_id'].values).to(device)
item_input = torch.LongTensor(df_val['like_id'].values).to(device)
y_hat = model(user_input, item_input)
avg_epsilon = computeEDF(protectedAttributes,y_hat,num_items,item_input,device)
U_abs = computeAbsoluteUnfairness(protectedAttributes,y_hat,num_items,item_input,device)
avg_epsilon = avg_epsilon.cpu().detach().numpy().reshape((-1,)).item()
print(f"average differential fairness: {avg_epsilon: .3f}")
U_abs = U_abs.cpu().detach().numpy().reshape((-1,)).item()
print(f"absolute unfairness: {U_abs: .3f}")
#%% load data
train_users= pd.read_csv("train-test/train_usersID.csv",names=['user_id'])
test_users = pd.read_csv("train-test/test_usersID.csv",names=['user_id'])
train_careers= pd.read_csv("train-test/train_concentrationsID.csv",names=['like_id'])
test_careers = pd.read_csv("train-test/test_concentrationsID.csv",names=['like_id'])
train_protected_attributes= pd.read_csv("train-test/train_protectedAttributes.csv")
test_protected_attributes = pd.read_csv("train-test/test_protectedAttributes.csv")
# =============================================================================
# train_labels= pd.read_csv("train-test/train_labels.csv",names=['labels'])
# test_labels = pd.read_csv("train-test/test_labels.csv",names=['labels'])
#
# unique_concentrations = (pd.concat([train_careers['like_id'],train_labels['labels']],axis=1)).reset_index(drop=True)
# unique_concentrations = unique_concentrations.drop_duplicates(subset='like_id', keep='first')
#
# unique_careers = unique_concentrations.sort_values(by=['like_id']).reset_index(drop=True)
# unique_careers.to_csv('train-test/unique_careers.csv',index=False)
# =============================================================================
unique_careers= pd.read_csv("train-test/unique_careers.csv")
train_userPages = pd.read_csv("train-test/train_userPages.csv")
train_data = (pd.concat([train_users['user_id'],train_careers['like_id']],axis=1)).reset_index(drop=True)
test_data = (pd.concat([test_users['user_id'],test_careers['like_id']],axis=1)).reset_index(drop=True)
#%% set hyperparameters
emb_size = 128
hidden_layers = np.array([emb_size, 64, 32, 16])
output_size = 1
num_epochs = 10
learning_rate = 0.001
batch_size = 256
num_negatives = 5
random_samples = 15
top_K = 10
# to load pre-train model correctly
num_uniqueUsers = len(train_userPages.user_id.unique())
num_uniqueLikes = len(train_userPages.like_id.unique())
# to fine tune career recommendation
num_uniqueCareers = len(train_data.like_id.unique())
train_gender = train_protected_attributes['gender'].values
test_gender = test_protected_attributes['gender'].values
fairness_thres = torch.tensor(0.1).to(device)
epsilonBase = torch.tensor(0.0).to(device)
#%% load pre-trained model
DF_NCF = neuralCollabFilter(num_uniqueUsers, num_uniqueLikes, emb_size, hidden_layers,output_size).to(device)
DF_NCF.load_state_dict(torch.load("trained-models/preTrained_NCF"))
DF_NCF.to(device)
#%% fine-tune to career recommendation
# replace page items with career items
DF_NCF.like_emb = nn.Embedding(num_uniqueCareers,emb_size).to(device)
# freeze user embedding
DF_NCF.user_emb.weight.requires_grad=False
# load debiased user embeddings
debias_users_embed = np.loadtxt('results/debias_users_embed.txt')
# replace user embedding of the model with debiased embeddings
DF_NCF.user_emb.weight.data = torch.from_numpy(debias_users_embed.astype(np.float32)).to(device)
fair_fine_tune_model(DF_NCF,train_data, num_epochs, learning_rate,batch_size,num_negatives,num_uniqueCareers,train_gender,fairness_thres,epsilonBase, unsqueeze=True)
torch.save(DF_NCF.state_dict(), "trained-models/DF_NCF")
#%% evaluate the fine-tune model
import sys
sys.stdout=open("NFCF_output.txt","w")
avg_HR_DF_NCF, avg_NDCG_DF_NCF = evaluate_fine_tune(DF_NCF,test_data.values,top_K,random_samples, num_uniqueCareers)
np.savetxt('results/avg_HR_NFCF.txt',avg_HR_DF_NCF)
np.savetxt('results/avg_NDCG_NFCF.txt',avg_NDCG_DF_NCF)
#%% fairness measurements
fairness_measures(DF_NCF,test_data,num_uniqueCareers,test_gender)