title: Using squared difference of two images as loss function in tensorflow
body: <p>I'm trying to use the SSD between two images as loss function for my network.</p>
  
  <pre class="lang-py prettyprint-override"><code># h_fc2 is my output layer, y_ is my label image.
  ssd = tf.reduce_sum(tf.square(y_ - h_fc2))
  
  train_step = tf.train.GradientDescentOptimizer(0.01).minimize(ssd)
  </code></pre>
  
  <p>Problem is, that the weights then diverge and I get the error</p>
  
  <pre class="lang-py prettyprint-override"><code> ReluGrad input is not finite. : Tensor had Inf values
  </code></pre>
  
  <p>Why's that? I did try some other stuff like normalizing the ssd by the image size (did not work) or cropping the output values to 1 (does not crash anymore, but I still need to evaluate this):</p>
  
  <pre class="lang-py prettyprint-override"><code>ssd_min_1 = tf.reduce_sum(tf.square(y_ - tf.minimum(h_fc2, 1)))
  train_step = tf.train.GradientDescentOptimizer(0.01).minimize(ssd_min_1)
  </code></pre>
  
  <p>Are my observations to be expected?</p>
  
  <p>Edit:
  @mdaoust suggestions proved to be correct. The main point was normalizing by batch size. This can be done independent of batch size by using this code</p>
  
  <pre class="lang-py prettyprint-override"><code>squared_diff_image = tf.square(label_image - output_img)
  # Sum over all dimensions except the first (the batch-dimension).
  ssd_images = tf.reduce_sum(squared_diff_image, [1, 2, 3])
  # Take mean ssd over batch.
  error_images = tf.reduce_mean(ssd_images)
  </code></pre>
  
  <p>With this change, only a slight decrease of the learning rate (to 0.0001) was necessary.</p>