Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def obj_gradA(w,x,y):
- grad = []
- yh = yhat(x,w)
- for j in range(len(w)):
- tmp = 0
- for i in range(len(x)):
- tmp = tmp+yh[i]*(y[i]-yh[i])*(1-yh[i])*x[i,j]
- grad.append(-2*tmp)
- return np.array(grad)
- def obj_gradB(w,x,y):
- grad = []
- yh = yhat(x,w)
- tmp = yh*(y-yh)*(1-yh)
- for j in range(len(w)):
- grad.append(-2*np.sum(tmp*x[:,j]))
- return np.array(grad)
- def obj_gradC(w,x,y):
- yh = yhat(x,w)
- return -2*np.dot(yh*(y-yh)*(1-yh,x))
- from scipy.optimize import minimize
- w_start = np.random.rand(*w_exact.shape)
- w_found = minimize(obj, w_start, method = 'Nelder-Mead',args=(x_rand,y))
- print(w_found)
- print('exact val', w_exact)
- final_simplex: (array([[ 1806.07992755, -358.33266465],
- [ 1806.07999633, -358.33267886],
- [ 1806.07999898, -358.33267909]]), array([-6.67715058, -6.67715058, -6.67715058]))
- fun: -6.6771505843807075
- message: 'Optimization terminated successfully.'
- nfev: 195
- nit: 78
- status: 0
- success: True
- x: array([ 1806.07992755, -358.33266465])
- exact val [5 3]
- C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\__main__.py:2: RuntimeWarning: overflow encountered in exp
- from ipykernel import kernelapp as app
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement