rdv
rdv

Reputation: 732

FailedPreconditionError: Attempting to use uninitialized value

I am trying to put together a neural network, and I want to save the weights with which I initialise the network for later use.

This is the code that creates the network:

def neural_network_model(data, layer_sizes):
    num_layers = len(layer_sizes) - 1 # hidden and output layers
    layers = [] # hidden and output layers

    # initialise the weights
    weights = {}
    for i in range(num_layers):
        w_name = 'W' + str(i+1)
        b_name = 'b' + str(i+1)
        w = tf.get_variable(w_name, [layer_sizes[i], layer_sizes[i+1]], 
            initializer = tf.contrib.layers.xavier_initializer(), dtype=tf.float32)
        b = tf.get_variable(b_name, [layer_sizes[i+1]], 
            initializer = tf.zeros_initializer(), dtype=tf.float32)
        layers.append({'weights': w, 'biases': b})
        weights[w_name] = w

    # save the weights dictionary
    saver = tf.train.Saver(var_list=weights)
    with tf.Session() as sess:
        sess.run(init)   
        save_path = saver.save(sess, path + 'weights.ckpt') # path is set elsewhere

The errors that I get is as follows:

FailedPreconditionError: Attempting to use uninitialized value b2

(I also got W1 and W3 as uninitialised values).

How is this possible? Should the sess.run(init) (I specify init as tf.global_variables_initializer() earlier in the code) not take care of all variable initialisation?

Upvotes: 0

Views: 1164

Answers (1)

layog
layog

Reputation: 4801

You need to create the variable initializer in the end, when you are done creating the graph.
When you call tf.global_variable_initializer(), it takes all the trainable variables that have been created up until that point. So, if you define this before creating your layers (and variables), those new variables won't be added to this initializer.

Upvotes: 1

Related Questions