forked from shiba24/learning2rank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRankNet.py
executable file
·238 lines (199 loc) · 9.19 KB
/
RankNet.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# -*- coding: utf-8 -*-
import sys,os
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../')
import numpy as np
import six
import pickle
import scipy
import chainer
import chainer.functions as F
import chainer.links as L
from chainer import optimizers
from chainer import serializers
from tqdm import tqdm
import scipy.stats as ss
from sklearn.preprocessing import StandardScaler
from learning2rank.utils import plot_result
from learning2rank.utils import NNfuncs
######################################################################################
# Define model
class Model(chainer.Chain):
"""
RankNet - Pairwise comparison of ranking.
The original paper:
http://research.microsoft.com/en-us/um/people/cburges/papers/ICML_ranking.pdf
Japanese only:
http://qiita.com/sz_dr/items/0e50120318527a928407
"""
def __init__(self, n_in, n_units1, n_units2, n_out):
super(Model, self).__init__(
l1=L.Linear(n_in, n_units1),
l2=L.Linear(n_units1, n_units2),
l3=L.Linear(n_units2, n_out),
)
def __call__(self, x_i, x_j, t_i, t_j):
s_i = self.l3(F.relu(self.l2(F.relu(self.l1(x_i)))))
s_j = self.l3(F.relu(self.l2(F.relu(self.l1(x_j)))))
s_diff = s_i - s_j
if t_i.data > t_j.data:
S_ij = 1
elif t_i.data < t_j.data:
S_ij = -1
else:
S_ij = 0
self.loss = (1 - S_ij) * s_diff / 2. + F.log(1 + F.exp(-s_diff))
return self.loss
def predict(self, x):
h1 = F.relu(self.l1(x))
h2 = F.relu(self.l2(h1))
h = F.relu(self.l3(h2))
return h.data
class RankNet(NNfuncs.NN):
"""
RankNet training function.
Usage (Initialize):
RankModel = RankNet()
Usage (Traininng):
Model.fit(X, y)
With options:
Model.fit(X, y, batchsize=100, n_iter=5000, n_units1=512, n_units2=128, tv_ratio=0.95, optimizerAlgorithm="Adam", savefigName="result.pdf", savemodelName="RankNet.model")
"""
def __init__(self, resumemodelName=None, verbose=True):
self.resumemodelName = resumemodelName
self.train_loss, self.test_loss = [], []
self._verbose = verbose
if resumemodelName is not None:
print("load resume model!")
self.loadModel(resumemodelName)
# Evaluation function of NDCG@100
def ndcg(self, y_true, y_score, k=100):
y_true = y_true.ravel()
y_score = y_score.ravel()
y_true_sorted = sorted(y_true, reverse=True)
ideal_dcg = 0
for i in range(k):
ideal_dcg += (2 ** y_true_sorted[i] - 1.) / np.log2(i + 2)
dcg = 0
argsort_indices = np.argsort(y_score)[::-1]
for i in range(k):
dcg += (2 ** y_true[argsort_indices[i]] - 1.) / np.log2(i + 2)
ndcg = dcg / ideal_dcg
return ndcg
# Training function
def trainModel(self, x_train, y_train, x_test, y_test, n_iter):
sigma = 5.0
loss_step = 100
for step in tqdm(range(n_iter)):
i, j = np.random.randint(len(x_train), size=2)
x_i = chainer.Variable(x_train[i].reshape(1, -1))
x_j = chainer.Variable(x_train[j].reshape(1, -1))
y_i = chainer.Variable(y_train[i])
y_j = chainer.Variable(y_train[j])
self.optimizer.update(self.model, x_i, x_j, y_i, y_j)
if (step + 1) % loss_step == 0:
train_score = self.model.predict(chainer.Variable(x_train))
test_score = self.model.predict(chainer.Variable(x_test))
train_ndcg = self.ndcg(y_train, train_score)
test_ndcg = self.ndcg(y_test, test_score)
self.train_loss.append(train_ndcg)
self.test_loss.append(test_ndcg)
if self._verbose:
print("step: {0}".format(step + 1))
print("NDCG@100 | train: {0}, test: {1}".format(train_ndcg, test_ndcg))
def fit(self, fit_X, fit_y, batchsize=100, n_iter=5000, n_units1=512, n_units2=128, tv_ratio=0.95, optimizerAlgorithm="Adam", savefigName="result.pdf", savemodelName="RankNet.model"):
train_X, train_y, validate_X, validate_y = self.splitData(fit_X, fit_y, tv_ratio)
print("The number of data, train:", len(train_X), "validate:", len(validate_X))
if self.resumemodelName is None:
self.initializeModel(Model, train_X, n_units1, n_units2, optimizerAlgorithm)
self.trainModel(train_X, train_y, validate_X, validate_y, n_iter)
plot_result.acc(self.train_loss, self.test_loss, savename=savefigName)
self.saveModels(savemodelName)
################################################################################################
## end of file ##
################################################################################################
##################
## MEMO
#
# # loss func of ranknet
# def ndcg(y_true, y_score, k=100):
# y_true = y_true.ravel()
# y_score = y_score.ravel()
# y_true_sorted = sorted(y_true, reverse=True)
# ideal_dcg = 0
# for i in range(k):
# ideal_dcg += (2 ** y_true_sorted[i] - 1.) / np.log2(i + 2)
# dcg = 0
# argsort_indices = np.argsort(y_score)[::-1]
# for i in range(k):
# dcg += (2 ** y_true[argsort_indices[i]] - 1.) / np.log2(i + 2)
# ndcg = dcg / ideal_dcg
# return ndcg
# ######################################################################################
# # training func of ranknet
# def trainRankNet(model, optimizer, x_train, y_train, x_test, y_test, n_iter, N_train, N_test):
# sigma = 5.0
# loss_step = 100
# train_ndcgs, test_ndcgs = [], []
# for step in tqdm(range(n_iter)):
# i, j = np.random.randint(N_train, size=2)
# x_i = chainer.Variable(x_train[i].reshape(1, -1))
# x_j = chainer.Variable(x_train[j].reshape(1, -1))
# y_i = chainer.Variable(y_train[i])
# y_j = chainer.Variable(y_train[j])
# optimizer.update(model, x_i, x_j, y_i, y_j)
# if (step + 1) % loss_step == 0:
# train_score = model.predict(chainer.Variable(x_train))
# test_score = model.predict(chainer.Variable(x_test))
# train_ndcg = ndcg(y_train, train_score)
# test_ndcg = ndcg(y_test, test_score)
# train_ndcgs.append(train_ndcg)
# test_ndcgs.append(test_ndcg)
# print("step: {0}".format(step + 1))
# print("NDCG@100 | train: {0}, test: {1}".format(train_ndcg, test_ndcg))
# return model, optimizer, train_ndcg, test_ndcg
# ################################################################################################
# ## 学習部分 ##
# ################################################################################################
# # n_iter は、多分めちゃめちゃ多い方がよいと思う。イメージ、データ数くらい?
# def fit(fit_X, fit_y, batchsize=100, n_iter=5000, n_units1=512, n_units2=128, tv_ratio=0.95, optimizerAlgorithm="Adam", savefigName="result.png", savemodelName="Spearman.model", resumemodelName=None):
# print('load dataset')
# perm = np.random.permutation(len(fit_X))
# N_train = np.floor(len(fit_X) * tv_ratio)
# train_X, validate_X = np.split(fit_X[perm].astype(np.float32), [N_train])
# train_y, validate_y = np.split(fit_Y[perm].astype(np.float32).reshape(len(fit_Y), 1), [N_train])
# N_validate = len(validate_y)
# print("The number of data, train:",N_train, "validate:", N_validate) # トレーニングとテストのデータ数を表示
# if resumemodelName is None:
# print("prepare initialized model!")
# model = Model(len(x_train[0]), n_units1, n_units2, 1)
# if optimizerAlgorithm == "Adam":
# optimizer = optimizers.Adam()
# elif optimizerAlgorithm == "AdaGrad":
# optimizer = optimizers.AdaGrad()
# elif optimizerAlgorithm == "SGD":
# optimizer = optimizers.MomentumSGD()
# else:
# raise ValueError('could not find %s in optimizers {"Adam", "AdaGrad", "SGD"}' % (optimizerAlgorithm))
# optimizer.setup(model)
# else:
# print("load resume model!")
# model, optimizer = loadModel(resumemodelName)
# model, optimizer, train_loss, test_loss = trainRankNet(model, optimizer, train_X, train_y, validate_X, validate_y, n_iter, N_train, N_validate)
# # plot_result(train_loss, test_loss, savename=savefigName)
# print('save the model')
# serializers.save_hdf5(savemodelName, model)
# print('save the optimizer')
# serializers.save_hdf5(savemodelName[:-5]+ 'state', optimizer)
# return model
# ################################################################################################
# ## 予測 ##
# ################################################################################################
# def predict(model, predict_X, batchsize=100):
# predict_y = predictTargets(model, predict_X.astype(np.float32), batchsize)
# return predict_y
# def loadModel(modelName):
# print('Load model')
# serializers.load_hdf5(modelName, model)
# print('Load optimizer state')
# serializers.load_hdf5(modelName[:-5] + 'state', optimizer)
# return model, optimizer