Last active
June 20, 2019 20:44
-
-
Save sriharshaj/3c07465e046030d3b241dd0ef8d6a846 to your computer and use it in GitHub Desktop.
Tensorflow snippets
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
v1 = tf.get_variable("v1", shape=(), initializer=tf.zeros_initializer()) | |
v2 = tf.get_variable("v2", shape=(), initializer=tf.zeros_initializer()) | |
switch = tf.placeholder(tf.bool) | |
cond = tf.cond(switch, lambda: tf.assign(v1, 1.0), lambda: tf.assign(v2, 2.0)) | |
with tf.train.MonitoredSession() as session: | |
session.run(cond, feed_dict={switch: True}) | |
print(session.run([v1, v2])) |
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
# Control Dependency | |
x = tf.get_variable("x", shape=(), initializer=tf.zeros_initializer()) | |
assign_x = tf.assign(x, 10) | |
# z value depends on the assign_x tensor | |
with tf.control_dependencies([assign_x]): | |
z = x + 1 | |
with tf.train.MonitoredSession() as session: | |
print(session.run(z)) |
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
# Iterate over input using Tensorflow | |
X_tensor = tf.placeholder(tf.float64, (None, 32, 128)) | |
dataset = tf.data.Dataset.from_tensor_slices(X_tensor) | |
iterator = dataset.make_initializable_iterator() | |
next_element = iterator.get_next() | |
with tf.Session() as sess: | |
sess.run(iterator.initializer, feed_dict={X_tensor: X}) | |
sess.run(next_element) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment