title: Tensorflow - training weights via GD by hand
body: <p>I am new to Tensorflow and I want to train a toy neural network to solve the xor problem. I want to be able to pass in two bits as inputs and have 1 bit as an output. I will have 4 training examples: <code>[0,0], [0,1], [1,0], [1,1]</code></p>
  
  <p>I understand how the forward feed works but I do not exactly know how I would optimize the weights?</p>
  
  <p>I do not want to use a built in function such as</p>
  
  <pre><code>train_step = tf.train.GradientDescentOptimizer(0.3).minimize(mse)
  </code></pre>
  
  <p>Instead I want to calculate the deltas and gradients by for each individual node (with respect to how many deltas and gradiants are needed for the node type)</p>
  
  <p>I can take in the Y_hat and subtract from the true Y to get my error. I can then calculate my delta for this output and calculate the deltas for the hidden nodes. Using the deltas I can calculate my gradiants. Using these gradiants I can calculate how much I need to change each weight by.</p>
  
  <p>My issue is with updating the weights, since I am giving the session.run 4 inputs (the training examples) When I update the weights I get a dimention error because there are 4 training examples and the weights are for each example not 4 at a time.</p>
  
  <p>I am not sure how I would update the weights for each sample individually. </p>
  
  <p>Here is some code that I have written:</p>
  
  <pre><code>'''
  Simple neural network to solve the xor problem
  2 inputs representing bits of the xor gate
  1 output representing the output bit of the xor gate
  '''
  
  import tensorflow as tf
  import numpy as np
  # build computational graph
  n_input = 2
  n_hidden = 2
  n_classes = 1
  
  #weights describing which layer it is affecting
  #ie h1 affects layer 1
  weights = {
      'h1': tf.Variable(tf.random_normal([n_input, n_hidden])),# inputs x hidden
      'ol': tf.Variable(tf.random_normal([n_hidden, n_classes]))
  }
  #biases describing which layer it is affecting
  #ie b1 affects layer 1
  biases = {
      'b1': tf.Variable(tf.random_normal([n_hidden])),
      'ol': tf.Variable(tf.random_normal([n_classes]))
          }
  
  def sigmoid(x, derivative = False):
      if derivative == True:
          return (1.0/(1+tf.exp(-x))) * (1.0 - (1.0/(1+tf.exp(-x))))
      return 1.0/(1+tf.exp(-x))
  
  def forward(x, weights, biases):
      # 1st hidden layer
      layer1 = tf.matmul(x, weights['h1']) + biases['b1'] #w*x + b
      layer1 = sigmoid(layer1, False)#sigmoid activation
  
      output = tf.add(tf.matmul(layer1, weights['ol']), biases['ol']) #w*x + b
      output = sigmoid(output, False)#sigmoid activation
      return output
  
  def optimizer(out):
      return tf.trainable_variables()
  
  x = tf.placeholder('float', None, name='X_value_placeholder')
  
  pred = forward(x, weights, biases)
  
  opt = optimizer(pred)
  
  x_vals = np.array([[1.0, 0.0], [0.0,0.0]])
  # initialize variables
  init = tf.global_variables_initializer()
  
  # create session and run the graph
  with tf.Session() as sess:
      sess.run(init)
      for i in range(1):
          print 'epoch {}'.format(i)
          print (sess.run(opt, feed_dict={x: x_vals}))
  </code></pre>
  
  <p>From my understanding I need to do the following for optimizing the weights:</p>
  
  <p><strong>Deltas:</strong></p>
  
  <p><em>Hidden nodes (using layer 1 as an example):</em></p>
  
  <ul>
  <li><code>sigmoid(l1_sums, True) * ol_deltas * weights['h1']</code></li>
  </ul>
  
  <p><em>output node:</em></p>
  
  <ul>
  <li><code>-E * sigmoid(self.sums, True)</code></li>
  </ul>
  
  <p><strong>Gradiants:</strong></p>
  
  <ul>
  <li><code>l1_outputs * ol_deltas</code></li>
  </ul>
  
  <p><strong>Update Weights</strong></p>
  
  <ul>
  <li><code>l1_gradiants*learning_rate + momentum * l1_weight_deltas</code></li>
  </ul>
  
  <p><strong>TLDR:</strong></p>
  
  <p>To sum it up, how can optimize the weights (apply these functions) to the graph for a batch input as opposed to running the graph for every sample?</p>