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

Pankit Gami

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]}))
Pietro Tortella

In the case 1, the variables are not initialized, and if you try

sess.run(linear_model)

it should give you some kind of error (FailedPreconditionError on my compiler).

Case 2 is the working one.

The command

tf.global_variables_initializer()

should be called after all variables are created, otherwise the same error will be raised.

As far as I understand, each time you call tf.Variable, the nodes related to a variable are added to the graph. These are the following:

Variable/initial_value
Variable
Variable/Assign
Variable/read

(you obtain the nodes constructed so far with the command

for n in tf.get_default_graph().as_graph_def().node:
    print n.name

)

The variable itself, has no value until you run within a Session the Variable/Assign node.

The command

init = tf.global_variables_initializer() 

creates a single node containing all the assign nodes of all the variables constructed so far, and associate it to the python variable 'init' so that when it is executed the line

sess.run(init)

all variables acquire the initial value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the best way to initialize a bean?

Best way to initialize variable in python module

What's the best way to initialize Quartz?

What is the best way to initialize a JavaScript Date to midnight?

What is the best way to define/initialize a variable after DOM has loaded in React?

What is the best way in Blazor to initialize a page that hits the DB to initialize itself

What is the correct way to initialize my number variable?

What is the correct way to initialize a variable in C++

what is the best way to multiply tensors in tensorflow

Best way to initialize QString

What's the best way to initialize a dict of dicts in Python?

What is the best way of using Arrays.asList() to initialize a List

What is the best way to initialize the following instance in my class?

What is the best way to initialize a pointer to a char array as mutable?

What is the best way to include or initialize jQuery library in Vue project?

What is the best way to pass a method when I initialize an object

What is the best way to initialize a state in ReactNative with custom values on a Modal?

What is the best way to check if a variable is a list?

What is the best way to toggle a boolean variable?

What is the best way to have variable attributes in JSX?

What is the correct way to initialize a variable from a binary buffer?

What is the best way to run saved model with different batch size in TensorFlow?

What is the best way to deploy a tensorflow trained graph into production?

What's the best way to access single gradients in a batch in TensorFlow?

Is this the right way to initialize a variable

Best way to initialize ArrayList and HashMap?

Best way to initialize class members?

Cannot initialize variable with a placeholder in tensorflow

tensorflow fail to initialize some variable