add the norm of the gradient as output of the optimization of the cost...
add the norm of the gradient as output of the optimization of the cost function and improve the use of custom stopping criteria for the optimization using StopIteration exceptions