Model to recognize digits in the MNIST dataset. Network structure is equivalent to: https://github.com/tensorflow/tensorflow/blob/r1.5/tensorflow/examples/tutorials/mnist/mnist_deep.py and https://github.com/tensorflow/models/blob/master/tutorials/image/mnist/convolutional.py But uses the
()
| 67 | |
| 68 | |
| 69 | def create_model(): |
| 70 | """Model to recognize digits in the MNIST dataset. |
| 71 | |
| 72 | Network structure is equivalent to: |
| 73 | https://github.com/tensorflow/tensorflow/blob/r1.5/tensorflow/examples/tutorials/mnist/mnist_deep.py |
| 74 | and |
| 75 | https://github.com/tensorflow/models/blob/master/tutorials/image/mnist/convolutional.py |
| 76 | But uses the tf.keras API. |
| 77 | Returns: |
| 78 | A tf.keras.Model. |
| 79 | """ |
| 80 | # Assumes data_format == 'channel_last'. |
| 81 | # See https://www.tensorflow.org/performance/performance_guide#data_formats |
| 82 | |
| 83 | input_shape = [28, 28, 1] |
| 84 | |
| 85 | l = tf.keras.layers |
| 86 | max_pool = l.MaxPooling2D((2, 2), (2, 2), padding='same') |
| 87 | # The model consists of a sequential chain of layers, so tf.keras.Sequential |
| 88 | # (a subclass of tf.keras.Model) makes for a compact description. |
| 89 | model = tf.keras.Sequential( |
| 90 | [ |
| 91 | l.Reshape( |
| 92 | target_shape=input_shape, |
| 93 | input_shape=(28 * 28,)), |
| 94 | l.Conv2D(2, 5, padding='same', activation=tf.nn.relu), |
| 95 | max_pool, |
| 96 | l.Conv2D(4, 5, padding='same', activation=tf.nn.relu), |
| 97 | max_pool, |
| 98 | l.Flatten(), |
| 99 | l.Dense(32, activation=tf.nn.relu), |
| 100 | l.Dropout(0.4), |
| 101 | l.Dense(10) |
| 102 | ]) |
| 103 | # TODO(brianklee): Remove when @kaftan makes this happen by default. |
| 104 | # TODO(brianklee): remove `autograph=True` when kwarg default is flipped. |
| 105 | model.call = tfe.function(model.call, autograph=True) |
| 106 | # Needs to have input_signature specified in order to be exported |
| 107 | # since model.predict() is never called before saved_model.export() |
| 108 | # TODO(brianklee): Update with input signature, depending on how the impl of |
| 109 | # saved_model.restore() pans out. |
| 110 | model.predict = tfe.function(model.predict, autograph=True) |
| 111 | # ,input_signature=(tensor_spec.TensorSpec(shape=[28, 28, None], dtype=tf.float32),) # pylint: disable=line-too-long |
| 112 | return model |
| 113 | |
| 114 | |
| 115 | def mnist_datasets(): |
no test coverage detected