Advertisement
Guest User

Network

a guest
Jun 15th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. import numpy as np
  2. import Layer
  3. import math
  4.  
  5. learningRate = 0.05
  6. momentum = 0.9
  7.  
  8.  
  9. class Network:
  10. def __init__(self, layersArray):
  11. self.layers = layersArray
  12.  
  13. def activationFun(self, values, function):
  14. x, y = values.shape
  15. result = np.asmatrix(np.zeros((x, y), dtype=float))
  16. if function == 'lin':
  17. for i in range(x):
  18. for j in range(y):
  19. result[i, j] = values[i, j]
  20. return result
  21. elif function == 'sig':
  22. for i in range(x):
  23. for j in range(y):
  24. result[i, j] = (1.0 / (1.0 + np.exp(-values[i, j])))
  25. return result
  26.  
  27. def activationFunDerivative(self, values, function):
  28. x, y = values.shape
  29. result = np.asmatrix(np.zeros((x, y), dtype=float))
  30. if function == 'lin':
  31. for i in range(x):
  32. for j in range(y):
  33. result[i, j] = 1
  34. return result
  35. elif function == 'sig':
  36. temp = self.activationFun(values, 'sig')
  37. for i in range(x):
  38. for j in range(y):
  39. result[i, j] = temp[i, j] * (1.0 - temp[i, j])
  40. return result
  41.  
  42. def forwardPropagate(self, inputMatrix):
  43. self.layers[0].input = inputMatrix
  44.  
  45. if isinstance(self.layers[0], Layer.affineLayer):
  46. if (self.layers[0].isBias):
  47. self.layers[0].z = self.layers[0].weights * self.layers[0].input + self.layers[0].bias
  48. else:
  49. self.layers[0].z = self.layers[0].weights * self.layers[0].input
  50.  
  51. self.layers[0].output = self.activationFun(self.layers[0].z, self.layers[0].activator)
  52. else:
  53. centroids = self.layers[0].centroids
  54. widths = self.layers[0].widths
  55. self.layers[0].output = np.asmatrix(np.zeros((centroids.shape[0], 1), dtype=float))
  56.  
  57. for counter, centroid in enumerate(centroids):
  58. sum = 0
  59. for i in range(centroid.shape[1]):
  60. sum += pow(centroid[0, i] - inputMatrix[i, 0], 2)
  61. self.layers[0].output[counter, 0] = math.exp(-widths[counter, 0] * sum)
  62.  
  63. for i in range(1, self.layers.size):
  64. self.layers[i].input = self.layers[i - 1].output
  65.  
  66. if isinstance(self.layers[i], Layer.affineLayer):
  67. if (self.layers[i].isBias):
  68. self.layers[i].z = self.layers[i].weights * self.layers[i].input + self.layers[i].bias
  69.  
  70. else:
  71. self.layers[i].z = self.layers[i].weights * self.layers[i].input
  72.  
  73. self.layers[i].output = self.activationFun(self.layers[i].z, self.layers[i].activator)
  74. else:
  75. centroids = self.layers[i].centroids
  76. widths = self.layers[i].widths
  77. self.layers[i].output = np.asmatrix(np.zeros((centroids.shape[0], 1), dtype=float))
  78.  
  79. for counter, centroid in enumerate(centroids):
  80. sum = 0
  81. for i in range(centroid.shape[1]):
  82. sum += pow(centroid[0, i] - inputMatrix[i, 0], 2)
  83. self.layers[0].output[counter, 0] = math.exp(-widths[counter, 0] * sum)
  84.  
  85. def computeErrors(self, outputMatrix):
  86. self.layers[-1].error = outputMatrix - self.layers[-1].output
  87. for i in reversed(range(self.layers.size - 1)):
  88. if isinstance(self.layers[i], Layer.affineLayer):
  89. self.layers[i].error = self.layers[i + 1].weights.T * self.layers[i + 1].error
  90. else:
  91. continue
  92.  
  93. def modifyWeights(self):
  94. for i in range(self.layers.size):
  95. if isinstance(self.layers[i], Layer.affineLayer):
  96. # WAGI NEURONOW
  97. self.layers[i].weightsChanges = learningRate * np.multiply(
  98. self.activationFunDerivative(self.layers[i].output, self.layers[i].activator),
  99. self.layers[i].error) * self.layers[i].input.T + momentum * self.layers[i].weightsChanges
  100.  
  101. self.layers[i].weights = self.layers[i].weights + self.layers[i].weightsChanges
  102.  
  103. # WAGI BIASU
  104. if (self.layers[i].isBias):
  105. self.layers[i].biasChanges = learningRate * np.multiply(
  106. self.activationFunDerivative(self.layers[i].output, self.layers[i].activator),
  107. self.layers[i].error) + momentum * self.layers[i].biasChanges
  108.  
  109. self.layers[i].bias = self.layers[i].bias + self.layers[i].biasChanges
  110. else:
  111. continue
  112.  
  113. def simulate(self, inputMatrix, outputMatrix):
  114. self.forwardPropagate(inputMatrix)
  115. self.computeErrors(outputMatrix)
  116. self.modifyWeights()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement