Guest User

Untitled

a guest
Nov 16th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import numpy as np
  2.  
  3. class NeuralNet(object):
  4. '''
  5. Neural Network with tanh activation and softmax output function using Adam Optimizer.
  6.  
  7. Parameters
  8. ----------
  9. wShape : tuple
  10. Shape of feedforward neural network architecture.
  11.  
  12. Usage: nn = NeuralNet(ni, nhi, no)
  13. ni: Number of input features.
  14. nhi: Number of neurons in ith hidden layer.
  15. no: Number of outputs.
  16. '''
  17.  
  18. def __init__(self, wShape):
  19. super().__init__()
  20. self.wShape = wShape
  21. self.param = {
  22. 'W':[np.random.randn(self.wShape[i+1], self.wShape[i]) for i in range(len(self.wShape)-1)],
  23. 'B':[np.zeros((self.wShape[i+1], 1)) for i in range(len(wShape)-1)]
  24. }
  25.  
  26. def softmax(self, x):
  27. x = np.exp(x - np.max(x, axis=0))
  28. x /= np.sum(x, axis=0)
  29. return x
  30.  
  31. def forwardPass(self, X):
  32. A, Z = [X], []
  33. for i in range(len(self.wShape)-1):
  34. Z.append(np.add(np.matmul(self.param['W'][i], A[-1]), self.param['B'][i]))
  35. A.append((self.softmax(Z[-1]) if i==len(self.wShape)-2 else np.tanh(Z[-1])))
  36.  
  37. Y_hat = A[-1]; del A[0];
  38. cache = {
  39. 'Z': Z,
  40. 'A': A
  41. }
  42. return Y_hat, cache
  43.  
  44. def crossEntropyLoss(self, Y_hat, Y, esp=1e-15):
  45. return -np.mean(np.log(np.clip(Y_hat[Y], esp, 1-esp)))
  46.  
  47. def predictLabel(self, Y_hat):
  48. return np.argmax(Y_hat, axis=0)
  49.  
  50. def backwardPass(self, cache, X, Y):
  51. dZ, dW, dB = [cache['A'][-1] - Y], [], []
  52. for i in range(len(self.wShape)-3, -1, -1):
  53. dZ.insert(0, np.matmul(np.transpose(self.param['W'][i+1]), dZ[0]) * (1 - np.power(cache['A'][i], 2)))
  54. dW.insert(0, np.matmul(dZ[1], np.transpose(cache['A'][i]))); dB.insert(0, np.sum(dZ[1], axis=1, keepdims=True))
  55. dW.insert(0, np.matmul(dZ[0], np.transpose(X))); dB.insert(0, np.sum(dZ[0], axis=1, keepdims=True))
  56.  
  57. grads = {
  58. 'dW': dW,
  59. 'dB': dB
  60. }
  61. return grads
  62.  
  63. def updateParam(self, X, Y, itrr=100, alpha=1e-2, beta1=0.9, beta2=0.999, espp=1e-8):
  64. '''
  65. Adam Optimizer:-
  66. Vdw = beta1*Vdw + (1-beta1)*dW; VdB = beta1*VdB + (1-beta1)*dB
  67. Sdw = beta2*Sdw + (1-beta2)*dW**2; SdB = beta2*SdB + (1-beta2)*dB**2
  68. Vdw, VdB /= (1 - beta1**(itteration)); Sdw, SdB /= (1 - beta2**(itteration))
  69. W -= alpha * Vdw/(sqrt(Sdw) + esp); B -= alpha * VdB/(sqrt(SdB) + esp)
  70. '''
  71.  
  72. Vdw = [0. for i in range(len(self.wShape)-1)]
  73. VdB = [0. for i in range(len(self.wShape)-1)]
  74. Sdw = [0. for i in range(len(self.wShape)-1)]
  75. SdB = [0. for i in range(len(self.wShape)-1)]
  76.  
  77. for itteration in range(1, itrr+1):
  78. Y_hat, cache = self.forwardPass(X)
  79. grads = self.backwardPass(cache, X, Y)
  80. for layer in range(len(self.wShape)-1):
  81. Vdw[layer] = (beta1*Vdw[layer] + (1-beta1)*grads['dW'][layer])/(1-beta1**itteration)
  82. VdB[layer] = (beta1*VdB[layer] + (1-beta1)*grads['dB'][layer])/(1-beta1**itteration)
  83. Sdw[layer] = (beta2*Sdw[layer] + (1-beta2)*np.power(grads['dW'][layer], 2))/(1-beta2**itteration)
  84. SdB[layer] = (beta2*SdB[layer] + (1-beta2)*np.power(grads['dB'][layer], 2))/(1-beta2**itteration)
  85. self.param['W'][layer] -= alpha * Vdw[layer]/(np.sqrt(Sdw[layer])+espp)
  86. self.param['B'][layer] -= alpha * VdB[layer]/(np.sqrt(SdB[layer])+espp)
  87.  
  88. def accuracy(self, y_hat, y):
  89. return np.mean(y_hat==y)*100
Add Comment
Please, Sign In to add comment