Advertisement
Guest User

Untitled

a guest
May 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from numpy import linalg as la
  4. from scipy.sparse import spdiags
  5. import scipy.io as sio
  6.  
  7.  
  8. # EX1 Part.A
  9.  
  10. def sigmoid(X,W):
  11. return 1 / (1 + np.exp(-1 * np.matmul(X.T, W)))
  12.  
  13. def cost(X,C,W):
  14. return (-1/len(X)) * (np.dot(C.T, np.log(sigmoid(X, W))) + np.dot((1-C).T, np.log(1-sigmoid(X, W))))
  15.  
  16. def gradient(X,C,W):
  17. ans = 1/X.shape[1] * np.dot(X ,(sigmoid(X,W) - C))
  18. return ans/np.linalg.norm(ans)
  19.  
  20. def hessian(X,C,W):
  21. D = np.diag((sigmoid(X,W) * (1-sigmoid(X,W))).reshape(-1))
  22. return 1/X.shape[1] * np.matmul(X, np.matmul(D,X.T))
  23.  
  24. # part 2 - tests
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement