Created
June 1, 2019 02:20
-
-
Save brandonwillard/2c1beb922310bdebc50929dddba14326 to your computer and use it in GitHub Desktop.
Investigating unification for TensorFlow graphs/objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import tensorflow_probability as tfp | |
from tensorflow.python.framework import ops | |
ops.disable_eager_execution() | |
x_tf = tf.constant(1, name='x', dtype=tf.float64) | |
# An `Operation` | |
x_op = x_tf.op | |
x_op.type | |
# | |
# Where's the value (i.e. 1), name and dtype in the Op inputs? | |
# | |
list(x_op.inputs) | |
x_op.outputs | |
# ... | |
# the REAL operator is an `OpDef` (wtf?) | |
# yes, it's one of those protobuf objects | |
x_op_def = x_op.op_def | |
x_op_def.name == x_op.type | |
# is it `x_op.inputs`? | |
x_op_def.input_arg | |
# we've found our missing parameters/inputs: | |
list(x_op_def.attr) | |
# but those are just the "definition" of an Op, the | |
# actual instantiated Op object is `x_op` above. | |
# now, we know what to ask for in the protobuf `x_op` object. | |
x_op.get_attr("value") | |
x_op.get_attr("dtype") | |
# | |
# Let's use this to unify TF objects/graphs. | |
# | |
from unification import unify, reify, var, variables | |
y_tf = tf.constant(2, name='y', dtype=tf.float64) | |
z_tf = tf.add(x_tf, y_tf, name='z') | |
# How? | |
# unify(z_tf, ('add', var('x'), 2), {}) | |
unify((z_tf.op.type, z_tf.op.inputs[0], z_tf.op.inputs[1]), | |
('Add', var('x'), 2), | |
{}) | |
unify(var('x'), z_tf.op.inputs[0], {}) | |
unify(z_tf, ..., {}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment