aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/autograph/README.md
blob: 674859bed4ec157d5d5b33b6fc015c930e54b392 (plain)
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
# AutoGraph

IMPORTANT: AutoGraph is alpha software, and under active development. Expect rough edges and bugs, but if you try it, we appreciate early feedback! We'd also love contributions ([please see our contributing guidelines](CONTRIBUTING.md) and our [style guide](STYLE_GUIDE.md)).

AutoGraph is a Python to TensorFlow compiler.

With AutoGraph, you can write [Eager style](https://www.tensorflow.org/programmers_guide/eager) code in a concise manner, and run it as a TensorFlow graph. AutoGraph uses source code transformation and partial evaluation to generate Python code that builds an equivalent TensorFlow subgraph. The result is code that behaves like ops and can be freely combined with other TensorFlow ops.

For example, this Python function:

```
def f(x):
  if x < 0:
    x = -x
  return x
```

would be converted to this:

```
def graph_mode_f(x):
  with tf.name_scope('f'):

    def if_true():
      with tf.name_scope('if_true'):
        x_1, = x,
        x_1 = tf.negative(x_1)
        return x_1,

    def if_false():
      with tf.name_scope('if_false'):
        x_1, = x,
        return x_1,
    x = ag__.utils.run_cond(tf.greater(x, 0), if_true, if_false)
    return x
```

so you can use it like an op:

```
with tf.Graph().as_default():
  x = tf.constant(-1.0)

  converted_f = autograph.to_graph(f)
  y = converted_f(x)

  with tf.Session() as sess:
    print(sess.run(y))
    # Output: 1
```

# Getting started

Use AutoGraph in one of the following ways, described below:

 1. Annotations (simpler)
 2. Functional API (more flexible)

To get started, install the latest nightly TensorFlow build:

```shell
pip install -U tf-nightly
```

Then import the `autograph` module from `tf.contrib`:

```
from tensorflow.contrib import autograph as ag
```

### Interactive demo notebooks

For more extensive examples, check out these interactive notebooks:

 * [RNN trained using Keras and Estimators](https://colab.sandbox.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/contrib/autograph/examples/notebooks/rnn_keras_estimator.ipynb)
 * [Demo from the TF Dev Summit 2018](https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/contrib/autograph/examples/notebooks/dev_summit_2018_demo.ipynb)

## Using with annotations

Annotating a function or class with `@convert` converts it in place:

```
@ag.convert()
def f(x):
  if x < 0:
    x = -x
  return x
```

... so that it always outputs TensorFlow code:

```
with tf.Graph().as_default():
  x = tf.constant(-1)

  y = f(x)

  with tf.Session() as sess:
    print(sess.run(y))
    # Output: 1
```

## Using the functional API

The functional API allows you to convert an existing function, class or object after it was defined:

```
converted_f = ag.to_graph(f)

print(converted_f(tf.constant(-1)))
# Output: Tensor

print(f(-1))
# Output: 1
```

You can use the functional API to inspect the generated code as well:

```
print(ag.to_code(f))
# Output: <Python and TensorFlow code>
```