Skip to content

Instantly share code, notes, and snippets.

@jzstark
Last active February 14, 2019 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jzstark/c35847ca2ace09af7bf617b704ce5c95 to your computer and use it in GitHub Desktop.
Save jzstark/c35847ca2ace09af7bf617b704ce5c95 to your computer and use it in GitHub Desktop.
A initial simple example of owl-tensorflow cgraph convert

Owl-Tensorflow Converter

This gist shows an initial simple demonstration of how to define a computation graph in Owl, and then execute it in Tensorflow.

  • Step 1 : running OCaml script tf_converter.ml, which generates a file test_cgraph.pbtxt
  • Step 2 : make sure test_cgraph.pbtxt and exec_cgraph.py in the same graph; make sure Tensorflow/numpy etc. is installed.
  • Step 3 : execute python exec_cgraph.py, and the expected printed output is a 3x3 matrix.

Here we only assume the python script writer knows where to find the output node (in collection "result").

There could be many posssible source of error at this stage, one of which could be incompatible tensorflow version; in that case, probably find this line in test_cgraph.pbtxt : tensorflow_version: "1.12.0" and then change the version number.

#!/usr/bin/env python
from __future__ import print_function
import numpy as np
import os
import tensorflow as tf
from google.protobuf import text_format
from tensorflow.python.framework import graph_io
def eval(meta_file, verbose=False, yvalue=3.):
with tf.Graph().as_default():
sess = tf.Session()
saver = tf.train.import_meta_graph(meta_file)
graph = tf.get_default_graph()
x = graph.get_tensor_by_name('x:0')
y = graph.get_tensor_by_name('y:0')
result = tf.get_collection("result")[0]
init = tf.global_variables_initializer()
sess.run(init)
arr = np.ones((3, 3))
foo = sess.run(result, feed_dict={x:arr, y:yvalue})
if verbose == True:
g = saver.export_meta_graph()
print(g)
print(foo)
filename = 'test_cgraph'
with open(filename + '.pbtxt', 'r') as f:
metagraph_def = tf.MetaGraphDef()
file_content = f.read()
text_format.Merge(file_content,metagraph_def)
graph_io.write_graph(metagraph_def,
os.path.dirname(filename),
os.path.basename(filename) + '.pb',
as_text=False)
eval(filename+'.pb', yvalue=1.)
#!/usr/bin/env owl
#require "owl-tensorflow"
open Owl
open Owl_tensorflow
open Owl_converter
module N = Dense.Ndarray.S
module G = Owl_computation_cpu_engine.Make (N)
module T = Owl_converter.Make (G)
include Owl_algodiff_generic.Make (G)
(* construct computation in AD *)
let f x y =
let weight = Mat.ones 3 3 in
Maths.( (pack_flt 2.) * (x *@ weight + y) + (pack_flt 1.))
let x = G.var_arr "x" |> pack_arr
let y = G.var_elt "y" |> pack_elt
let z = f x y
(* build computation graph *)
let output = [| unpack_arr z |> G.arr_to_node |]
let input = [|
unpack_arr x |> G.arr_to_node;
unpack_elt y |> G.elt_to_node
|]
let g = G.make_graph ~input ~output "graph_01"
(* evaluate graph *)
let x_val = N.ones [|3; 3|]
let y_val = 3.
let _ = G.assign_arr (unpack_arr x) x_val
let _ = G.assign_elt (unpack_elt y) y_val
let _ = G.eval_graph g
(* output to tensorflow pbtxt *)
let _ =
let pbtxt = T.(convert g |> to_pbtxt) in
(* print_endline pbtxt *)
Owl_io.write_file "test_cgraph.pbtxt" pbtxt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment