-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_lambda_dcn.py
129 lines (106 loc) · 4.66 KB
/
run_lambda_dcn.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
import pandas as pd
import tensorflow as tf
import json
from ctr4keras import FeatureType
from ctr4keras.preprocessing import SimplePreprocessor, fillna
from ctr4keras.models import DCN, LambdaRanker, DIN, DeepFM
from ctr4keras.snippets import HistoryExt, Plot, save_model_as_savedmodel, set_seed, train_test_split
from ctr4keras.metrics import evaluate_report, rank_evaluate
set_seed(2021)
pd.set_option("display.max_row", 200)
pd.set_option("display.max_column", 200)
pd.set_option('display.width', 100)
pd.set_option('max_colwidth', 100)
def main():
# 1. feature preprocess
data = pd.read_pickle("/home/baojunshan/douya-ctr/ltr/train_data_220607_lgb.pkl")
ftr_names = json.load(open('/home/baojunshan/douya-ctr/ltr/train_data_ftr_name_220607_lgb.json'))
data = data.rename(columns=ftr_names)
print(data.head())
feat_type_dict = dict()
for i in data.columns:
if i.startswith("C"):
feat_type_dict[i] = FeatureType.Categorical
elif i.startswith("I"):
feat_type_dict[i] = FeatureType.Continuous
elif i.startswith("M"):
feat_type_dict[i] = FeatureType.MultiCategorical
elif i.startswith("O"):
feat_type_dict[i] = FeatureType.Ordered
elif i.startswith("S"):
feat_type_dict[i] = FeatureType.Sequential
data = fillna(data, feat_type_dict, cont_na=0.0, cate_na="NULL")
preprocessor = SimplePreprocessor(feat_type_dict=feat_type_dict)
df = preprocessor.fit_transform(data)
print(df.head())
# df.to_pickle('/home/baojunshan/douya-ctr/ltr/train_data_220607_lgb_processed.pkl')
# preprocessor.save('/home/baojunshan/douya-ctr/ltr/train_data_220607_lgb_processor.pkl')
# df = pd.read_pickle('/home/baojunshan/douya-ctr/ltr/train_data_220607_lgb_processed.pkl')
# preprocessor = SimplePreprocessor.load('/home/baojunshan/douya-ctr/ltr/train_data_220607_lgb_processor.pkl')
# 3. build model
# model = LambdaRanker(
# module=DCN,
# features=preprocessor.features,
# cross_layer_num=5,
# dense_emb_dim=4,
# dense_hidden_dims=[128, 128, 32],
# regularizer=1e-5
# )
model = LambdaRanker(
module=DeepFM,
features=preprocessor.features,
dense_emb_dim=8,
dense_hidden_dims=[128, 64, 32],
regularizer=1e-3
)
model.summary()
model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=[tf.keras.metrics.BinaryAccuracy(), tf.keras.metrics.AUC()]
)
# 4. train model
feature_columns = list(preprocessor.features.keys())
train, test = train_test_split(df, test_size=0.1, shuffle=False, sort_col=["ts"], group_col=['uid', 'ts'],
ascending=True)
train, valid = train_test_split(train, test_size=0.1, shuffle=False, sort_col=["ts"], group_col=['uid', 'ts'],
ascending=True)
x_train, y_train = train[[c for c in train.columns if c in feature_columns]], train["label"]
g_train = train.groupby(['uid', 'ts'], as_index=False, sort=False).count()['label'].values
x_valid, y_valid = valid[[c for c in valid.columns if c in feature_columns]], valid["label"]
g_valid = valid.groupby(['uid', 'ts'], as_index=False, sort=False).count()['label'].values
x_test, y_test = test[[c for c in test.columns if c in feature_columns]], test["label"]
g_test = test.groupby(['uid', 'ts'], as_index=False, sort=False).count()['label'].values
train_input = preprocessor.generator(x_train)
valid_input = preprocessor.generator(x_valid)
test_input = preprocessor.generator(x_test)
history_callback = HistoryExt()
model.fit(
train_input,
y_train,
g_train,
batch_size=4096,
epochs=10,
verbose=1,
eval_x=valid_input,
eval_y=y_valid,
eval_group=g_valid,
callbacks=[history_callback]
)
# 5. save model as saved-model-format for service
# save_model_as_savedmodel(model, "../service/serving_model/1/")
# 5. evaluate or predict
y_pred = model.predict(test_input)
evaluate_report(y_true=y_test, y_score=y_pred)
rank_evaluate(y_pred=y_pred, y_true=y_test, group=g_test, eval_at=[1, 3, 5, 10])
plot = Plot(title="LambdaDCN eval metrics")
for t in ["loss", "auc"]:
plot.add_plot(
label2dpair={
f"train_{t}": history_callback.get(value=t, mode="epoch"),
f"valid_{t}": history_callback.get(value=f"val_{t}", mode="epoch")
}, sub_xlabel="Batch", sub_ylabel=t.capitalize(), sub_title=f"{t}"
)
plot.plot(save_path="./lambda_dcn_220609.jpg")
if __name__ == "__main__":
main()