forked from rasbt/python-machine-learning-book-3rd-edition
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ch14_part3.py
203 lines (117 loc) · 4.48 KB
/
ch14_part3.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
# coding: utf-8
import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np
# *Python Machine Learning 3rd Edition* by [Sebastian Raschka](https://sebastianraschka.com) & [Vahid Mirjalili](http://vahidmirjalili.com), Packt Publishing Ltd. 2019
#
# Code Repository: https://github.com/rasbt/python-machine-learning-book-3rd-edition
#
# Code License: [MIT License](https://github.com/rasbt/python-machine-learning-book-3rd-edition/blob/master/LICENSE.txt)
# # Chapter 14: Going Deeper -- the Mechanics of TensorFlow (Part 3/3)
# Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s).
# ### Using Estimators for MNIST hand-written digit classification
BUFFER_SIZE = 10000
BATCH_SIZE = 64
NUM_EPOCHS = 20
steps_per_epoch = np.ceil(60000 / BATCH_SIZE)
def preprocess(item):
image = item['image']
label = item['label']
image = tf.image.convert_image_dtype(
image, tf.float32)
image = tf.reshape(image, (-1,))
return {'image-pixels':image}, label[..., tf.newaxis]
#Step 1: Defining the input functions (one for training and one for evaluation)
## Step 1: Define the input function for training
def train_input_fn():
datasets = tfds.load(name='mnist')
mnist_train = datasets['train']
dataset = mnist_train.map(preprocess)
dataset = dataset.shuffle(BUFFER_SIZE)
dataset = dataset.batch(BATCH_SIZE)
return dataset.repeat()
## define input-function for evaluation:
def eval_input_fn():
datasets = tfds.load(name='mnist')
mnist_test = datasets['test']
dataset = mnist_test.map(preprocess).batch(BATCH_SIZE)
return dataset
## Step 2: feature column
image_feature_column = tf.feature_column.numeric_column(
key='image-pixels', shape=(28*28))
## Step 3: instantiate the estimator
dnn_classifier = tf.estimator.DNNClassifier(
feature_columns=[image_feature_column],
hidden_units=[32, 16],
n_classes=10,
model_dir='models/mnist-dnn/')
## Step 4: train
dnn_classifier.train(
input_fn=train_input_fn,
steps=NUM_EPOCHS * steps_per_epoch)
eval_result = dnn_classifier.evaluate(
input_fn=eval_input_fn)
print(eval_result)
# ### Creating a custom Estimator from an existing Keras model
## Set random seeds for reproducibility
tf.random.set_seed(1)
np.random.seed(1)
## Create the data
x = np.random.uniform(low=-1, high=1, size=(200, 2))
y = np.ones(len(x))
y[x[:, 0] * x[:, 1]<0] = 0
x_train = x[:100, :]
y_train = y[:100]
x_valid = x[100:, :]
y_valid = y[100:]
## Step 1: Define the input functions
def train_input_fn(x_train, y_train, batch_size=8):
dataset = tf.data.Dataset.from_tensor_slices(
({'input-features':x_train}, y_train.reshape(-1, 1)))
# Shuffle, repeat, and batch the examples.
return dataset.shuffle(100).repeat().batch(batch_size)
def eval_input_fn(x_test, y_test=None, batch_size=8):
if y_test is None:
dataset = tf.data.Dataset.from_tensor_slices(
{'input-features':x_test})
else:
dataset = tf.data.Dataset.from_tensor_slices(
({'input-features':x_test}, y_test.reshape(-1, 1)))
# Shuffle, repeat, and batch the examples.
return dataset.batch(batch_size)
## Step 2: Define the feature columns
features = [
tf.feature_column.numeric_column(
key='input-features:', shape=(2,))
]
features
## Step 3: Create the estimator: convert from a Keras model
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(2,), name='input-features'),
tf.keras.layers.Dense(units=4, activation='relu'),
tf.keras.layers.Dense(units=4, activation='relu'),
tf.keras.layers.Dense(units=4, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()
model.compile(optimizer=tf.keras.optimizers.SGD(),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=[tf.keras.metrics.BinaryAccuracy()])
my_estimator = tf.keras.estimator.model_to_estimator(
keras_model=model,
model_dir='models/estimator-for-XOR/')
## Step 4: use the estimator: train/evaluate/predict
num_epochs = 200
batch_size = 2
steps_per_epoch = np.ceil(len(x_train) / batch_size)
my_estimator.train(
input_fn=lambda: train_input_fn(x_train, y_train, batch_size),
steps=num_epochs * steps_per_epoch)
my_estimator.evaluate(
input_fn=lambda: eval_input_fn(x_valid, y_valid, batch_size))
# ...
# # Summary
# ...
# ---
#
# Readers may ignore the next cell.