Guest User

Untitled

a guest
Jul 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. import random
  2. import math
  3.  
  4.  
  5. class ForwardPropagation:
  6. def __init__(self):
  7. # Inputs
  8. self.input1 = 0.05
  9. self.input2 = 0.10
  10.  
  11. # Outputs
  12. self.output1 = 0.80
  13. self.output2 = 0.99
  14.  
  15. # bias
  16. self.bias1 = 0.35
  17. self.bias2 = 0.60
  18.  
  19. # self.weight = list(range(9))
  20. self.weight = [.15, .20, .25, .30, .40, .45, .50, .55]
  21.  
  22. # Hidden Layer
  23. self.hidden1 = 0.0
  24. self.hidden2 = 0.0
  25.  
  26. self.tempo1 = 0.0
  27. self.tempo2 = 0.0
  28.  
  29. self.error1 = 0.0
  30. self.error2 = 0.0
  31.  
  32. self.input1Lowest = 0.0
  33. self.input2Lowest = 0.0
  34.  
  35. self.lowestError = 1
  36. self.randomValue = random.random()
  37.  
  38. def calculateNet(self, i1, i2, w1, w2, b):
  39. return (w1*i1)+(w2*i2)+(b*1)
  40.  
  41. def calculateOutput(self, val):
  42. return (1/(1+math.exp(-val)))
  43.  
  44. def calculateError(self, targetO, output):
  45. return (0.5 * ((targetO - output) * (targetO - output)))
  46.  
  47. def forwardPropagation(self):
  48. # self.inputEdgeWeight()
  49.  
  50. for i in range(100):
  51. self.forwardPassPropagation()
  52.  
  53. print("for the value of i1 & i2 :")
  54. print(str(self.input1Lowest) + "," + str(self.input2Lowest))
  55.  
  56. def forwardPassPropagation(self):
  57. self.input1 = random.random()
  58. self.input2 = random.random()
  59. print("input1: " + str(self.input1) + "input2: " + str(self.input2))
  60. self.hidden1 = self.calculateNet(self.input1, self.input2, self.weight[0], self.weight[1], self.bias1)
  61. self.hidden1 = self.calculateOutput(self.hidden1)
  62. self.hidden2 = self.calculateNet(self.input1, self.input2, self.weight[2], self.weight[3], self.bias1)
  63. self.hidden2 = self.calculateOutput(self.hidden2)
  64.  
  65. self.tempo1 = self.calculateNet(self.hidden1, self.hidden2, self.weight[4], self.weight[5], self.bias2)
  66. self.tempo1 = self.calculateOutput(self.tempo1)
  67. self.tempo2 = self.calculateNet(self.hidden1, self.hidden2, self.weight[6], self.weight[7], self.bias2)
  68. self.tempo2 = self.calculateOutput(self.tempo2)
  69.  
  70. # error
  71. self.error1 = self.calculateError(self.output1, self.tempo1)
  72. print("error1: " + str(self.error1))
  73. self.error2 = self.calculateError(self.output2, self.tempo2)
  74. print("error2: " + str(self.error2))
  75.  
  76. self.Error = self.error1 + self.error2
  77.  
  78. if self.Error < self.lowestError:
  79. self.input1Lowest = self.input1
  80. self.input2Lowest = self.input2
  81.  
  82. print("Total error: " + str(self.error1 + self.error2))
  83.  
  84.  
  85. demo = ForwardPropagation()
  86. demo.forwardPropagation()
Add Comment
Please, Sign In to add comment