杨梓东
杨梓东

Reputation: 25

tensorflow fail to initialize some variable

I use the following code to initialize some variable in tensorflow. To my surprise, I got variable 'V' as an empty list while other variables were reasonable numbers. Only If I comment the code for weights_W, I was able to get values for 'V'.

I am new to tensorflow. Did I mess anything up?

Code:

import math
import tensorflow as tf

layer_sizes = [4, 5, 3]
shapes = zip(layer_sizes[:-1], layer_sizes[1:])
L = len(layer_sizes)-1

def bi(inits, size, name):
    return tf.Variable(inits * tf.ones([size]), name=name)

def wi(shape, name):
    return tf.Variable(tf.random_normal(shape, name=name)) / math.sqrt(shape[0])

weights_W = {'W': [wi(s, "W") for s in shapes]}
weights_V = {'V': [wi(s[::-1], "V") for s in shapes]}
bias_b = {'beta': [bi(0.0, layer_sizes[l+1], "beta") for l in range(L)]}
bias_g = {'gamma': [bi(1.0, layer_sizes[l+1], "gamma") for l in range(L)]}

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    print(sess.run(weights_W))
    print(sess.run(weights_V))
    print(sess.run(bias_b))
    print(sess.run(bias_g))

Outputs:

{'W': [array([[ 0.87929118, -1.52028453, -0.25481933, -0.0707642 ,  0.21771625],
   [-0.46657208, -0.08346261,  0.18036443, -0.75888193,  0.41950777],
   [-0.7241388 ,  0.08610565, -0.6172654 , -0.40768555,  0.24912448],
   [ 0.38304791, -0.16632535,  1.0700382 , -0.06679908, -0.68657762]], dtype=float32), array([[-0.37793589, -0.14964254, -0.56092912],
   [-0.18630502, -0.31269881,  0.25770813],
   [-0.12167504, -0.20703614, -0.06239036],
   [ 0.35287923, -0.67617333, -0.01133266],
   [ 0.24189886, -0.34389392,  0.0007165 ]], dtype=float32)]}
{'V': []}
{'beta': [array([ 0.,  0.,  0.,  0.,  0.], dtype=float32), array([ 0.,  0.,  0.], dtype=float32)]}
{'gamma': [array([ 1.,  1.,  1.,  1.,  1.], dtype=float32), array([ 1.,  1.,  1.], dtype=float32)]}

I am using tensorflow 1.2.1 in python 3.5.4 without GPUs.

Thanks for any help.

Upvotes: 1

Views: 75

Answers (1)

Mehraban
Mehraban

Reputation: 3334

It's because zip returns an iterator and you can iterate over it only once.

You can make a list of the zip and use that instead.

shapes = list(zip(layer_sizes[:-1], layer_sizes[1:]))

Upvotes: 1

Related Questions