python - Scipy optimize function: matrixes not aligned -
edit: data set mnist data set homework of week 4 of andrew ng's machine learning course
i've checked question on scipy optimize still couldn't figure out wrong code. trying optimize theta onevsall question on andrew ng coursera course.
here relevant code
def sigmoid(x): = [] item in x: a.append(1/(1+math.exp(-item))) return def hypothesis(x, theta): return np.array(sigmoid(np.dot(x, theta))) def costfunction(theta, x, y, lamba_): m = x.shape[0] part1 = np.dot(y.t, np.log(hypothesis(x, theta)).reshape(m,1)) part2 = np.dot((np.ones((m,1)) - y).t, np.log( 1 - hypothesis(x, theta)).reshape(m,1)) summ = (part1 + part2) return -summ[0]/m def gradientvect(theta, x, y, lambda_): n = x.shape[1] m = x.shape[0] gradient = [] theta = theta.reshape(n,1) beta = hypothesis(x, theta) - y reg = theta[1:] * lambda_/m grad = np.dot(x.t, beta) * 1./m grad[1:] = grad[1:] * reg return grad.flatten() scipy import optimize def optimizetheta(x, y, nlabels, lambda_): in np.arange(0, nlabels): theta = np.zeros((n,1)) res = optimize.minimize(costfunction, theta, args=(x, (y == i)*1, lambda_), method=none, jac=gradientvect, options={'maxiter':50}) print(res) return result
but running
optimizetheta(x, y, 10, 0) # x shape = 401, 500
gives me following error:
--------------------------------------------------------------------------- valueerror traceback (most recent call last) <ipython-input-247-e0e6e4c1eddd> in <module>() 3 n = x.shape[1] 4 ----> 5 optimizetheta(x, y, 10, 0) <ipython-input-246-0a15e9f4769a> in optimizetheta(x, y, nlabels, lambda_) 54 theta = np.zeros((n,1)) 55 res = optimize.minimize(costfunction, x0 = theta, args=(x, (y == i)*1, lambda_), method=none, ---> 56 jac=gradientvect, options={'maxiter':50}) 57 print(res) 58 return result //anaconda/lib/python3.5/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options) 439 return _minimize_cg(fun, x0, args, jac, callback, **options) 440 elif meth == 'bfgs': --> 441 return _minimize_bfgs(fun, x0, args, jac, callback, **options) 442 elif meth == 'newton-cg': 443 return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback, //anaconda/lib/python3.5/site-packages/scipy/optimize/optimize.py in _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options) 859 gnorm = vecnorm(gfk, ord=norm) 860 while (gnorm > gtol) , (k < maxiter): --> 861 pk = -numpy.dot(hk, gfk) 862 try: 863 alpha_k, fc, gc, old_fval, old_old_fval, gfkp1 = \ valueerror: shapes (401,401) , (2005000,) not aligned: 401 (dim 1) != 2005000 (dim 0)
and can't figure out why shapes not aligned.
thanks!
so realized wrong question. problem sigmoid function returning list , not integer , therefore messed matrixes multiplications afterwards. new sigmoid function is
def sigmoid(z): return(1 / (1 + np.exp(-z)))
Comments
Post a Comment