tensorflow6_session.py 738 B

12345678910111213141516171819202122232425262728293031
  1. # View more python learning tutorial on my Youtube and Youku channel!!!
  2. # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
  3. # Youku video tutorial: http://i.youku.com/pythontutorial
  4. """
  5. Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
  6. """
  7. from __future__ import print_function
  8. import tensorflow as tf
  9. matrix1 = tf.constant([[3, 3]])
  10. matrix2 = tf.constant([[2],
  11. [2]])
  12. product = tf.matmul(matrix1, matrix2) # matrix multiply np.dot(m1, m2)
  13. # method 1
  14. sess = tf.Session()
  15. result = sess.run(product)
  16. print(result)
  17. sess.close()
  18. # method 2
  19. with tf.Session() as sess:
  20. result2 = sess.run(product)
  21. print(result2)