Skip to content

Commit fd00868

Browse files
committed
update addition example's python code for tf2.5
1 parent 00faff6 commit fd00868

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

examples/addition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn main() -> Result<(), Box<dyn Error>> {
4949
let mut args = SessionRunArgs::new();
5050
args.add_feed(&graph.operation_by_name_required("x")?, 0, &x);
5151
args.add_feed(&graph.operation_by_name_required("y")?, 0, &y);
52-
let z = args.request_fetch(&graph.operation_by_name_required("z")?, 0);
52+
let z = args.request_fetch(&graph.operation_by_name_required("Identity")?, 0);
5353
session.run(&mut args)?;
5454

5555
// Check our results.

examples/addition/addition.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
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+
return tf.add(x, y)
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)
17+
18+
# check inputs/outputs node names to refer from Rust later on
19+
print(f'input nodes : {concrete_function.inputs}')
20+
print(f'output nodes : {concrete_function.outputs}')

examples/addition/model.pb

78 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)