title: How to reuse a simple dense Tensorflow graph in a program?
body: <p>I am pretty new to Tensorflow,and was trying to find out a way of saving and restoring a dense layer using simple programs. I used the following simple program to initialise and save the graph.</p>
  
  <pre><code>from __future__ import absolute_import, division, print_function
  
  import os
  
  import tensorflow as tf
  
  x = tf.constant([[1], [2], [3], [4]], dtype=tf.float32,name = "x" )
  y_true = tf.constant([[0], [-1], [-2], [-3]], dtype=tf.float32, name = "y_t")
  
  linear_model = tf.layers.Dense(units=1, name = "sutej")
  
  y_pred = linear_model(x)
  loss = tf.losses.mean_squared_error(labels=y_true, predictions=y_pred)
  
  optimizer = tf.train.GradientDescentOptimizer(0.01)
  train = optimizer.minimize(loss)
  
  init = tf.global_variables_initializer()
  
  sess = tf.Session()
  sess.run(init)
  
  for i in range(1000):
    _, loss_value = sess.run((train, loss))
    print(loss_value)
  
  all_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
  print(all_vars)
  for v in all_vars:
      v_ = sess.run(v)
      print(v_)
  
  print(sess.run(y_pred))
  
  saver = tf.train.Saver()
  
  saver.save(sess, '/home/sutej/Tensorflow/newsave/newsave',global_step=1000)
  </code></pre>
  
  <p>And the following code to restore the graph</p>
  
  <pre><code>from __future__ import absolute_import, division, print_function
  
  import os
  
  import tensorflow as tf
  
  sess = tf.Session()
  saver = tf.train.import_meta_graph('/home/sutej/Tensorflow/newsave/newsave-1000.meta')
  saver.restore(sess,tf.train.latest_checkpoint('/home/sutej/Tensorflow/newsave/'))
  
  graph = tf.get_default_graph()
  x=graph.get_tensor_by_name('x:0')
  y_true=graph.get_tensor_by_name('y_t:0')
  
  graph = tf.get_default_graph()
  
  all_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
  print(all_vars)
  for v in all_vars:
      v_ = sess.run(v)
      print(v_)
  
  print(sess.run('sutej/kernel:0'))
  
  
  print(sess.run(tf.layers.dense(inputs=x,units=1,name = 'sutej', reuse=True)))
  </code></pre>
  
  <p>In the output I am able to see that the graph has been restored and so have the constants, biases and weights from the saved file. But when I try to pass the inputs through the dense layer (last line of my restore code) I am getting an error. The output log is as follows.  </p>
  
  <pre><code>[&lt;tf.Variable 'sutej/kernel:0' shape=(1, 1) dtype=float32_ref&gt;, &lt;tf.Variable 'sutej/bias:0' shape=(1,) dtype=float32_ref&gt;]
  [[-0.98440635]]
  [0.95415276]
  [[-0.98440635]]
  Traceback (most recent call last):
    File "load_saver.py", line 26, in &lt;module&gt;
      print(sess.run(tf.layers.dense(inputs=x,units=1,name = 'sutej', reuse=True)))
    File "/home/sutej/.local/lib/python3.5/site-packages/tensorflow/python/layers/core.py", line 253, in dense
      return layer.apply(inputs)
    File "/home/sutej/.local/lib/python3.5/site-packages/tensorflow/python/layers/base.py", line 828, in apply
      return self.__call__(inputs, *args, **kwargs)
    File "/home/sutej/.local/lib/python3.5/site-packages/tensorflow/python/layers/base.py", line 699, in __call__
      self.build(input_shapes)
    File "/home/sutej/.local/lib/python3.5/site-packages/tensorflow/python/layers/core.py", line 138, in build
      trainable=True)
    File "/home/sutej/.local/lib/python3.5/site-packages/tensorflow/python/layers/base.py", line 546, in add_variable
      partitioner=partitioner)
    File "/home/sutej/.local/lib/python3.5/site-packages/tensorflow/python/training/checkpointable.py", line 436, in _add_variable_with_custom_getter
      **kwargs_for_getter)
    File "/home/sutej/.local/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 1317, in get_variable
      constraint=constraint)
    File "/home/sutej/.local/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 1079, in get_variable
      constraint=constraint)
    File "/home/sutej/.local/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 425, in get_variable
      constraint=constraint)
    File "/home/sutej/.local/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 394, in _true_getter
      use_resource=use_resource, constraint=constraint)
    File "/home/sutej/.local/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 751, in _get_single_variable
      "reuse=tf.AUTO_REUSE in VarScope?" % name)
  ValueError: Variable sutej/kernel does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=tf.AUTO_REUSE in VarScope?
  </code></pre>
  
  <p>What is that I am getting wrong? And how can I work around that? I do not want to manually multiply the kernel and add the bias, but I am looking for something more elegant like <code>print(sess.run(y_pred))</code> in the first part of the code. </p>
  
  <p>Thank you for the help.</p>