This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
example.py
72 lines (56 loc) · 1.97 KB
/
example.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
import tensorflow as tf
import shutil
# setup feature columns
domain = tf.feature_column.categorical_column_with_hash_bucket(
"domain", 100000, dtype=tf.string)
hour = tf.feature_column.categorical_column_with_identity("hour", 24)
device_type = tf.feature_column.categorical_column_with_vocabulary_list(
"device_type", vocabulary_list=["desktop", "mobile", "tablet"],
default_value=0)
feature_columns = [domain, hour, device_type]
# actual model setup
ftrl = tf.train.FtrlOptimizer(
learning_rate=0.1,
learning_rate_power=-0.5,
l1_regularization_strength=0.001,
l2_regularization_strength=0.0
)
model_dir = "."
estimator = tf.estimator.LinearClassifier(
feature_columns=feature_columns,
optimizer=ftrl,
model_dir=model_dir
)
def input_fn(paths):
""" model input function """
names = ["domain", "hour", "device_type", "is_click"]
record_defaults = [[""], [0], ["desktop"], [0]]
def _parse_csv(rows_string_tensor):
columns = tf.decode_csv(rows_string_tensor, record_defaults)
features = dict(zip(names, columns[:-1]))
labels = columns[-1]
return features, labels
def _input_fn():
dataset = tf.data.TextLineDataset(paths)
dataset = dataset.map(_parse_csv)
dataset = dataset.batch(100)
iterator = dataset.make_one_shot_iterator()
features, labels = iterator.get_next()
return features, labels
return _input_fn
# Train the model.
paths = ["example.csv"]
estimator.train(input_fn=input_fn(paths), steps=None)
# Export our model
columns = [('hour', tf.int64),
('domain', tf.string),
('device_type', tf.string)]
feature_placeholders = {
name: tf.placeholder(dtype, [1], name=name + "_placeholder")
for name, dtype in columns
}
export_input_fn = tf.estimator.export.build_raw_serving_input_receiver_fn(
feature_placeholders)
path = estimator.export_savedmodel(model_dir, export_input_fn)
# rename export directory
shutil.move(path, "EXPORT")