basic_operations.py 567 B

1234567891011121314151617181920212223
  1. import tensorflow as tf
  2. #With constants
  3. a = tf.constant(2)
  4. b = tf.constant(3)
  5. with tf.Session() as sess:
  6. print "a=2, b=3"
  7. print "Addition with constants: %i" % sess.run(a+b)
  8. print "Multiplication with constants: %i" % sess.run(a*b)
  9. #With variables
  10. a = tf.placeholder(tf.types.int16)
  11. b = tf.placeholder(tf.types.int16)
  12. add = tf.add(a, b)
  13. mul = tf.mul(a, b)
  14. with tf.Session() as sess:
  15. print "Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3})
  16. print "Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3})