title: Gradient Descent: AssertionError: shapes of matrix don't match
body: <p>I am stuck on assignment from Andrew Ng DL-NN course. 
  The code has a assertion when matrix gradient of the loss with respect to <code>w</code> ( <code>dw</code> ) must have the same shape of <code>w</code> ( <code>.shape == ( 2, 1 )</code> ) but in calculating <code>dw</code> I have to include the matrix of variable <code>X</code>, that has a shape of <code>(2,2)</code>, so by broadcasting the shape of <code>dw</code> will always be <code>(2,2)</code> and never the same as <code>w</code> <code>(2,1)</code>.</p>
  
  <p>Can someone help please?  </p>
  
  <p>Function</p>
  
  <pre><code>def propagate(w, b, X, Y):
      """
      Implement the cost function and its gradient for the propagation explained above
      Arguments:
      w -- weights, a numpy array of size (num_px * num_px * 3, 1)
      b -- bias, a scalar
      X -- data of size (num_px * num_px * 3, number of examples)
      Y -- true "label" vector (containing 0 if non-cat, 1 if cat) of size (1, number of examples)
      Return:
      cost -- negative log-likelihood cost for logistic regression
      dw -- gradient of the loss with respect to w, thus same shape as w
      db -- gradient of the loss with respect to b, thus same shape as b    
      """
      m = X.shape[1] 
  
      A = 1 / (1 + np.exp(-(np.dot(w.T, X) + b)))                           
      cost = -(1/m) *(np.dot(Y, np.log(A).T)) + (1 - Y) * np.log(1-A)             
      dz = A - Y
      dw = (1/m)*X*((dz.T))
      db = (1/m)*np.sum(dz)
  
      #print(X.shape)
      #print(X)
      #print(A.shape)
      #print(Y.shape)
      print(dw.shape)
      #print(dw)
      #print(w.shape)
  
      assert(dw.shape == w.shape)
      assert(db.dtype == float)
      cost = np.squeeze(cost)
      assert(cost.shape == ())
  
      grads = {"dw": dw,
               "db": db}
      return grads, cost
  </code></pre>
  
  <p>Call Function</p>
  
  <pre><code>w, b, X, Y = np.array([[1],[2]]), 2, np.array([[1,2],[3,4]]), 
  np.array([[1,0]])
  grads, cost = propagate(w, b, X, Y)
  print ("dw = " + str(grads["dw"]))
  print ("db = " + str(grads["db"]))
  print ("cost = " + str(cost))
  </code></pre>
  
  <p>Error message</p>
  
  <pre><code>---------------------------------------------------------------------------
  AssertionError                            Traceback (most recent call last)
  &lt;ipython-input-84-93098709c7a5&gt; in &lt;module&gt;()
        1 w, b, X, Y = np.array([[1],[2]]), 2, np.array([[1,2],[3,4]]), np.array([[1,0]])
  ----&gt; 2 grads, cost = propagate(w, b, X, Y)
        3 print ("dw = " + str(grads["dw"]))
        4 print ("db = " + str(grads["db"]))
        5 print ("cost = " + str(cost))
  
  &lt;ipython-input-82-e28b1a4b33cb&gt; in propagate(w, b, X, Y)
       45     ### END CODE HERE ###
       46 
  ---&gt; 47     assert(dw.shape == w.shape)
       48     assert(db.dtype == float)
       49     cost = np.squeeze(cost)
  
  AssertionError: 
  </code></pre>