Skip to content

Instantly share code, notes, and snippets.

@jzstark
Last active February 14, 2019 19:44
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/80a0c6d9d861cb7b48d11ec162d2a129 to your computer and use it in GitHub Desktop.
Save jzstark/80a0c6d9d861cb7b48d11ec162d2a129 to your computer and use it in GitHub Desktop.
Owl-Tensorflow Converter Example: Higher-Order Derivatives

Owl-Tensorflow Converter Example: Higher-Order Derivatives

In this gist, we first defines a function f0, then construct the computation graph of from the first to the fourth derivative by calling diff function from Owl's Algorithmic Differentiation module. This computation graph is then passed to Tensorflow for executation.

This is a recreation of a exsiting Owl AD module example. See the doc for more details.

  • Step 1 : running OCaml script tf_convert_diff.ml, which generates a file tf_convert_diff.pbtxt in current directory.
  • Step 2 : make sure tf_convert_diff.pbtxt and tf_convert_diff.py in the same graph; make sure Tensorflow/numpy etc. is installed.
  • Step 3 : execute python tf_convert_diff.py, and the expected output is a saved image tf_convert_diff.png in the current directory.

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

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. Also, Tensorflow may yield some warning messages about version/dataset etc. Current scripts may also ignore some factors like file/directory location.

#!/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)
let f0 x = Maths.(tanh x)
let f1 = diff f0
let f2 = diff f1
let f3 = diff f2
let f4 = diff f3
let x = G.var_arr ~shape:[|100|] "x" |> pack_arr
let y0 = f0 x
let y1 = f1 x
let y2 = f2 x
let y3 = f3 x
let y4 = f4 x
let output = [|
unpack_arr y0 |> G.arr_to_node;
unpack_arr y1 |> G.arr_to_node;
unpack_arr y2 |> G.arr_to_node;
unpack_arr y3 |> G.arr_to_node;
unpack_arr y4 |> G.arr_to_node;
|]
let input = [|
unpack_arr x |> G.arr_to_node
|]
let g = G.make_graph ~input ~output "graph_diff"
let _ =
let pbtxt = T.(convert g |> to_pbtxt) in
Owl_io.write_file "tf_convert_diff.pbtxt" pbtxt
#!/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
import matplotlib.pyplot as plt
def eval(meta_file, arr):
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')
result0 = tf.get_collection("result")[0]
result1 = tf.get_collection("result")[1]
result2 = tf.get_collection("result")[2]
result3 = tf.get_collection("result")[3]
result4 = tf.get_collection("result")[4]
init = tf.global_variables_initializer()
sess.run(init)
y0 = sess.run(result0, feed_dict={x:arr})
y1 = sess.run(result1, feed_dict={x:arr})
y2 = sess.run(result2, feed_dict={x:arr})
y3 = sess.run(result3, feed_dict={x:arr})
y4 = sess.run(result4, feed_dict={x:arr})
return y0, y1, y2, y3, y4
filename = 'tf_convert_diff'
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)
x = np.linspace(-4.0, 4.0, num=100)
y0, y1, y2, y3, y4 = eval(filename+'.pb', x)
plt.plot(x, y0, label='tanh')
plt.plot(x, y1, label='1st derivative')
plt.plot(x, y2, label='2nd derivative')
plt.plot(x, y3, label='3rd derivative')
plt.plot(x, y4, label='4th derivative')
plt.legend()
plt.title("Derivatives of function tanh(x) ")
plt.xlabel("x")
plt.ylabel("f(x)")
plt.savefig("tf_convert_diff.png", dpi=500)
@jzstark
Copy link
Author

jzstark commented Feb 14, 2019

Expected output image:

tf_convert_diff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment