mnist_eager_test.py 2.3 KB
Newer Older
Asim Shankar's avatar
Asim Shankar committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Copyright 2018 The TensorFlow Authors. 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.
# ==============================================================================

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

Karmel Allison's avatar
Karmel Allison committed
20
21
import tensorflow as tf  # pylint: disable=g-bad-import-order
import tensorflow.contrib.eager as tfe  # pylint: disable=g-bad-import-order
Asim Shankar's avatar
Asim Shankar committed
22

23
24
from official.mnist import mnist
from official.mnist import mnist_eager
Asim Shankar's avatar
Asim Shankar committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48


def device():
  return "/device:GPU:0" if tfe.num_gpus() else "/device:CPU:0"


def data_format():
  return "channels_first" if tfe.num_gpus() else "channels_last"


def random_dataset():
  batch_size = 64
  images = tf.random_normal([batch_size, 784])
  labels = tf.random_uniform([batch_size], minval=0, maxval=10, dtype=tf.int32)
  return tf.data.Dataset.from_tensors((images, labels))


def train(defun=False):
  model = mnist.Model(data_format())
  if defun:
    model.call = tfe.defun(model.call)
  optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
  dataset = random_dataset()
  with tf.device(device()):
49
50
    mnist_eager.train(model, optimizer, dataset,
                      step_counter=tf.train.get_or_create_global_step())
Asim Shankar's avatar
Asim Shankar committed
51
52
53
54
55
56
57
58
59
60
61
62


def evaluate(defun=False):
  model = mnist.Model(data_format())
  dataset = random_dataset()
  if defun:
    model.call = tfe.defun(model.call)
  with tf.device(device()):
    mnist_eager.test(model, dataset)


class MNISTTest(tf.test.TestCase):
Karmel Allison's avatar
Karmel Allison committed
63
  """Run tests for MNIST eager loop."""
Asim Shankar's avatar
Asim Shankar committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

  def test_train(self):
    train(defun=False)

  def test_evaluate(self):
    evaluate(defun=False)

  def test_train_with_defun(self):
    train(defun=True)

  def test_evaluate_with_defun(self):
    evaluate(defun=True)


if __name__ == "__main__":
  tfe.enable_eager_execution()
  tf.test.main()