4

I am trying to initialize a tf.Variable() in a tf.InteractiveSession(). I already have some pre-trained weights which are individual numpy files. How do I effectively initialize the variable with these numpy values ?

I have gone through the following options:

  1. Using tf.assign()
  2. using sess.run() directly during tf.Variable() creation

Seems like the values are not correctly initialized. Following is some code I have tried. Let me know which is the correct one ?

def read_numpy(file):
    return np.fromfile(file,dtype='f')

def build_network():
    with tf.get_default_graph().as_default():
        x = tf.Variable(tf.constant(read_numpy('foo.npy')),name='var1')
        sess = tf.get_default_session()
        with sess.as_default():
            sess.run(tf.global_variables_initializer())

sess = tf.InteractiveSession()
with sess.as_default():
    build_network()

Is this the correct way to do it ? I have printed the session object, and it is the same session used throughout.

edit : Currently it seems like using sess.run(tf.global_variables_initializer()) is calling a random initialize op

1 Answer 1

3

tf.Variable() accepts numpy arrays as initial values:

import tensorflow as tf
import numpy as np

init = np.ones((2, 2))
x = tf.Variable(init) # <-- set initial value to assign to a variable

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer()) # <-- this will assign the init value
    print(x.eval())
# [[1. 1.]
#  [1. 1.]]

So just use the numpy array to initialize, no need to convert it to a tensor first.

Alternatively, you could also use tf.Variable.load() to assign values from numpy array to a variable within a session context:

import tensorflow as tf
import numpy as np

x = tf.Variable(tf.zeros((2, 2)))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    init = np.ones((2, 2))
    x.load(init)
    print(x.eval())
# [[1. 1.]
#  [1. 1.]]
Sign up to request clarification or add additional context in comments.

4 Comments

is it fine to use the tf.get_variable() ? .. I used that and it worked. I had an attribute as initializer=tf.constant(read_numpy('foo.npy'))
Yes, you can. The difference between tf.Variable() and tf.get_variable() is that the tf.Variable() creates a new variable, whereas get_variable() may create a new one or may return an existing variable. For more read this answer
Oh okay, I did get an error on this first time when I used tf.get_variable() since naming was clashing.. but then I included it within a tf.variable_scope() and that was handled. thanks for the response !
Glad it helped. I would still use x = tf.Variable(read_numpy('foo.npy')). This way it is shorter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.