python - Gradient Descent -


so writing program handles gradient descent. im using method solve equations of form

ax = b random 10x10 matrix , b random 10x1 matrix 

here code:

import numpy np import math import random def steepestdistance(a,b,xo, e):     xprev = xo     dprev = -((a * xprev) - b)     magdprev = np.linalg.norm(dprev)     danger =  np.asscalar(((magdprev * magdprev)/(np.dot(dprev.t,a * dprev))))     xnext = xprev + (danger * dprev)     step = 1     while (np.linalg.norm((a * xnext) - b) >= e , np.linalg.norm((a * xnext) - b) < math.pow(10,4)):         xprev = xnext         dprev = -((a * xprev) - b)         magdprev = np.linalg.norm(dprev)         danger = np.asscalar((math.pow(magdprev,2))/(np.dot(dprev.t,a * dprev)))         xnext = xprev + (danger * dprev)         step = step + 1     return xnext  ##print(steepestdistance(np.matrix([[5,2],[2,1]]),np.matrix([[1],[1]]),np.matrix([[0.5],[0]]), math.pow(10,-5)))  def chooserandmatrix():     matrix = np.zeros(shape = (10,10))     in range(10):         in range(10):             matrix[i][a] = random.randint(0,100)     return matrix.t * matrix  def chooserandcolarray():     arra = np.zeros(shape = (10,1))     in range(10):         arra[i][0] = random.randint(0,100)     return arra in range(4):    matrix = np.asmatrix(chooserandmatrix())   array = np.asmatrix(chooserandcolarray())   print(steepestdistance(matrix, array, np.asmatrix(chooserandcolarray()),math.pow(10,-5))) 

when run method steepestdistance on random matrix , column, keep getting infinite loop. works fine when simple 2x2 matrices used a, loops indefinitely 10x10 matrices. problem in np.linalg.norm((a * xnext) - b); keeps growing indefinitely. thats why put upper bound on it; im not supposed algorithm however. can tell me problem is?

solving linear system ax=b gradient descent means minimize quadratic function

f(x) = 0.5*x^t*a*x - b^t*x.  

this works if matrix symmetric, a=a^t, since derivative or gradient of f

f'(x)^t = 0.5*(a+a^t)*x - b,  

and additionally must positive definite. if there negative eigenvalues,then descent proceed minus infinity, there no minimum found.


one work-around replace b a^tb , a^t*a, minimize function

f(x) = 0.5*||a*x-b||^2      = 0.5*x^t*a^t*a*x - b^t*a*x + 0.5*b^t*b 

with gradient

f'(x)^t = a^t*a*x - a^t*b 

but large matrices not recommended since condition number of a^t*a square of condition number of a.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -