Skip to content

Commit b78afaf

Browse files
authored
Merge pull request #307 from dskkato/examples/addition
Update addition example's python code for tf2.5
2 parents 00faff6 + 4850f7d commit b78afaf

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

examples/addition/addition.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
# TODO: Stop using v1 compatibility
2-
import tensorflow.compat.v1 as tf
1+
import tensorflow as tf
32

3+
# check tensorflow version is 2.x
4+
tf_major_version = tf.__version__.split('.')[0]
5+
assert tf_major_version == '2'
46

5-
tf.disable_eager_execution()
6-
x = tf.placeholder(tf.int32, name = 'x')
7-
y = tf.placeholder(tf.int32, name = 'y')
8-
z = tf.add(x, y, name = 'z')
7+
@tf.function
8+
def add(x, y):
9+
tf.add(x, y, name='z')
910

10-
tf.variables_initializer(tf.global_variables(), name = 'init')
11+
x = tf.TensorSpec((), dtype=tf.dtypes.int32, name='x')
12+
y = tf.TensorSpec((), dtype=tf.dtypes.int32, name='y')
1113

12-
definition = tf.Session().graph_def
14+
concrete_function = add.get_concrete_function(x, y)
1315
directory = 'examples/addition'
14-
tf.train.write_graph(definition, directory, 'model.pb', as_text=False)
16+
tf.io.write_graph(concrete_function.graph, directory, 'model.pb', as_text=False)

examples/addition/model.pb

40 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)