-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
208 lines (173 loc) · 8.1 KB
/
main.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
import random
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.initializers import VarianceScaling
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import Callback
from sklearn.metrics import roc_auc_score
from mmoe import MMoE
SEED = 1
np.random.seed(SEED)
random.seed(SEED)
tf.random.set_random_seed(SEED)
class ROCCallback(Callback):
def __init__(self, training_data, validation_data, test_data):
self.train_X = training_data[0]
self.train_Y = training_data[1]
self.validation_X = validation_data[0]
self.validation_Y = validation_data[1]
self.test_X = test_data[0]
self.test_Y = test_data[1]
def on_train_begin(self, logs={}):
return
def on_train_end(self, logs={}):
return
def on_epoch_begin(self, epoch, logs={}):
return
def on_epoch_end(self, epoch, logs={}):
train_prediction = self.model.predict(self.train_X)
validation_prediction = self.model.predict(self.validation_X)
test_prediction = self.model.predict(self.test_X)
file1 = open('Train-ROC', 'a+', encoding='UTF-8')
file2 = open('Validation-ROC', 'a+', encoding='UTF-8')
file3 = open('Test-ROC', 'a+', encoding='UTF-8')
# 遍历每个任务并在不同的数据集上输出它们的ROC-AUC
for index, output_name in enumerate(self.model.output_names):
train_roc_auc = roc_auc_score(self.train_Y[index], train_prediction[index])
validation_roc_auc = roc_auc_score(self.validation_Y[index], validation_prediction[index])
test_roc_auc = roc_auc_score(self.test_Y[index], test_prediction[index])
print(
'ROC-AUC-{}-Train: {} ROC-AUC-{}-Validation: {} ROC-AUC-{}-Test: {}\n'.format(
output_name, round(train_roc_auc, 4),
output_name, round(validation_roc_auc, 4),
output_name, round(test_roc_auc, 4)
)
)
file1.write(str(train_roc_auc) + '\n')
file2.write(str(validation_roc_auc) + '\n')
file3.write(str(test_roc_auc) + '\n')
return
def on_batch_begin(self, batch, logs={}):
return
def on_batch_end(self, batch, logs={}):
return
def data_preparation():
column_names = ['age', 'class_worker', 'det_ind_code', 'det_occ_code', 'education', 'wage_per_hour', 'hs_college',
'marital_stat', 'major_ind_code', 'major_occ_code', 'race', 'hisp_origin', 'sex', 'union_member',
'unemp_reason', 'full_or_part_emp', 'capital_gains', 'capital_losses', 'stock_dividends',
'tax_filer_stat', 'region_prev_res', 'state_prev_res', 'det_hh_fam_stat', 'det_hh_summ',
'instance_weight', 'mig_chg_msa', 'mig_chg_reg', 'mig_move_reg', 'mig_same', 'mig_prev_sunbelt',
'num_emp', 'fam_under_18', 'country_father', 'country_mother', 'country_self', 'citizenship',
'own_or_self', 'vet_question', 'vet_benefits', 'weeks_worked', 'year', 'income_50k']
# 用pandas库加载数据集
train_df = pd.read_csv(
'data/data.gz',
delimiter=',',
header=None,
index_col=None,
names=column_names
)
other_df = pd.read_csv(
'data/test.gz',
delimiter=',',
header=None,
index_col=None,
names=column_names
)
# 用收入和婚姻状况两个内容
label_columns = ['income_50k', 'marital_stat']
# 独热编码分类列
categorical_columns = ['class_worker', 'det_ind_code', 'det_occ_code', 'education', 'hs_college', 'major_ind_code',
'major_occ_code', 'race', 'hisp_origin', 'sex', 'union_member', 'unemp_reason',
'full_or_part_emp', 'tax_filer_stat', 'region_prev_res', 'state_prev_res', 'det_hh_fam_stat',
'det_hh_summ', 'mig_chg_msa', 'mig_chg_reg', 'mig_move_reg', 'mig_same', 'mig_prev_sunbelt',
'fam_under_18', 'country_father', 'country_mother', 'country_self', 'citizenship',
'vet_question']
train_raw_labels = train_df[label_columns]
other_raw_labels = other_df[label_columns]
transformed_train = pd.get_dummies(train_df.drop(label_columns, axis=1), columns=categorical_columns)
transformed_other = pd.get_dummies(other_df.drop(label_columns, axis=1), columns=categorical_columns)
# 在另一个集合中填充缺失的列
transformed_other['det_hh_fam_stat_ Grandchild <18 ever marr not in subfamily'] = 0
# 独热编码分类标签
train_income = to_categorical((train_raw_labels.income_50k == ' 50000+.').astype(int), num_classes=2)
train_marital = to_categorical((train_raw_labels.marital_stat == ' Never married').astype(int), num_classes=2)
other_income = to_categorical((other_raw_labels.income_50k == ' 50000+.').astype(int), num_classes=2)
other_marital = to_categorical((other_raw_labels.marital_stat == ' Never married').astype(int), num_classes=2)
dict_outputs = {
'income': train_income.shape[1],
'marital': train_marital.shape[1]
}
dict_train_labels = {
'income': train_income,
'marital': train_marital
}
dict_other_labels = {
'income': other_income,
'marital': other_marital
}
output_info = [(dict_outputs[key], key) for key in sorted(dict_outputs.keys())]
# 将其他数据集拆分为1:1验证,测试集
validation_indices = transformed_other.sample(frac=0.5, replace=False, random_state=SEED).index
test_indices = list(set(transformed_other.index) - set(validation_indices))
validation_data = transformed_other.iloc[validation_indices]
validation_label = [dict_other_labels[key][validation_indices] for key in sorted(dict_other_labels.keys())]
test_data = transformed_other.iloc[test_indices]
test_label = [dict_other_labels[key][test_indices] for key in sorted(dict_other_labels.keys())]
train_data = transformed_train
train_label = [dict_train_labels[key] for key in sorted(dict_train_labels.keys())]
return train_data, train_label, validation_data, validation_label, test_data, test_label, output_info
def main():
train_data, train_label, validation_data, validation_label, test_data, test_label, output_info = data_preparation()
num_features = train_data.shape[1]
print('Training data shape = {}\n'.format(train_data.shape))
print('Validation data shape = {}\n'.format(validation_data.shape))
print('Test data shape = {}\n'.format(test_data.shape))
input_layer = Input(shape=(num_features,))
mmoe_layers = MMoE(
units=4,
num_experts=8,
num_tasks=2
)(input_layer)
output_layers = []
# MMoE层创建tower层
for index, task_layer in enumerate(mmoe_layers):
tower_layer = Dense(
units=8,
activation='relu',
kernel_initializer=VarianceScaling())(task_layer)
output_layer = Dense(
units=output_info[index][0],
name=output_info[index][1],
activation='softmax',
kernel_initializer=VarianceScaling())(tower_layer)
output_layers.append(output_layer)
model = Model(inputs=[input_layer], outputs=output_layers)
adam_optimizer = Adam()
model.compile(
loss={'income': 'binary_crossentropy', 'marital': 'binary_crossentropy'},
optimizer=adam_optimizer,
metrics=['accuracy']
)
model.summary()
# 训练模型
model.fit(
x=train_data,
y=train_label,
validation_data=(validation_data, validation_label),
callbacks=[
ROCCallback(
training_data=(train_data, train_label),
validation_data=(validation_data, validation_label),
test_data=(test_data, test_label)
)
],
epochs=100
)
if __name__ == '__main__':
main()