• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Demo of the tfdbg curses UI: A TF v2 network computing Fibonacci sequence."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import argparse
21import sys
22
23import absl
24import numpy as np
25from six.moves import xrange  # pylint: disable=redefined-builtin
26import tensorflow.compat.v2 as tf
27
28FLAGS = None
29
30tf.compat.v1.enable_v2_behavior()
31
32
33def main(_):
34  # Wrap the TensorFlow Session object for debugging.
35  # TODO(anthonyjliu): Enable debugger from flags
36  if FLAGS.debug and FLAGS.tensorboard_debug_address:
37    raise ValueError(
38        "The --debug and --tensorboard_debug_address flags are mutually "
39        "exclusive.")
40  if FLAGS.debug:
41    raise NotImplementedError(
42        "tfdbg v2 support for debug_fibonacci is not implemented yet")
43  elif FLAGS.tensorboard_debug_address:
44    raise NotImplementedError(
45        "Tensorboard Debugger Plugin support for debug_fibonacci_v2 is not "
46        "implemented yet"
47    )
48
49  # Construct the TensorFlow network.
50  n0 = tf.constant(np.ones([FLAGS.tensor_size] * 2), dtype=tf.int32)
51  n1 = tf.constant(np.ones([FLAGS.tensor_size] * 2), dtype=tf.int32)
52
53  for _ in xrange(2, FLAGS.length):
54    n0, n1 = n1, tf.add(n0, n1)
55
56  print("Fibonacci number at position %d:\n%s" % (FLAGS.length, n1.numpy()))
57
58
59if __name__ == "__main__":
60  parser = argparse.ArgumentParser()
61  parser.register("type", "bool", lambda v: v.lower() == "true")
62  parser.add_argument(
63      "--tensor_size",
64      type=int,
65      default=1,
66      help="""\
67      Size of tensor. E.g., if the value is 30, the tensors will have shape
68      [30, 30].\
69      """)
70  parser.add_argument(
71      "--length",
72      type=int,
73      default=20,
74      help="Length of the fibonacci sequence to compute.")
75  parser.add_argument(
76      "--debug",
77      dest="debug",
78      action="store_true",
79      help="Use TensorFlow Debugger (tfdbg). Mutually exclusive with the "
80      "--tensorboard_debug_address flag.")
81  parser.add_argument(
82      "--tensorboard_debug_address",
83      type=str,
84      default=None,
85      help="Connect to the TensorBoard Debugger Plugin backend specified by "
86      "the gRPC address (e.g., localhost:1234). Mutually exclusive with the "
87      "--debug flag.")
88
89  FLAGS, unparsed = parser.parse_known_args()
90
91  absl.app.run(main=main, argv=[sys.argv[0]] + unparsed)
92