-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
181 lines (157 loc) · 5.05 KB
/
train.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
import os
import json
import random
from typing import List
import torch
import torch.optim as optim
import pandas as pd
from deepctr_torch.models import *
from deepctr_torch.callbacks import EarlyStopping, ModelCheckpoint
from preprocess import preprocess, generate_datasets
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# dataset and features
sparse_features = [
'msno',
'song_id',
'source_system_tab',
'source_screen_name',
'source_type',
'composer',
'lyricist',
'language',
'is_featured',
'city',
'registered_via',
'gender',
]
dense_features = [
'song_length',
'num_artist',
'num_composer',
'num_lyricist',
'bd',
'registration_init_time',
'expiration_date',
'duration',
]
varlen_features = [
'genre_ids',
'artist_name',
]
def random_features(features: List[str]):
sample_size = random.randint(1, len(features))
sampled_features = random.sample(features, sample_size)
return sampled_features
def train_pipeline(train_df: pd.DataFrame,
test_df: pd.DataFrame,
songs_df: pd.DataFrame,
members_df: pd.DataFrame,
deepctr_model,
num_models: int = 5,
**kwargs):
tr_song_msno_df, val_song_msno_df, ts_song_msno_df, item2idx = preprocess(
train_df, test_df, songs_df, members_df)
trained_models = {}
used_features = set()
ckpt_fp = f'../checkpoints/{deepctr_model.__name__}'
os.makedirs(ckpt_fp, exist_ok=True)
i = 0
while i < num_models:
sampled_sparse = random_features(sparse_features)
sampled_dense = random_features(dense_features)
sampled_varlen = random_features(varlen_features)
sampled_all = tuple(
sorted(sampled_sparse + sampled_dense + sampled_varlen))
if sampled_all in used_features:
continue
used_features.add(sampled_all)
(tr_model_input, val_model_input, ts_model_input,
linear_feature_columns,
dnn_feature_columns) = generate_datasets(tr_song_msno_df,
val_song_msno_df,
ts_song_msno_df,
sampled_sparse,
sampled_dense,
sampled_varlen,
item2idx,
embed_dim=64)
# train model
model = deepctr_model(linear_feature_columns,
dnn_feature_columns,
task='binary',
device=DEVICE,
**kwargs)
es = EarlyStopping(monitor='val_auc',
min_delta=0,
verbose=1,
patience=1,
mode='max')
mdckpt = ModelCheckpoint(filepath=f'{ckpt_fp}/model_{i}.ckpt',
monitor='val_auc',
verbose=1,
save_best_only=True,
mode='max')
model.compile(
# optimizer="adam",
optimizer=optim.RMSprop(model.parameters(), lr=1e-3),
loss="binary_crossentropy",
metrics=['auc'])
model.fit(tr_model_input,
tr_song_msno_df['target'].values,
batch_size=8192,
epochs=10,
verbose=2,
validation_data=(val_model_input,
val_song_msno_df['target'].values),
shuffle=True,
callbacks=[es, mdckpt])
if mdckpt.best >= 0.68:
trained_models[i] = mdckpt.best
i += 1
print()
print(trained_models)
with open(f'{ckpt_fp}/result.json', 'w', encoding='utf-8') as f:
json.dump(trained_models, f)
if __name__ == "__main__":
train_df = pd.read_csv('../data/train.csv')
test_df = pd.read_csv('../data/test.csv')
songs_df = pd.read_csv('../data/songs.csv')
members_df = pd.read_csv('../data/members.csv')
# DeepFM
# train_pipeline(
# train_df,
# test_df,
# songs_df,
# members_df,
# DeepFM,
# l2_reg_embedding=1e-4,
# l2_reg_dnn=1e-4,
# dnn_dropout=0.3,
# dnn_use_bn=True,
# )
# xDeepFM
train_pipeline(
train_df,
test_df,
songs_df,
members_df,
xDeepFM,
l2_reg_cin=1e-4,
l2_reg_linear=1e-4,
l2_reg_embedding=1e-4,
l2_reg_dnn=1e-4,
dnn_dropout=0.3,
dnn_use_bn=True,
)
# WDL
# train_pipeline(
# train_df,
# test_df,
# songs_df,
# members_df,
# WDL,
# l2_reg_embedding=1e-4,
# dnn_dropout=0.3,
# l2_reg_dnn=1e-4,
# dnn_use_bn=True,
# )