SHARE
TWEET

Untitled

a guest Feb 20th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tensorflow as tf
  2.  
  3. my_local = tf.get_variable("my_local", shape=(), collections=[tf.GraphKeys.LOCAL_VARIABLES],
  4.                            initializer=tf.constant_initializer(1.0))
  5. my_global = tf.get_variable("my_global", shape=(),
  6.                             initializer=tf.constant_initializer(2.0))
  7.  
  8. target_value = tf.constant(4.0)
  9. loss = tf.abs(my_local + my_global - target_value)
  10. optim = tf.train.AdamOptimizer(learning_rate=1.0).minimize(loss)
  11.  
  12. for v in tf.trainable_variables():
  13.     print(v.name)
  14.  
  15. with tf.Session() as sess:
  16.     sess.run(tf.global_variables_initializer())
  17.     sess.run(tf.local_variables_initializer())
  18.     print("local init: ", sess.run(my_local))
  19.     print("global init: ", sess.run(my_global))
  20.     for i in range(2):
  21.         _, l = sess.run([optim, loss])
  22.         print("loss {:.4f}".format(l))
  23.         print("local: ", sess.run(my_local))
  24.         print("global: ", sess.run(my_global))
  25.      
  26. my_local:0
  27. my_global:0
  28. local init:  1.0
  29. global init:  2.0
  30. loss 1.0000
  31. local:  1.9999996
  32. global:  2.9999995
  33. loss 1.0000
  34. local:  1.9473683
  35. global:  2.9473681
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top