title: Understanding Variable scope example in Tensorflow
body: <p>I was looking at the mechanics section for Tensorflow, specifically on <a href="https://chromium.googlesource.com/external/github.com/tensorflow/tensorflow/+/r0.7/tensorflow/g3doc/how_tos/variable_scope/index.md" rel="nofollow noreferrer">shared variables</a>. In the section "The problem", they are dealing with a convolutional neural net, and provide the following code (which runs an image through the model):</p>
  
  <pre class="lang-python prettyprint-override"><code># First call creates one set of variables.
  result1 = my_image_filter(image1)
  # Another set is created in the second call.
  result2 = my_image_filter(image2)
  </code></pre>
  
  <p>If the model was implemented in such a way, would it then be impossible to learn/update the parameters because there's a new set of parameters for each image in my training set?</p>
  
  <p>Edit:
  I've also tried "the problem" approach on a simple linear regression <a href="https://github.com/nlintz/TensorFlow-Tutorials/blob/master/1_linear_regression.py" rel="nofollow noreferrer">example</a>, and there do not appear to be any issues with this method of implementation. Training seems to work as well as can be shown by the last line of the code. So I'm wondering if there is a subtle discrepancy in the tensorflow documentation and what I'm doing. :</p>
  
  <pre class="lang-python prettyprint-override"><code>import tensorflow as tf
  import numpy as np
  
  trX = np.linspace(-1, 1, 101)
  trY = 2 * trX + np.random.randn(*trX.shape) * 0.33 # create a y value which is         approximately linear but with some random noise
  
  X = tf.placeholder("float") # create symbolic variables
  Y = tf.placeholder("float")
  
  
  def model(X):
      with tf.variable_scope("param"):
          w = tf.Variable(0.0, name="weights") # create a shared variable (like theano.shared) for the weight matrix
  
      return tf.mul(X, w) # lr is just X*w so this model line is pretty simple
  
  
  y_model = model(X)
  
  cost = (tf.pow(Y-y_model, 2)) # use sqr error for cost function
  
  train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost) # construct an optimizer to minimize cost and fit line to my data
  
  sess = tf.Session()
  init = tf.initialize_all_variables() # you need to initialize variables (in this case just variable W)
  sess.run(init)
  
  with tf.variable_scope("train"):
      for i in range(100):
          for (x, y) in zip(trX, trY):
          sess.run(train_op, feed_dict={X: x, Y: y})
  
  print sess.run(y_model, feed_dict={X: np.array([1,2,3])})
  </code></pre>