human
I am working on the file "ML-Logistic-Regression-Multinomial.ipynb".
The first task is:
<hr />
Find optimal parameters for the 10 models
This is the main training loop. All 10 models are optimized using the columns of $Y$ and the training data set. Each model is fit to it's number (0-9) by evaluation it's cost function against all of the other numbers "the rest".
You can see that some of the models required many more iterations before convergence. There was also some numerical overflow present. I'm not too concerned about this since it is an artifact of the optimization run. The models converged OK and gave reasonably good set of parameters for each of the 10 models. It is possible to work on each model separately to try to get better fits and the regularization term could be adjusted per model. I did play with the optimization somewhat but wont worry about it too much since in teh next post I'll be doing an implementation of "Stochastic Gradient Descent" and will likely use this data again as an example.
Can you write Python code for it?

Give me a Python code to find optimal parameters for the 10 models?
assistant
#Here is Python code to find optimal parameters for the 10 models
.

reg =300.0 # Regularization term
np.random.seed(42)
aguess = np.random.randn(X_train.shape[1]) # A random guess for the parameters 
A_opt = np.zeros((X_train.shape[1],10)) # The matrix of optimized parameters 
Res=[] # List to hold the full optimizitaion output of each model

for i in range(10):
    print('\nFitting {} against the rest\n'.format(i))
    def opt_J_reg(a) :
        return J(X_train,a,Y_train[:,i])
    def opt_gradJ_reg(a) :
        return gradJ_reg(X_train,a,Y_train[:,i],reg)
    
    res = minimize(opt_J_reg, aguess, method='CG', jac=opt_gradJ_reg, tol=1e-6, options={'disp': True})
    Res.append(res)
    A_opt[:,i] = res.x