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:
- Using
tf.assign() - using
sess.run()directly duringtf.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