Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from numpy import linalg as la
- from scipy.sparse import spdiags
- import scipy.io as sio
- # EX1 Part.A
- def sigmoid(X,W):
- return 1 / (1 + np.exp(-1 * np.matmul(X.T, W)))
- def cost(X,C,W):
- return (-1/len(X)) * (np.dot(C.T, np.log(sigmoid(X, W))) + np.dot((1-C).T, np.log(1-sigmoid(X, W))))
- def gradient(X,C,W):
- ans = 1/X.shape[1] * np.dot(X ,(sigmoid(X,W) - C))
- return ans/np.linalg.norm(ans)
- def hessian(X,C,W):
- D = np.diag((sigmoid(X,W) * (1-sigmoid(X,W))).reshape(-1))
- return 1/X.shape[1] * np.matmul(X, np.matmul(D,X.T))
- # part 2 - tests
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement