Skip to content

Instantly share code, notes, and snippets.

@gSrikar
Last active July 24, 2017 00:30
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 gSrikar/12060095edd8797605636699fccc0a18 to your computer and use it in GitHub Desktop.
Save gSrikar/12060095edd8797605636699fccc0a18 to your computer and use it in GitHub Desktop.
Placeholders allow us to feed data to the model during the session. To get more detailed information about it, take a look at my blog post at http://gsrikar.blogspot.com/2017/06/tensorflow-how-to-use-placeholders.html
'''
Class uses placeholders instead of constants.
'''
import tensorflow as tf
def main():
'''
Main Method
'''
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
# Log the tensors
print('A: ', a) # A: Tensor("Placeholder:0", dtype=float32)
print('B: ', b) # B: Tensor("Placeholder_1:0", dtype=float32)
# Add the two tensors
adder_node = tf.add(a, b)
print('Adder Node: ', adder_node) # Adder Node: Tensor("Add:0", dtype=float32)
# Start a session
with tf.Session() as sess:
# Add two numbers
result1 = sess.run(adder_node, {a: 1.3, b: 5})
# Add two 1x2 matrices
result2 = sess.run(adder_node, {a: [2, 8], b: [3, 5]})
# Add two 3x3 matrices
result3 = sess.run(adder_node, {a: [[1, 7, 9], [4, 3, 6], [5, 1, 9]],
b: [[2, 8, 2], [7, 9, 0], [4, 4, 5]]})
print('Result, Add 1: ', result1) # Result, Add 1: 6.3
print('Result, Add 2: ', result2) # Result, Add 2: [ 5. 13.]
print('Result, Add 3: ', result3)
# Result, Add 3: [[ 3. 15. 11.]
# [ 11. 12. 6.]
# [ 9. 5. 14.]]
if __name__ == "__main__":
'''
Starting point
'''
# This file is run directly
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment