Created
October 23, 2017 16:42
-
-
Save phizaz/21a5454ddc6c2a15c5c0eae91c96cda5 to your computer and use it in GitHub Desktop.
Stackoverflow: Connecting two graphs
This file contains hidden or 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
{ | |
"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