-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathmpg_example.py
More file actions
308 lines (241 loc) · 11.3 KB
/
mpg_example.py
File metadata and controls
308 lines (241 loc) · 11.3 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Copyright 2018 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Example using auto-mpg data from UCI repository."""
# pylint: disable=g-bad-import-order
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import os
import tempfile
import tensorflow as tf
import tensorflow_transform as tft
from apache_beam.io import textio
from tensorflow.contrib.learn.python.learn.utils import input_fn_utils
from tensorflow_transform.beam import impl as beam_impl
from tensorflow_transform.beam.tft_beam_io import transform_fn_io
from tensorflow_transform.coders import csv_coder
from tensorflow_transform.saved import saved_transform_io
from tensorflow_transform.tf_metadata import dataset_metadata
from tensorflow_transform.tf_metadata import dataset_schema
import apache_beam as beam
# to download and prepare the data:
# curl https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data|grep -v "?"|sed -E -e 's/[[:blank:]]{2,}/,/g'|sed -E -e $'s/\t/,/g' | head -n340 > auto-mpg.csv
# curl https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data|grep -v "?"|sed -E -e 's/[[:blank:]]{2,}/,/g'|sed -E -e $'s/\t/,/g' | tail -n50 > auto-mpg-test.csv
ordered_columns = [
'mpg', 'cylinders', 'displacement', 'horsepower', 'weight', 'acceleration',
'year', 'origin', 'name'
]
CATEGORICAL_FEATURE_KEYS = [
'cylinders', 'year', 'name', 'origin'
]
NUMERIC_FEATURE_KEYS = [
'displacement', 'horsepower', 'weight', 'acceleration'
]
LABEL_KEY = 'mpg'
def _create_raw_metadata():
"""Create a DatasetMetadata for the raw data."""
column_schemas = {
key: dataset_schema.ColumnSchema(
tf.string, [], dataset_schema.FixedColumnRepresentation())
for key in CATEGORICAL_FEATURE_KEYS
}
column_schemas.update({
key: dataset_schema.ColumnSchema(
tf.float32, [], dataset_schema.FixedColumnRepresentation())
for key in NUMERIC_FEATURE_KEYS
})
column_schemas[LABEL_KEY] = dataset_schema.ColumnSchema(
tf.float32, [], dataset_schema.FixedColumnRepresentation())
raw_data_metadata = dataset_metadata.DatasetMetadata(dataset_schema.Schema(
column_schemas))
return raw_data_metadata
RAW_DATA_METADATA = _create_raw_metadata()
# Constants used for training. Note that the number of instances will be
# computed by tf.Transform in future versions, in which case it can be read from
# the metadata. Similarly BUCKET_SIZES will not be needed as this information
# will be stored in the metadata for each of the columns. The bucket size
# includes all listed categories in the dataset description as well as one extra
# for "?" which represents unknown.
BATCH_SIZE = 5
TRAIN_NUM_EPOCHS = 20
NUM_TRAIN_INSTANCES = 340
NUM_TEST_INSTANCES = 50
BUCKET_SIZES = [5, 12, 1024, 3]
EXPORTED_MODEL_DIR = 'exported_model_dir'
def create_transform_fn(train_data_file, working_dir):
"""Create a transform function that can be run on-the-fly while training
Read in the data using the CSV reader, and transform it using a
preprocessing pipeline that scales numeric data and converts categorical data
from strings to int64 values indices, by creating a vocabulary for each
category.
Args:
train_data_file: File containing training data
working_dir: Directory to write transformed data and metadata to
"""
def preprocessing_fn(inputs):
"""Preprocess input columns into transformed columns."""
outputs = {}
# Scale numeric columns to have range [0, 1].
for key in NUMERIC_FEATURE_KEYS:
outputs[key] = tft.scale_to_0_1(inputs[key])
# For all categorical columns except the label column, we use
# tft.string_to_int which computes the set of unique values and uses this
# to convert the strings to indices.
for key in CATEGORICAL_FEATURE_KEYS:
outputs[key] = tft.string_to_int(inputs[key])
# For the label column we provide the mapping from string to index.
outputs[LABEL_KEY] = inputs[LABEL_KEY]
return outputs
# The "with" block will create a pipeline, and run that pipeline at the exit
# of the block.
with beam.Pipeline() as pipeline:
with beam_impl.Context(temp_dir=tempfile.mkdtemp()):
# Create a coder to read the mpg data with the schema. To do this we
# need to list all columns in order since the schema doesn't specify the
# order of columns in the csv.
converter = csv_coder.CsvCoder(ordered_columns, RAW_DATA_METADATA.schema)
# Read in raw data and convert using CSV converter. Note that we apply
# some Beam transformations here, which will not be encoded in the TF
# graph since we don't do the from within tf.Transform's methods
# (AnalyzeDataset, TransformDataset etc.). These transformations are just
# to get data into a format that the CSV converter can read, in particular
# removing empty lines and removing spaces after commas.
raw_data = (
pipeline
| 'ReadTrainData' >> textio.ReadFromText(train_data_file)
| 'FilterTrainData' >> beam.Filter(lambda line: line)
| 'FixCommasTrainData' >> beam.Map(
lambda line: line.replace(', ', ','))
| 'DecodeTrainData' >> beam.Map(converter.decode))
# Combine data and schema into a dataset tuple. Note that we already used
# the schema to read the CSV data, but we also need it to interpret
# raw_data.
raw_dataset = (raw_data, RAW_DATA_METADATA)
transformed_dataset, transform_fn = (
raw_dataset | beam_impl.AnalyzeAndTransformDataset(preprocessing_fn))
transformed_data, transformed_metadata = transformed_dataset
# Will write a SavedModel and metadata to two subdirectories of
# working_dir, given by transform_fn_io.TRANSFORM_FN_DIR and
# transform_fn_io.TRANSFORMED_METADATA_DIR respectively.
_ = (
transform_fn
| 'WriteTransformFn' >>
transform_fn_io.WriteTransformFn(working_dir))
def file_decode_csv(line):
columns_default_values = [
[0.0] if key in NUMERIC_FEATURE_KEYS or key == LABEL_KEY else [''] for key
in ordered_columns]
parsed_line = tf.decode_csv(line, columns_default_values)
features = parsed_line
d = dict(zip(ordered_columns, features))
label = d[LABEL_KEY]
del d[LABEL_KEY]
return d, label
def _make_training_input_fn(working_dir, csv_file, batch_size):
dataset = (tf.data.TextLineDataset(csv_file, buffer_size=8 * 1048576))
dataset = dataset.shuffle(NUM_TRAIN_INSTANCES)
dataset = dataset.apply(
tf.contrib.data.map_and_batch(file_decode_csv, batch_size,
num_parallel_batches=4))
dataset = dataset.prefetch(4)
raw_features, raw_label = dataset.make_one_shot_iterator().get_next()
_, transformed_features = saved_transform_io.partially_apply_saved_transform(
os.path.join(working_dir, transform_fn_io.TRANSFORM_FN_DIR), raw_features)
return transformed_features, raw_label
def _make_serving_input_fn(working_dir):
"""Creates an input function reading from raw data.
Args:
working_dir: Directory to read transformed metadata from.
Returns:
The serving input function.
"""
raw_feature_spec = RAW_DATA_METADATA.schema.as_feature_spec()
# Remove label since it is not available during serving.
raw_feature_spec.pop(LABEL_KEY)
def serving_input_fn():
"""Input function for serving."""
# Get raw features by generating the basic serving input_fn and calling it.
# Here we generate an input_fn that expects a parsed Example proto to be fed
# to the model at serving time. See also
# input_fn_utils.build_default_serving_input_fn.
raw_input_fn = input_fn_utils.build_parsing_serving_input_fn(
raw_feature_spec)
raw_features, _, default_inputs = raw_input_fn()
# Apply the transform function that was used to generate the materialized
# data.
_, transformed_features = (
saved_transform_io.partially_apply_saved_transform(
os.path.join(working_dir, transform_fn_io.TRANSFORM_FN_DIR),
raw_features))
return tf.estimator.export.ServingInputReceiver(transformed_features,
default_inputs)
return serving_input_fn
def train_and_evaluate(working_dir, num_train_instances=NUM_TRAIN_INSTANCES,
num_test_instances=NUM_TEST_INSTANCES):
"""Train the model on training data and evaluate on eval data.
Args:
working_dir: Directory to read transformed data and metadata from and to
write exported model to.
num_train_instances: Number of instances in train set
num_test_instances: Number of instances in test set
Returns:
"""
one_hot_columns = [
tf.feature_column.indicator_column(
tf.feature_column.categorical_column_with_identity(key=key,
num_buckets=num_buckets))
for key, num_buckets in zip(CATEGORICAL_FEATURE_KEYS, BUCKET_SIZES)]
real_valued_columns = [tf.feature_column.numeric_column(key, shape=())
for key in NUMERIC_FEATURE_KEYS]
estimator = tf.estimator.DNNRegressor(
feature_columns=real_valued_columns + one_hot_columns,
model_dir=os.path.join(working_dir, "logs_directory"),
optimizer=tf.train.AdamOptimizer(),
hidden_units=[10, 5])
train_spec = tf.estimator.TrainSpec(
input_fn=lambda: _make_training_input_fn(working_dir, "auto-mpg.csv",
BATCH_SIZE),
max_steps=TRAIN_NUM_EPOCHS * num_train_instances / BATCH_SIZE)
eval_spec = tf.estimator.EvalSpec(
input_fn=lambda: _make_training_input_fn(working_dir, "auto-mpg-test.csv",
BATCH_SIZE),
throttle_secs=10, steps=num_test_instances / BATCH_SIZE)
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
# Export the model.
serving_input_fn = _make_serving_input_fn(working_dir)
exported_model_dir = os.path.join(working_dir, EXPORTED_MODEL_DIR)
estimator.export_savedmodel(exported_model_dir, serving_input_fn)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'input_data_dir',
help='path to directory containing input data')
parser.add_argument(
'--working_dir',
help='optional, path to directory to hold transformed data')
args = parser.parse_args()
if args.working_dir:
working_dir = args.working_dir
else:
working_dir = tempfile.mkdtemp(dir=args.input_data_dir)
train_data_file = os.path.join(args.input_data_dir, 'auto-mpg.csv')
# Will write a SavedModel and metadata to two subdirectories of
# working_dir, given by transform_fn_io.TRANSFORM_FN_DIR and
# transform_fn_io.TRANSFORMED_METADATA_DIR respectively.
create_transform_fn(train_data_file, working_dir)
# will transform features on the fly using the transform_fn created above
train_and_evaluate(working_dir)
if __name__ == '__main__':
main()