-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunny_lr_bs3.py
339 lines (251 loc) · 11 KB
/
sunny_lr_bs3.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# -*- coding: utf-8 -*-
# @Time : 2018/5/13 下午5:03
# @Author : Zhixin Piao
# @Email : [email protected]
import numpy as np
import pickle
import random
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils import data
import torchvision
import torchvision.transforms as transforms
def class_balanced_cross_entropy_loss(output, label, size_average=True, batch_average=True, use_balance=True):
"""Define the class balanced cross entropy loss to train the network
Args:
output: Output of the network
label: Ground truth label
Returns:
Tensor that evaluates the loss
"""
labels = label.float()
num_labels_pos = torch.sum(labels)
num_labels_neg = torch.sum(1.0 - labels)
num_total = num_labels_pos + num_labels_neg
output_gt_zero = torch.ge(output, 0).float()
loss_val = torch.mul(output, (labels - output_gt_zero)) - torch.log(
1 + torch.exp(output - 2 * torch.mul(output, output_gt_zero)))
loss_pos = torch.sum(-torch.mul(labels, loss_val))
loss_neg = torch.sum(-torch.mul(1.0 - labels, loss_val))
if use_balance:
final_loss = num_labels_neg / num_total * loss_pos + num_labels_pos / num_total * loss_neg
else:
final_loss = 0.5 * loss_pos + 0.5 * loss_neg
if num_labels_pos == 0:
final_loss = loss_neg
if num_labels_neg == 0:
final_loss = loss_pos
if size_average:
final_loss /= int(np.prod(label.size()))
elif batch_average:
final_loss /= int(label.size()[0])
return final_loss
def load_data(data_path, use_tensor=False, use_cuda=False):
"""
:param data_path:
:return: data_package = {
feature_standard_weight_list: 54
train_input: (7323, 54)
train_target: (7323, 2)
train_cust: (7323, 29)
train_cust: (7323, 29)
val_cust: (814, 29)
val_target: (814, 2)
}
"""
with open(data_path, 'rb') as f:
data_package = pickle.load(f)
train_input, train_cust, train_target = data_package['train_input'], data_package['train_cust'], data_package['train_target']
val_input, val_cust, val_target = data_package['val_input'], data_package['val_cust'], data_package['val_target']
train_cust = train_cust[:, -5:]
val_cust = val_cust[:, -5:]
train_responded, train_profit = train_target[:, 0:1], train_target[:, 1:2]
train_recommend = (train_responded == 1) * (train_profit > 30)
val_responded, val_profit = val_target[:, 0:1], val_target[:, 1:2]
val_recommend = (val_responded == 1) * (val_profit > 30)
train_recommend = train_recommend.reshape(-1, 1)
val_recommend = val_recommend.reshape(-1, 1)
ret_var = (train_input, train_cust, train_responded, train_profit, train_recommend,
val_input, val_cust, val_responded, val_profit, val_recommend)
if use_tensor:
if use_cuda:
ret_var = (torch.from_numpy(val.astype(np.float32)).cuda() for val in ret_var)
else:
ret_var = (torch.from_numpy(val.astype(np.float32)) for val in ret_var)
return ret_var
def compute_profit(pred_recommend, gt_profit):
"""
:param pred_recommend: [N, 1] value in {0, 1}
:param gt_profit: [N, 1]
:return profit: float
"""
profit = ((gt_profit - 30) * pred_recommend).sum()
return profit.item()
def compute_accuracy(pred_recommend, gt_recommend):
"""
:param pred_recommend: (N, 1)
:param gt_recommend: (N, 1)
:return accuracy float
"""
sample_num = gt_recommend.size(0)
gt_recommend_num = gt_recommend.sum().item()
total_precision = (pred_recommend == gt_recommend).sum().item() / sample_num
recommend_recall = (pred_recommend * gt_recommend).sum().item() / gt_recommend_num
return total_precision, recommend_recall
class MLR4(nn.Module):
def __init__(self, feature_num):
super(MLR4, self).__init__()
self.feature_num = feature_num
self.fc1 = nn.Linear(feature_num, 32)
self.fc2 = nn.Linear(32, 16)
self.fc3 = nn.Linear(16, 8)
self.fc4 = nn.Linear(16, 1)
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = self.fc4(x)
return x
class MLR2(nn.Module):
def __init__(self, feature_num):
super(MLR2, self).__init__()
self.feature_num = feature_num
self.fc1 = nn.Linear(feature_num, 32)
self.bn1 = nn.BatchNorm1d(32)
self.fc2 = nn.Linear(32, 1)
def forward(self, x):
x = self.bn1(F.relu(self.fc1(x)))
x = self.fc2(x)
return x
class MLR3(nn.Module):
def __init__(self, feature_num):
super(MLR3, self).__init__()
self.feature_num = feature_num
self.fc1 = nn.Linear(feature_num, 16)
self.fc2 = nn.Linear(16, 16)
# self.fc3 = nn.Linear(16, 1)
# self.fc4 = nn.Linear(32, 32)
# self.fc5 = nn.Linear(16, 1)
self.fc0p = nn.Linear(feature_num, 1)
self.fc1p = nn.Linear(16, 1)
self.fc2p = nn.Linear(16, 1)
self.bn1 = nn.BatchNorm1d(16)
self.bn2 = nn.BatchNorm1d(16)
self.bn3 = nn.BatchNorm1d(16)
self.bn4 = nn.BatchNorm1d(16)
def forward(self, x):
x1 = self.bn1(F.relu(self.fc1(x)))
x2 = self.bn2(F.relu(self.fc2(x1)))
# x3 = self.bn3(F.relu(self.fc3(x2))) + x2
# x4 = self.bn4(F.relu(self.fc4(x3))) + x3
x0p = self.fc0p(x)
x1p = self.fc1p(x1)
x2p = self.fc1p(x2)
x = x0p + x1p + x2p
return x
class LR(nn.Module):
def __init__(self, feature_num):
super(LR, self).__init__()
self.feature_num = feature_num
self.fc1 = nn.Linear(feature_num, 1)
def forward(self, x):
x = self.fc1(x)
return x
class MultiTaskModel(nn.Module):
def __init__(self, total_feature_num, cust_feature_num):
super(MultiTaskModel, self).__init__()
# self.fc1 = nn.Linear(feature_num, 32)
# self.fc2 = nn.Linear(32, 32)
# self.fc3 = nn.Linear(64, 64)
# self.fc4 = nn.Linear(64, 32)
self.responded_model = LR(total_feature_num)
self.profit_model = MLR3(cust_feature_num)
def forward(self, total_x, cust_x):
# x = F.relu(self.fc1(x))
# x = self.fc2(x)
# x = F.relu(self.fc3(x))
# x = self.fc4(x)
# x = self.fc1(x)
responded = self.responded_model(total_x)
profit = self.profit_model(cust_x)
return responded, profit
def compute_result(model, input_data, cust_data, responded_data, profit_data, recommend_data, print_details=True):
with torch.no_grad():
pred_responded, pred_profit = model(input_data, cust_data)
loss, responded_loss, profit_loss, final_profit_loss = multi_task_loss(pred_responded, pred_profit, responded_data, profit_data)
loss, responded_loss, profit_loss, final_profit_loss = loss.item(), responded_loss.item(), profit_loss.item(), final_profit_loss.item()
pred_responded = F.sigmoid(pred_responded)
# pred_recommend = torch.ge(pred_responded, 0.4).float() * torch.ge(pred_profit, 30).float()
# pred_recommend = torch.ge(pred_recommend, 30).float()
pred_recommend = torch.ge(pred_responded * pred_profit, 30).float()
profit = compute_profit(pred_recommend, profit_data)
total_precision, recommend_recall = compute_accuracy(pred_recommend, recommend_data)
if print_details:
print('loss: %.9f responded_loss: %.9f, profit_loss: %.9f, final_profit_loss: %.9f' % (loss, responded_loss, profit_loss, final_profit_loss))
print('total_precision: %s, recommend_recall: %s' % (total_precision, recommend_recall))
print('profit: ', profit)
return loss, profit, total_precision, recommend_recall
def multi_task_loss(pred_responded, pred_profit, gt_responded, gt_profit):
# sample num
sample_num = gt_responded.size(0)
# responded loss
responded_loss = class_balanced_cross_entropy_loss(pred_responded, gt_responded)
# profit loss
pos_num = (gt_responded == 1).sum().item()
neg_num = sample_num - pos_num
pos_pred_profit = pred_profit[gt_responded == 1]
pos_gt_profit = gt_profit[gt_responded == 1]
pos_profit_loss = ((pos_pred_profit - pos_gt_profit) ** 2).sum() / sample_num / 2
neg_pred_profit = pred_profit[gt_responded == 0]
neg_gt_profit = gt_profit[gt_responded == 0]
neg_profit_loss = ((neg_pred_profit - neg_gt_profit) ** 2).sum() / sample_num / 2
profit_loss = 4e-4 * (neg_num / sample_num * pos_profit_loss + pos_num / sample_num * neg_profit_loss)
# final profit
gt_final_profit = gt_profit[gt_profit >= 30].sum() / sample_num
pred_final_profit = (F.sigmoid(pred_responded) * pred_profit).mean()
final_profit_loss = 1e-3 * (gt_final_profit - pred_final_profit) ** 2 / 2
total_loss = responded_loss + profit_loss + final_profit_loss
return total_loss, responded_loss, profit_loss, final_profit_loss
def logistic_regression(data_type, model_name):
"""
:param data_type: 'zero', 'average', 'sample'
:param model_name: 'MLR4', 'MLR2', 'LR'
:return:
"""
# load data
train_input, train_cust, train_responded, train_profit, train_recommend, val_input, val_cust, val_responded, val_profit, val_recommend = \
load_data('data/%s/train.data' % data_type, use_tensor=True, use_cuda=True)
total_train_num, total_feature_num = train_input.size()
cust_feature_num = train_cust.size(1)
if model_name == 'MLR4':
base_model = MLR4
elif model_name == 'MLR2':
base_model = MLR2
elif model_name == 'LR':
base_model = LR
else:
raise Exception('error model name!!!')
model = MultiTaskModel(total_feature_num, cust_feature_num).cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=5e-3, weight_decay=2e-3)
# Train the model
num_epochs = 10000
train_input, train_cust, train_responded = train_input.requires_grad_(), train_cust.requires_grad_(), train_responded.requires_grad_()
# Start Train
for epoch in range(1, num_epochs + 1):
train_pred_responded, train_pred_profit = model(train_input, train_cust)
loss, responded_loss, profit_loss, final_profit_loss = multi_task_loss(train_pred_responded, train_pred_profit, train_responded, train_profit)
if epoch % 100 == 0:
print('[%d/%d]: loss: %.9f, responded_loss: %.9f, profit_loss: %.9f, final_profit_loss: %.9f' % (
epoch, num_epochs, loss.item(), responded_loss.item(), profit_loss.item(), final_profit_loss.item()))
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('Train:')
compute_result(model, train_input, train_cust, train_responded, train_profit, train_recommend)
print('Val:')
compute_result(model, val_input, val_cust, val_responded, val_profit, val_recommend)
def main():
logistic_regression(data_type='sample', model_name='MLR4')
if __name__ == '__main__':
main()