-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisaster_tweets_trainer.py
161 lines (131 loc) · 4.98 KB
/
disaster_tweets_trainer.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
import os
import tensorflow as tf
import tensorflow_transform as tft
from tensorflow.keras import layers
from tfx.components.trainer.fn_args_utils import FnArgs
LABEL_KEY = 'label'
FEATURE_KEY = 'text'
def transformed_name(key):
"""Renaming transformed features"""
return key + "_xf"
def gzip_reader_fn(filenames):
"""Loads compressed data"""
return tf.data.TFRecordDataset(filenames, compression_type='GZIP')
def input_fn(file_pattern, tf_transform_output, num_epochs, batch_size=64) -> tf.data.Dataset:
"""Get post_tranform feature & create batches of data"""
# Get post_transform feature spec
transform_feature_spec = (
tf_transform_output.transformed_feature_spec().copy()
)
# Create batches of data
dataset = tf.data.experimental.make_batched_features_dataset(
file_pattern = file_pattern,
batch_size = batch_size,
features = transform_feature_spec,
reader = gzip_reader_fn,
num_epochs = num_epochs,
label_key = transformed_name(LABEL_KEY)
)
return dataset
# Vocabulary size and number of words in a sequence
VOCAB_SIZE = 10000
SEQUENCE_LENGTH = 100
embedding_dim = 16
vectorize_layer = layers.TextVectorization(
standardize = 'lower_and_strip_punctuation',
max_tokens = VOCAB_SIZE,
output_mode = 'int',
output_sequence_length = SEQUENCE_LENGTH
)
def model_builder(hp):
"""Build machine learning model"""
inputs = tf.keras.Input(shape=(1,), name=transformed_name(FEATURE_KEY), dtype=tf.string)
reshaped_narrative = tf.reshape(inputs, [-1])
x = vectorize_layer(reshaped_narrative)
x = layers.Embedding(VOCAB_SIZE, hp['embedding_dim'], name='embedding')(x)
x = layers.Bidirectional(layers.LSTM(hp['lstm_units']))(x)
for _ in range(hp['num_layers']):
x = layers.Dense(hp['dense_units'], activation='relu')(x)
x = layers.Dropout(hp['dropout_rate'])(x)
outputs = layers.Dense(1, activation='sigmoid')(x)
model = tf.keras.Model(inputs = inputs, outputs = outputs)
model.compile(
loss = tf.keras.losses.BinaryCrossentropy(from_logits=True),
optimizer = tf.keras.optimizers.Adam(hp['learning_rate']),
metrics = [tf.keras.metrics.BinaryAccuracy()]
)
model.summary()
return model
def _get_serve_tf_examples_fn(model, tf_transform_output):
model.tft_layer = tf_transform_output.transform_features_layer()
@tf.function
def serve_tf_examples_fn(serialized_tf_examples):
feature_spec = tf_transform_output.raw_feature_spec()
feature_spec.pop(LABEL_KEY)
parsed_features = tf.io.parse_example(serialized_tf_examples, feature_spec)
transformed_features = model.tft_layer(parsed_features)
# get predictions using the transformed features
return model(transformed_features)
return serve_tf_examples_fn
def run_fn(fn_args: FnArgs) -> None:
log_dir = os.path.join(os.path.dirname(fn_args.serving_model_dir), 'logs')
hp = fn_args.hyperparameters['values']
tensorboard_callback = tf.keras.callbacks.TensorBoard(
log_dir = log_dir, update_freq='batch'
)
early_stop_callback = tf.keras.callbacks.EarlyStopping(
monitor = 'val_binary_accuracy',
mode = 'max',
verbose = 1,
patience = 10
)
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
fn_args.serving_model_dir,
monitor = 'val_binary_accuracy',
mode = 'max',
verbose = 1,
save_best_only = True
)
callbacks = [
tensorboard_callback,
early_stop_callback,
model_checkpoint_callback
]
# Load the transform output
tf_transform_output = tft.TFTransformOutput(fn_args.transform_graph_path)
# Create batches of data
train_set = input_fn(fn_args.train_files, tf_transform_output, hp['tuner/epochs'])
val_set = input_fn(fn_args.eval_files, tf_transform_output, hp['tuner/epochs'])
vectorize_layer.adapt(
[j[0].numpy()[0] for j in [
i[0][transformed_name(FEATURE_KEY)]
for i in list(train_set)
]]
)
# Build the model
model = model_builder(hp)
# Train the model
model.fit(
x = train_set,
validation_data = val_set,
callbacks = callbacks,
steps_per_epoch = fn_args.train_steps,
validation_steps = fn_args.eval_steps,
epochs = hp['tuner/epochs']
)
signatures = {
'serving_default': _get_serve_tf_examples_fn(
model, tf_transform_output
).get_concrete_function(
tf.TensorSpec(
shape = [None],
dtype = tf.string,
name = 'examples'
)
)
}
model.save(
fn_args.serving_model_dir,
save_format = 'tf',
signatures = signatures
)