-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBayesianSmoothing.py
170 lines (141 loc) · 5.62 KB
/
BayesianSmoothing.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import os
from utils.train_parse_args import parse_args
from utils.dataloader import load_local_data
import random
import scipy.special as special
from utils.s3utils import S3FileSystemPatched
import s3fs
import pandas as pd
from utils.s3utils import S3Filewrite, S3FileSystemPatched, S3Filewrite_bayes
def save_to_s3(src_path, dst_path):
cmd = 's3cmd put -r ' + src_path + ' ' + dst_path
os.system(cmd)
def load_s3_data(path):
s3fs.S3FileSystem = S3FileSystemPatched
fs = s3fs.S3FileSystem()
input_files = sorted([file for file in fs.ls(path) if file.find("part-") != -1])
print(input_files[:3])
index = 0
for file in input_files:
if index == 0:
feature = pd.read_csv("s3://" + file, header=None).values
if index > 0:
feature_ = pd.read_csv("s3://" + file, header=None).values
feature = np.r_[feature, feature_]
index += 1
print(index)
feature = np.array(feature)
return feature
class BayesianSmoothing(object):
def __init__(self, alpha, beta):
self.alpha = alpha
self.beta = beta
def sample(self, alpha, beta, num, imp_upperbound):
sample = np.random.beta(alpha, beta, num)
I = []
C = []
for clk_rt in sample:
imp = random.random() * imp_upperbound
imp = imp_upperbound
clk = imp * clk_rt
I.append(imp)
C.append(clk)
return I, C
def update(self, imps, clks, iter_num, epsilon):
for i in range(iter_num):
new_alpha, new_beta = self.__fixed_point_iteration(imps, clks, self.alpha, self.beta)
if abs(new_alpha-self.alpha)<epsilon and abs(new_beta-self.beta)<epsilon:
break
self.alpha = new_alpha
self.beta = new_beta
def __fixed_point_iteration(self, imps, clks, alpha, beta):
numerator_alpha = 0.00000001
numerator_beta = 0.00000001
denominator = 0.00000001
for i in range(len(imps)):
numerator_alpha += (special.digamma(clks[i]+alpha) - special.digamma(alpha))
numerator_beta += (special.digamma(imps[i]-clks[i]+beta) - special.digamma(beta))
denominator += (special.digamma(imps[i]+alpha+beta) - special.digamma(alpha+beta))
# print(denominator)
return alpha*(numerator_alpha/denominator), beta*(numerator_beta/denominator)
def train(args):
# 1. load and split data
file_path = args.data_input.split(',')[0]
base_p_path = args.data_input.split(',')[1]
feature = load_s3_data(file_path)
s3fs.S3FileSystem = S3FileSystemPatched
fs = s3fs.S3FileSystem()
input_file = sorted([file for file in fs.ls(base_p_path) if file.find("part-") != -1])
[base_alpha, base_beta] = list(map(float, pd.read_csv("s3://" + input_file[0], header=None).values[0]))
print("data_loading done!")
id, I, C = feature[:, 0], feature[:, 1], feature[:, 2]
pre = []
alpha = []
beta = []
nodeid = []
for i in range(len(I)):
nodeid.append(id[i])
x, y = list(map(float, str(I[i]).split(" "))), list(map(float, str(C[i]).split(" ")))
bs = BayesianSmoothing(base_alpha, base_beta)
bs.update(x, y, args.iter_num, args.epsilon)
ctr = []
for j in range(len(x)):
ctr.append((y[j]+bs.alpha)/(x[j]+bs.alpha+bs.beta))
pre.append(ctr[-1])
alpha.append(bs.alpha)
beta.append(bs.beta)
if i%10000 == 0:
print("pross:",i)
batch_size = 60000
for idx in range(0, len(id), batch_size):
s3writer = S3Filewrite_bayes(args)
s3writer.write(nodeid[idx:idx + batch_size], pre[idx:idx + batch_size], alpha[idx:idx + batch_size], beta[idx:idx + batch_size], idx)
print("write_batch_60000:", idx)
def train_local(args):
# 1. load and split data
# file_path = args.data_input.split(',')[0]
# base_p_path = args.data_input.split(',')[1]
# feature = load_s3_data(file_path)
# s3fs.S3FileSystem = S3FileSystemPatched
# fs = s3fs.S3FileSystem()
# input_file = sorted([file for file in fs.ls(base_p_path) if file.find("part-") != -1])
file_path = args.file_path
feature = pd.read_csv(file_path, header=0).values
# alo
base_alpha = args.alpha
base_beta = args.beta
# [base_alpha, base_beta] = list(map(float, pd.read_csv("s3://" + input_file[0], header=None).values[0]))
print("data_loading done!")
id, I, C = feature[:, 0], feature[:, 1], feature[:, 2]
pre = []
alpha = []
beta = []
nodeid = []
for i in range(len(I)):
nodeid.append(id[i])
x, y = list(map(float, str(I[i]).split(" "))), list(map(float, str(C[i]).split(" ")))
bs = BayesianSmoothing(base_alpha, base_beta)
bs.update(x, y, args.iter_num, args.epsilon)
ctr = []
for j in range(len(x)):
ctr.append((y[j]+bs.alpha)/(x[j]+bs.alpha+bs.beta))
pre.append(ctr[-1])
alpha.append(bs.alpha)
beta.append(bs.beta)
if i%10000 == 0:
print("pross:",i)
batch_size = 60000
# save to csv file
df = pd.DataFrame({'nodeid': nodeid, 'pre': pre, 'alpha': alpha, 'beta': beta})
df.to_csv(args.save_path, index=False)
if __name__ == '__main__':
args = parse_args()
# train(args)
train_local(args)