Skip to content

Instantly share code, notes, and snippets.

@phizaz
Created October 23, 2017 16:42
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 phizaz/21a5454ddc6c2a15c5c0eae91c96cda5 to your computer and use it in GitHub Desktop.
Save phizaz/21a5454ddc6c2a15c5c0eae91c96cda5 to your computer and use it in GitHub Desktop.
Stackoverflow: Connecting two graphs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4.0]\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"FLOAT = tf.float32\n",
"tf.reset_default_graph()\n",
"\n",
"def graph_1():\n",
" g = tf.Graph()\n",
" with g.as_default():\n",
" x = tf.placeholder(FLOAT, [], name='x')\n",
" y = tf.multiply(2.0, x, name='y')\n",
" return g\n",
"\n",
"def graph_2():\n",
" g = tf.Graph()\n",
" with g.as_default():\n",
" a = tf.placeholder(FLOAT, [], name='a')\n",
" b = tf.multiply(2.0, a, name='b')\n",
" return g\n",
"\n",
"# x = 1.0\n",
"x = tf.constant(1.0, FLOAT, [])\n",
"# feed x to graph_1 -> y = 2.0\n",
"g1 = graph_1()\n",
"[g1_y] = tf.import_graph_def(g1.as_graph_def(), input_map={'x': x}, return_elements=['y:0'])\n",
"# feed y to graph_2 -> b = 4.0\n",
"g2 = graph_2()\n",
"[g2_b] = tf.import_graph_def(g2.as_graph_def(), input_map={'a': g1_y}, return_elements=['b:0'])\n",
"\n",
"config = tf.ConfigProto()\n",
"config.gpu_options.allow_growth = True\n",
"with tf.Session(config=config) as sess:\n",
" print(sess.run([g2_b]))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment