0

As the title states, how does one initialize the variables from variable_scope in Tensorflow? I was not aware that it was necessary since I thought it was a constant. However, when I try to predict the output when running the session on Android, I get the error:

Error during inference: Failed precondition: Attempting to use uninitialized value weights0

[[Node: weights0/read = Identity[T=DT_FLOAT, _class=["loc:@weights0"], _device="/job:localhost/replica:0/task:0/cpu:0"](weights0)]]

I tried setting the variables with tf.Variable (i.e. 'h1': tf.Variable(vs.get_variable("weights0", [n_input, n_hidden_1], initializer=tf.contrib.layers.xavier_initializer()))) but I get the error `tensor name 'Variable' not found in checkpoint files when attempting to generate the protobuf file.

Snippet

def reg_perceptron(t, weights, biases):
    t = tf.nn.relu(tf.add(tf.matmul(t, weights['h1']), biases['b1']), name = "layer_1")
    t = tf.nn.sigmoid(tf.add(tf.matmul(t, weights['h2']), biases['b2']), name = "layer_2")
    t = tf.add(tf.matmul(t, weights['hOut'], name="LOut_MatMul"), biases['bOut'], name="LOut_Add")

    return tf.reshape(t, [-1], name="Y_GroundTruth")

g = tf.Graph()
with g.as_default():
   ...
   rg_weights = {
    'h1': vs.get_variable("weights0", [n_input, n_hidden_1], initializer=tf.contrib.layers.xavier_initializer()),
    'h2': vs.get_variable("weights1", [n_hidden_1, n_hidden_2], initializer=tf.contrib.layers.xavier_initializer()),
    'hOut': vs.get_variable("weightsOut", [n_hidden_2, 1], initializer=tf.contrib.layers.xavier_initializer())
    }


    rg_biases = {
    'b1': vs.get_variable("bias0", [n_hidden_1], initializer=init_ops.constant_initializer(bias_start)),
    'b2': vs.get_variable("bias1", [n_hidden_2], initializer=init_ops.constant_initializer(bias_start)),
    'bOut': vs.get_variable("biasOut", [1], initializer=init_ops.constant_initializer(bias_start))
    }

    pred = reg_perceptron(_x, rg_weights, rg_biases)
    ...
...

g_2 = tf.Graph()
with g_2.as_default():
    ...
    rg_weights_2 = {
    'h1': vs.get_variable("weights0", [n_input, n_hidden_1], initializer=tf.contrib.layers.xavier_initializer()),
    'h2': vs.get_variable("weights1", [n_hidden_1, n_hidden_2], initializer=tf.contrib.layers.xavier_initializer()),
    'hOut': vs.get_variable("weightsOut", [n_hidden_2, 1], initializer=tf.contrib.layers.xavier_initializer())
    }

    rg_biases_2 = {
    'b1': vs.get_variable("bias0", [n_hidden_1], initializer=init_ops.constant_initializer(bias_start)),
    'b2': vs.get_variable("bias1", [n_hidden_2], initializer=init_ops.constant_initializer(bias_start)),
    'bOut': vs.get_variable("biasOut", [1], initializer=init_ops.constant_initializer(bias_start))
    }

    pred_2 = reg_perceptron(_x_2, rg_weights_2, rg_biases_2)
    ...

EDIT

Could I be creating the protobuf file the wrong way? The code that I use for .PB generation which can be found here returns

(Here the blue line represents the target values whereas the green line shows the predicted values.)

from PB

whereas I should be getting from model

instead (from http://pastebin.com/RUFa9NkN) despite both codes using the same inputs and model.

Schlez
  • 91
  • 9
  • 1
    `sess.run(tf.initialize_all_variables())` and if that won't help `sess.run(tf.initialize_local_variables())` too. – sygi Nov 15 '16 at 19:04
  • Unfortunately, that did not work. I am still getting the same error. – Schlez Nov 15 '16 at 20:13

1 Answers1

0

If you want to use variable_scope, try something as following:

def initialize_variable(vs_name, ...): # other necessary arguments
    # a function for initialize variables    
    with tf.variable_scope(vs_name, reuse=None) as vs:
        h1 = tf.get_variable("weights0", [n_input, n_hidden_1], initializer=tf.contrib.layers.xavier_initializer())
        h2 = tf.get_variable("weights1", [n_hidden_1, n_hidden_2], initializer=tf.contrib.layers.xavier_initializer())
        hout = tf.get_variable("weightsOut", [n_hidden_2, 1], initializer=tf.contrib.layers.xavier_initializer())
        b1 = tf.get_variable("bias0", [n_hidden_1], initializer=init_ops.constant_initializer(bias_start))
        b2 = tf.get_variable("bias1", [n_hidden_2], initializer=init_ops.constant_initializer(bias_start))
        bout = tf.get_variable("biasOut", [1], initializer=init_ops.constant_initializer(bias_start))
        vs.reuse_variables()

Then in the graph, initialize the variables first use above function, then extract the variables.

g = tf.Graph()
with g.as_default():
  initialize_variable(vs_name, ...) #fill in other necessary arguments
  with tf.variable_scope(vs_name, reuse=True):
      rg_weights = {'h1' : tf.get_variable("weights0"),
                    'h2' : tf.get_variable("weights1"),
                    'hout' : tf.get_variable("weightsOut")}
      rg_biases = {'b1' : tf.get_variable("bias0"),
                   'b2' : tf.get_variable("bias1"),
                   'bOut': tf.get_variable("biasOut")}
  pred = reg_perceptron(_x, rg_weights, rg_biases)

If you don't want to involve variable_scope, try something like following... Although the following needs to feed an initial tensor in and doesn't accept an initializer.

g = tf.Graph()
with g.as_default():
   ...
   rg_weights = {'h1': tf.Variable(tf.truncated_normal([n_input, n_hidden1], mean, stddev), name='weights0')
                 'h2': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2],mean, stddev), name="weights1"),
                 'hOut': tf.Variable(tf.truncated_normal([n_hidden_2, 1],mean, stddev), name="weightsOut")}
   ...

Here is some documentation and example about variable sharing in TensorFlow

Zhongyu Kuang
  • 5,104
  • 2
  • 25
  • 26
  • Thanks for the reply. I will try that out tomorrow as I am unable to connect to Jupyter at the moment. – Schlez Nov 16 '16 at 17:50
  • Just a quick question (I have not regained access to Jupyter yet) but would it be possible to generate the protobuf file without having to remake the checkpoint files? – Schlez Nov 18 '16 at 22:39
  • I'm not very familiar with this topic. But to my knowledge checkpoint files only save graph variables, but not graph structure. If you have old saved checkpoint files and you can reconstruct the graph structure, I believe you can generate protobuf file from there without needing to remake checkpoint files. Here is two possibly useful threads: [thread 1](http://stackoverflow.com/questions/38947658/tensorflow-saving-into-loading-a-graph-from-a-file) and [thread 2](http://stackoverflow.com/questions/34343259/is-there-an-example-on-how-to-generate-protobuf-files-holding-trained-tensorflow) – Zhongyu Kuang Nov 19 '16 at 03:19
  • Ah I had assumed that I needed to insert that code in the graph used to initialize the variables as well. I have checked out those threads already and used them as the basis of how I created the .pb file. However, with the changes, I am getting the same error only with `vs_name/weights0/read` instead of `weights0/read` (I have no need of any additional arguments for `intialize_variables`). The modified protobuf creation code can be found [here](http://pastebin.com/CSmpYwhF) (lines 45-54 and 131-138) – Schlez Nov 19 '16 at 12:42
  • Which line threw the error? I notice that you have two graphs, the implementation in your first graph is the same as your old implementation(line 74-84). What's the full trace error message? – Zhongyu Kuang Nov 19 '16 at 16:33
  • I have downloaded your code, and I commented out the code in the first graph, as I don't have any files stored in my local station and the connection btw the two graphs is a bit obscure to me. I ran the code with a randomly generated test_x to feed into the graph and everything went fine without throwing any error – Zhongyu Kuang Nov 19 '16 at 16:51
  • Yeah for some reason, running the code directly in Python does not return any error. It seems to just happen when I run load the pb file on Android. – Schlez Nov 19 '16 at 17:07
  • I forgot to mention that while the code does not return errors in Python, the output can't be correct as I get very different outputs every time I run `predy = sess_2.run(pred_2, feed_dict={_x_2: INPUT)` even though the input remains the same. – Schlez Nov 19 '16 at 17:14
  • My checkpoint file can be found [here](https://github.com/tensorflow/tensorflow/files/601598/Y6_1476978999.zip). I tried directly generating a .pb file (http://pastebin.com/HcWA0xMQ) and using `freeze_graph` from tensorflow. This did not return any error when loading it up on Android. However, the size of the resulting graph is smaller than the checkpoint file and for some reason I am getting the same result regardless of the input. – Schlez Nov 19 '16 at 18:17
  • Just wanted to say I got it fixed while using the frozen graph. There is a bug in the JNI interface which messed up the values (accidentally saved as int instead of float). – Schlez Nov 19 '16 at 22:42
  • Oh okay, great to hear that! Sorry I was away and I just saw the message! – Zhongyu Kuang Nov 19 '16 at 23:50