tensorflow7_variable.py 945 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. state = tf.Variable(0, name='counter')
  10. #print(state.name)
  11. one = tf.constant(1)
  12. new_value = tf.add(state, one)
  13. update = tf.assign(state, new_value)
  14. # tf.initialize_all_variables() no long valid from
  15. # 2017-03-02 if using tensorflow >= 0.12
  16. if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
  17. init = tf.initialize_all_variables()
  18. else:
  19. init = tf.global_variables_initializer()
  20. with tf.Session() as sess:
  21. sess.run(init)
  22. for _ in range(3):
  23. sess.run(update)
  24. print(sess.run(state))