What is the best way to initialize the Variable in TensorFlow?

Viewed 6877

In TensorFlow, I can initialize variables in two the ways:

  1. Call global_variable_intializer() before declaration of variable:

    import tensorflow as tf
    
    # Initialize the global variable and session
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)
    
    W = tf.Variable([.3], tf.float32)
    x = tf.Variable([-.3], tf.float32)
    b = tf.Variable([-.3], tf.float32)
    linear_model = W * x + b
    
  2. Call global_variable_intializer() after declaration of variable:

    import tensorflow as tf
    
    W = tf.Variable([.3], tf.float32)
    x = tf.Variable([-.3], tf.float32)
    b = tf.Variable([-.3], tf.float32)
    linear_model = W * x + b 
    
    # Initialize the global variable and session
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)
    

What is the difference between both? Which is the best way to initialize variables?

Edit

This is the actual program I am running :

import tensorflow as tf

# Initialize the global variable and session
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)

x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)



linear_model = W * x + b

square_delta = tf.square(linear_model - y)

loss = tf.reduce_sum(square_delta)

fixW = tf.assign(W, [-1.])
fixb = tf.assign(b, [1.])

sess.run([fixW, fixb])

print(sess.run(loss, {x:[1,2,3,4], y:[0,-1,-2,-3]}))
3 Answers
Related