Skip to content

Instantly share code, notes, and snippets.

@protoget
Last active April 16, 2017 21:43
Show Gist options
  • Save protoget/8de816a14ec0834180bf5362d866de0b to your computer and use it in GitHub Desktop.
Save protoget/8de816a14ec0834180bf5362d866de0b to your computer and use it in GitHub Desktop.
import tensorflow as tf
tf.reset_default_graph()
g1 = tf.Graph()
with g1.as_default():
print g1
with tf.name_scope('myscope'):
keys = [1]
values = ["foo"]
t1 = tf.contrib.lookup.HashTable(
tf.contrib.lookup.KeyValueTensorInitializer(keys, values, key_dtype=tf.int64),
"no luck",
# one line change and hashtable is now shared across sessions
shared_name="hehe",
name="my_table")
init_op = t1.init
ts = tf.constant([1], dtype=tf.int64)
lookup_op = t1.lookup(ts)
with tf.Session("grpc://localhost:9000") as sess:
sess.run(init_op)
print "t1.name: ", t1.name
print sess.run(lookup_op)
# complain table not initialized
with tf.Session("grpc://localhost:9000") as sess:
# sess.run(init_op)
print "t1.name: ", t1.name
print sess.run(lookup_op)
import tensorflow as tf
tf.reset_default_graph()
g1 = tf.Graph()
with g1.as_default():
print g1
with tf.name_scope('myscope'):
keys = [1]
values = ["foo"]
t1 = tf.contrib.lookup.HashTable(
tf.contrib.lookup.KeyValueTensorInitializer(keys, values, key_dtype=tf.int64),
"no luck",
shared_name="hehe",
name="my_table")
init_op = t1.init
ts = tf.constant([1], dtype=tf.int64)
lookup_op = t1.lookup(ts)
with tf.Session("") as sess:
sess.run(init_op)
print "t1.name: ", t1.name
print sess.run(lookup_op)
# tf.Session() creates a new runtime, therefore still complains table not initialized
# even if hashtable is shared (across different sessions connecting the same runtime)
with tf.Session("") as sess:
# sess.run(init_op)
print "t1.name: ", t1.name
print sess.run(lookup_op)
import tensorflow as tf
tf.reset_default_graph()
# variables are shared under the same name, in a distributed runtime across different sessions
g1 = tf.Graph()
with g1.as_default():
with tf.variable_scope('foo'):
w = tf.get_variable('w', shape=[2, 4], initializer=tf.constant_initializer([0, 1, 2, 3, 4, 5, 6, 7]))
init_op = tf.global_variables_initializer()
with tf.Session("grpc://localhost:9000") as sess:
sess.run(init_op)
print "w.name: ", w.name
print sess.run(w)
# variable already initialized
with tf.Session("grpc://localhost:9000") as sess:
# sess.run(init_op)
print "w.name: ", w.name
print sess.run(w)
# hashtables aren't shared under the same name, but under "shared_name", in a distributed runtime, across different sessions
import tensorflow as tf
tf.reset_default_graph()
g1 = tf.Graph()
with g1.as_default():
print g1
with tf.name_scope('myscope'):
keys = [1]
values = ["foo"]
t1 = tf.contrib.lookup.HashTable(
tf.contrib.lookup.KeyValueTensorInitializer(keys, values, key_dtype=tf.int64),
"no luck",
name="my_table")
init_op = t1.init
ts = tf.constant([1], dtype=tf.int64)
lookup_op = t1.lookup(ts)
with tf.Session("grpc://localhost:9000") as sess:
sess.run(init_op)
print "t1.name: ", t1.name
print sess.run(lookup_op)
# complain table not initialized
with tf.Session("grpc://localhost:9000") as sess:
# sess.run(init_op)
print "t1.name: ", t1.name
print sess.run(lookup_op)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment