code: def flatten_grad(func):
      r
      @wraps(func)
      def new_func(*args, **kwargs):
          return flatten(func(*args, **kwargs), returns_shapes=False)
  
      return new_func
docs: r"""
      Decorator to flatten structured gradients.
  
      Examples
      --------
      >>> def cost(w, lambda_):
      ...     sq_norm = w.T.dot(w)
      ...     return lambda_ * w, .5 * sq_norm
      >>> grad = cost(np.array([.5, .1, -.2]), .25)
  
      >>> len(grad)
      2
      >>> grad_w, grad_lambda = grad
      >>> np.shape(grad_w)
      (3,)
      >>> np.shape(grad_lambda)
      ()
      >>> grad_w
      array([ 0.125,  0.025, -0.05 ])
      >>> np.isclose(grad_lambda, 0.15)
      True
  
      >>> cost_new = flatten_grad(cost)
      >>> grad_new = cost_new(np.array([.5, .1, -.2]), .25)
      >>> grad_new
      array([ 0.125,  0.025, -0.05 ,  0.15 ])