18
18
import os
19
19
import tensorflow as tf
20
20
21
- tf .logging .set_verbosity (tf .logging .DEBUG )
21
+ tf .compat . v1 . logging .set_verbosity (tf . compat . v1 .logging .DEBUG )
22
22
23
23
24
24
def cnn_model_fn (features , labels , mode ):
@@ -33,30 +33,30 @@ def cnn_model_fn(features, labels, mode):
33
33
# Padding is added to preserve width and height.
34
34
# Input Tensor Shape: [batch_size, 28, 28, 1]
35
35
# Output Tensor Shape: [batch_size, 28, 28, 32]
36
- conv1 = tf .layers .conv2d (
36
+ conv1 = tf .compat . v1 . layers .conv2d (
37
37
inputs = input_layer , filters = 32 , kernel_size = [5 , 5 ], padding = "same" , activation = tf .nn .relu
38
38
)
39
39
40
40
# Pooling Layer #1
41
41
# First max pooling layer with a 2x2 filter and stride of 2
42
42
# Input Tensor Shape: [batch_size, 28, 28, 32]
43
43
# Output Tensor Shape: [batch_size, 14, 14, 32]
44
- pool1 = tf .layers .max_pooling2d (inputs = conv1 , pool_size = [2 , 2 ], strides = 2 )
44
+ pool1 = tf .compat . v1 . layers .max_pooling2d (inputs = conv1 , pool_size = [2 , 2 ], strides = 2 )
45
45
46
46
# Convolutional Layer #2
47
47
# Computes 64 features using a 5x5 filter.
48
48
# Padding is added to preserve width and height.
49
49
# Input Tensor Shape: [batch_size, 14, 14, 32]
50
50
# Output Tensor Shape: [batch_size, 14, 14, 64]
51
- conv2 = tf .layers .conv2d (
51
+ conv2 = tf .compat . v1 . layers .conv2d (
52
52
inputs = pool1 , filters = 64 , kernel_size = [5 , 5 ], padding = "same" , activation = tf .nn .relu
53
53
)
54
54
55
55
# Pooling Layer #2
56
56
# Second max pooling layer with a 2x2 filter and stride of 2
57
57
# Input Tensor Shape: [batch_size, 14, 14, 64]
58
58
# Output Tensor Shape: [batch_size, 7, 7, 64]
59
- pool2 = tf .layers .max_pooling2d (inputs = conv2 , pool_size = [2 , 2 ], strides = 2 )
59
+ pool2 = tf .compat . v1 . layers .max_pooling2d (inputs = conv2 , pool_size = [2 , 2 ], strides = 2 )
60
60
61
61
# Flatten tensor into a batch of vectors
62
62
# Input Tensor Shape: [batch_size, 7, 7, 64]
@@ -67,17 +67,17 @@ def cnn_model_fn(features, labels, mode):
67
67
# Densely connected layer with 1024 neurons
68
68
# Input Tensor Shape: [batch_size, 7 * 7 * 64]
69
69
# Output Tensor Shape: [batch_size, 1024]
70
- dense = tf .layers .dense (inputs = pool2_flat , units = 1024 , activation = tf .nn .relu )
70
+ dense = tf .compat . v1 . layers .dense (inputs = pool2_flat , units = 1024 , activation = tf .nn .relu )
71
71
72
72
# Add dropout operation; 0.6 probability that element will be kept
73
- dropout = tf .layers .dropout (
73
+ dropout = tf .compat . v1 . layers .dropout (
74
74
inputs = dense , rate = 0.4 , training = mode == tf .estimator .ModeKeys .TRAIN
75
75
)
76
76
77
77
# Logits layer
78
78
# Input Tensor Shape: [batch_size, 1024]
79
79
# Output Tensor Shape: [batch_size, 10]
80
- logits = tf .layers .dense (inputs = dropout , units = 10 )
80
+ logits = tf .compat . v1 . layers .dense (inputs = dropout , units = 10 )
81
81
82
82
predictions = {
83
83
# Generate predictions (for PREDICT and EVAL mode)
@@ -90,17 +90,17 @@ def cnn_model_fn(features, labels, mode):
90
90
return tf .estimator .EstimatorSpec (mode = mode , predictions = predictions )
91
91
92
92
# Calculate Loss (for both TRAIN and EVAL modes)
93
- loss = tf .losses .sparse_softmax_cross_entropy (labels = labels , logits = logits )
93
+ loss = tf .compat . v1 . losses .sparse_softmax_cross_entropy (labels = labels , logits = logits )
94
94
95
95
# Configure the Training Op (for TRAIN mode)
96
96
if mode == tf .estimator .ModeKeys .TRAIN :
97
- optimizer = tf .train .GradientDescentOptimizer (learning_rate = 0.001 )
98
- train_op = optimizer .minimize (loss = loss , global_step = tf .train .get_global_step ())
97
+ optimizer = tf .compat . v1 . train .GradientDescentOptimizer (learning_rate = 0.001 )
98
+ train_op = optimizer .minimize (loss = loss , global_step = tf .compat . v1 . train .get_global_step ())
99
99
return tf .estimator .EstimatorSpec (mode = mode , loss = loss , train_op = train_op )
100
100
101
101
# Add evaluation metrics (for EVAL mode)
102
102
eval_metric_ops = {
103
- "accuracy" : tf .metrics .accuracy (labels = labels , predictions = predictions ["classes" ])
103
+ "accuracy" : tf .compat . v1 . metrics .accuracy (labels = labels , predictions = predictions ["classes" ])
104
104
}
105
105
return tf .estimator .EstimatorSpec (mode = mode , loss = loss , eval_metric_ops = eval_metric_ops )
106
106
@@ -134,7 +134,7 @@ def _parse_args():
134
134
135
135
136
136
def serving_input_fn ():
137
- inputs = {"x" : tf .placeholder (tf .float32 , [None , 784 ])}
137
+ inputs = {"x" : tf .compat . v1 . placeholder (tf .float32 , [None , 784 ])}
138
138
return tf .estimator .export .ServingInputReceiver (inputs , inputs )
139
139
140
140
@@ -155,15 +155,15 @@ def serving_input_fn():
155
155
# Set up logging for predictions
156
156
# Log the values in the "Softmax" tensor with label "probabilities"
157
157
tensors_to_log = {"probabilities" : "softmax_tensor" }
158
- logging_hook = tf .train .LoggingTensorHook (tensors = tensors_to_log , every_n_iter = 50 )
158
+ logging_hook = tf .estimator .LoggingTensorHook (tensors = tensors_to_log , every_n_iter = 50 )
159
159
160
160
# Train the model
161
- train_input_fn = tf .estimator .inputs .numpy_input_fn (
161
+ train_input_fn = tf .compat . v1 . estimator .inputs .numpy_input_fn (
162
162
x = {"x" : train_data }, y = train_labels , batch_size = 50 , num_epochs = None , shuffle = True
163
163
)
164
164
165
165
# Evaluate the model and print results
166
- eval_input_fn = tf .estimator .inputs .numpy_input_fn (
166
+ eval_input_fn = tf .compat . v1 . estimator .inputs .numpy_input_fn (
167
167
x = {"x" : eval_data }, y = eval_labels , num_epochs = 1 , shuffle = False
168
168
)
169
169
@@ -172,4 +172,4 @@ def serving_input_fn():
172
172
tf .estimator .train_and_evaluate (mnist_classifier , train_spec , eval_spec )
173
173
174
174
if args .current_host == args .hosts [0 ]:
175
- mnist_classifier .export_savedmodel ("/opt/ml/model" , serving_input_fn )
175
+ mnist_classifier .export_saved_model ("/opt/ml/model" , serving_input_fn )
0 commit comments