# scipy.optimize.minimize cost function of neural networks fails

I have written the cost function of a neural network for image classification problem. To optimize my cost function i have used scipy.optimize.minimize with solver ='TNC' though their isn't any run time error but after complication of optimization, result says success = 'false'

Here is my cost function

`````` def costandGrad(nn_param,X,Y_matrix,lam,i,j):

Theta1,Theta2 = reshape(nn_param,i,j)
Theta1 = np.matrix(Theta1)
Theta2 = np.matrix(Theta2)

#part1 - IMPLEMENTING FEEDFORWARD PROPOGATION ALGORTHIM

a1 = X
Z2 = X*np.transpose(Theta1)
a2 = sigmoid(Z2)

a2 = np.hstack((np.ones([len(a2),1]),a2))

Z3 = a2*np.transpose(Theta2)
a3 = sigmoid(Z3)
Hyp = a3

#PART2 - IMPLEMENTING COST WITH REGULARISATION

first_term = np.trace((np.transpose(-Y_matrix))* (np.log(Hyp)));

second_term = np.trace(((1-(np.transpose(Y_matrix)))*(np.log(1-Hyp))));

JU = first_term + second_term
sq1 = (np.square(Theta1[:,1:]))
sq2 = (np.square(Theta2[:,1:]))
regularization = (lam/(2*m)) * (sum(np.transpose(sum(sq1))) + sum(np.transpose(sum(sq2))))
J = JU  + regularization

#PART3 - BACKPROPOGATION ALGORTHIM
D3 = Hyp - Y_matrix
D2 = D3*Theta2[:,1:]

Delta1 = np.transpose(D2)*X;
Delta2 = np.transpose(D3)*a2;

nn_parameters =  np.concatenate((np.array(Theta1).ravel(), np.array(Theta2).ravel()))

return J,nn_parameters
``````

here is the result

`````` fim = sc.optimize.minimize(fun=costandGrad, x0=nn_params, args=(X_train, Y_matrix, 3,i,j), method='TNC', jac=True, options={'maxiter': 2000})

fun: matrix([[-227085.02475254]])
jac: array([ 0.08245473, -0.00159231,  0.47998975, ...,  1.12524447,
-1.30664152,  1.14908335])
message: 'Linear search failed'
nfev: 50
nit: 0
status: 4
success: False
x: array([ 0.08245473, -0.00159231,  0.47998975, ...,  1.12524447,
-1.30664152,  1.14908335])enter code here
``````