title: Invalid value encountered in subtract - Softmax, Python
body: <p>I'm using a numerically stabilised version of softmax as:-</p>
  
  <pre><code>def softmax(arr):
      print(arr)
      expArr=np.exp(arr-np.max(arr))
      print(expArr)
      return expArr/np.sum(expArr)
  </code></pre>
  
  <p>It is being used as:-</p>
  
  <pre><code>def feedforward(x_i,W):
      ...
      outputLayer = softmax(np.dot(network[-1],W[-1]))
      ...
  </code></pre>
  
  <p>And this function is called iteratively:-</p>
  
  <pre><code>for j in range(len(x)):
      ....
      network = feedforward(x[j],weights)
      ....
  </code></pre>
  
  <p>Still, for some sequence of arrays, I get the warning:- </p>
  
  <pre><code>RuntimeWarning: invalid value encountered in subtract
    expArr=np.exp(arr-np.max(arr))
  </code></pre>
  
  <p>The inputs (and outputs) going to the function right before the warning are:-</p>
  
  <pre><code>input
  [-1.36678160e+211 -1.97916134e+206 -5.44472726e+204 -5.47948095e+276
   -6.30134248e+251 -4.04707279e+210  7.72371508e+204  1.34861349e+268
    5.47948093e+276  1.06699784e+206]
  output
  [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
  input
  [-7.06701455e+257  1.47067222e+250              inf             -inf
   -1.13669521e+298 -6.54589076e+254  8.22221348e+250              inf
               -inf -5.44761594e+251]
  digit.py:22: RuntimeWarning: invalid value encountered in subtract
    expArr=np.exp(arr-np.max(arr))
  output
  [ 0.  0. nan  0.  0.  0.  0. nan  0.  0.]
  input
  [nan nan nan nan nan nan nan nan nan nan]
  output
  [nan nan nan nan nan nan nan nan nan nan]
  </code></pre>
  
  <p>I want to know that even though I stabilised the softmax function by introducing the <code>np.max(arr)</code> term, why am I still getting this error, and how can I fix it? Thanks!</p>
  
  <p>Also, I also used the softmax function given in <code>scipy.special</code>, but still ended up with the same warning.</p>