-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
218 lines (189 loc) · 7.91 KB
/
model.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
import os
import pprint
import tensorflow as tf
import tensorflow_transform as tft
class CensusModel(object):
def __init__(self,
working_dir='model',
num_epochs=3,
train_batch_size=32,
eval_batch_size=32,
num_eval_examples=1000):
self.num_epochs = num_epochs
self.model_dir = os.path.join(working_dir, 'exported')
train_data_pattern = os.path.join(working_dir, 'train', 'part-*')
eval_data_pattern = os.path.join(working_dir, 'test', 'part-*')
transform_dir = os.path.join(working_dir, 'transform_output')
self.tft_transform_output = tft.TFTransformOutput(transform_dir)
self.train_dataset = self.get_dataset_batch(
file_pattern=train_data_pattern,
tft_transform_output=self.tft_transform_output,
batch_size=train_batch_size
)
self.eval_steps = num_eval_examples // eval_batch_size
self.eval_dataset = self.get_dataset_batch(
file_pattern=eval_data_pattern,
tft_transform_output=self.tft_transform_output,
batch_size=eval_batch_size,
limit=num_eval_examples
)
def get_dataset_batch(
self,
file_pattern: str,
tft_transform_output: tft.TFTransformOutput,
batch_size: int,
limit: int = None,
) -> tf.data.Dataset:
features = tft_transform_output.transformed_feature_spec()
dataset = tf.data.Dataset.list_files(file_pattern)
dataset = dataset.interleave(
tf.data.TFRecordDataset, num_parallel_calls=tf.data.experimental.AUTOTUNE
)
if limit != None:
dataset = dataset.take(limit)
dataset = dataset.batch(batch_size, drop_remainder=True)
dataset = dataset.apply(
tf.data.experimental.parse_example_dataset(
features, num_parallel_calls=tf.data.experimental.AUTOTUNE
)
)
dataset = dataset.map(
map_func=lambda x: (x, x.pop(self.get_label_key())),
num_parallel_calls=tf.data.experimental.AUTOTUNE,
deterministic=False,
)
dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
return dataset
def attach_prediction_head(self, model):
probabilities = tf.keras.layers.Activation("sigmoid", name="predictions")(
model.output
)
return tf.keras.Model(inputs=model.inputs, outputs=probabilities)
def export_model(self, model):
full_model = self.attach_prediction_head(model)
full_model.save(
filepath=self.model_dir,
overwrite=True,
signatures=self.get_serve_tf_examples_fn(model),
)
def get_serve_tf_examples_fn(self, model):
# Returns a function that parses a serialized tf.Example and applies TFT.
model.tft_layer = self.tft_transform_output.transform_features_layer()
@tf.function
def inference_model(serialized_tf_examples):
# Returns the output to be used in the serving signature.
raw_feature_spec = self.tft_transform_output.raw_feature_spec()
raw_feature_spec.pop(self.get_label_key())
parsed_features = tf.io.parse_example(
serialized_tf_examples, raw_feature_spec
)
transformed_features = model.tft_layer(parsed_features)
return model(transformed_features)
@tf.function
def serving_default_signature(serialized_examples):
logits = inference_model(serialized_examples)
two_class_logits = tf.concat(
(tf.zeros_like(logits), logits), axis=-1, name="two_class_logits"
)
return {
"scores": tf.keras.layers.Softmax(name="probabilities")(
two_class_logits
),
}
@tf.function
def predict_signature(serialized_examples):
logits = inference_model(serialized_examples)
two_class_logits = tf.concat(
(tf.zeros_like(logits), logits), axis=-1, name="two_class_logits"
)
return {
"logits": logits,
"logistic": tf.keras.layers.Activation("sigmoid")(logits),
"probabilities": tf.keras.layers.Softmax(name="probabilities")(
two_class_logits
),
}
return {
"serving_default": serving_default_signature.get_concrete_function(
tf.TensorSpec(shape=[None], dtype=tf.string, name="inputs")
),
"predict": predict_signature.get_concrete_function(
tf.TensorSpec(shape=[None], dtype=tf.string, name="examples")
),
}
def get_label_key(self):
return "label"
def _get_encoded_features_columns(self):
numeric_features = [
tf.feature_column.numeric_column(key, shape=())
for key in ['age', 'fnlwgt', 'education-num',
'capital-gain', 'capital-loss', 'hours-per-week', ]
]
categorical_features = [
tf.feature_column.indicator_column(
tf.feature_column.categorical_column_with_vocabulary_file(
key, self.tft_transform_output.vocabulary_file_by_name(key)
)
)
for key in ['workclass', 'education', 'marital-status',
'occupation', 'relationship', 'race', 'sex',
'native-country', ]
]
transformed_features = [
tf.feature_column.numeric_column('education_tfidf_score', shape=()),
tf.feature_column.numeric_column('marital-status_tfidf_score', shape=()),
]
return numeric_features + \
categorical_features + \
transformed_features
def get_model(self) -> tf.keras.models.Model:
feature_spec = self.tft_transform_output.transformed_feature_spec().copy()
feature_spec.pop(self.get_label_key())
feature_inputs = {
k: tf.keras.Input(name=k, shape=v.shape, dtype=v.dtype)
for k, v in feature_spec.items()
}
encoded_inputs = self._get_encoded_features_columns()
feature_layer = tf.keras.layers.DenseFeatures(
feature_columns=encoded_inputs
)(feature_inputs)
output = tf.keras.layers.Dense(100, activation='relu')(feature_layer)
output = tf.keras.layers.Dense(50, activation='relu')(output)
output = tf.keras.layers.Dense(20, activation='relu')(output)
output = tf.keras.layers.Dense(1, name="logits")(output)
model = tf.keras.Model(inputs=feature_inputs, outputs=output)
return model
def get_training_strategy(self):
gpus = tf.config.list_physical_devices("GPU")
if len(gpus) == 1:
strategy = tf.distribute.OneDeviceStrategy("/gpu:0")
elif len(gpus) > 1:
strategy = tf.distribute.MirroredStrategy()
else:
strategy = tf.distribute.OneDeviceStrategy("/cpu:0")
return strategy
def get_compiled_model(self):
strategy = self.get_training_strategy()
with strategy.scope():
model = self.get_model()
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'],
)
return model
def train_and_evaluate(self):
model = self.get_compiled_model()
model.summary()
for x, y in self.train_dataset.take(1):
print("TRANSORMED FEATURES:")
pprint.pprint(x)
history = model.fit(
self.train_dataset,
epochs=self.num_epochs,
validation_data=self.eval_dataset,
validation_steps=self.eval_steps,
verbose=1,
)
self.export_model(model)
return history