Gradient Based Optimization Code Python Love