Guest User

Untitled

a guest
Jan 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. import math
  2.  
  3. class MLP():
  4. learning_rate = 0.1
  5. epoch = 2000
  6. display = 100
  7. w11=0.5;w12=0.9;w21=0.4;w22=1.0;w31=-1.2;w32=1.1;b1=-1;b2=-1;b3=-1
  8.  
  9. def train(self):
  10. step = 1
  11. while(step < self.epoch):
  12. for i in range(0,4):
  13. result= self.train_net(int(i/2),i%2)
  14. if step % self.display==0:
  15. print("epoch:",step,"input1:",int(i/2),"input2:",i%2,"output:",result)
  16. step+=1
  17.  
  18. def train_net(self,input1,input2):
  19. net1 = self.w11*input1+self.w21*input2+self.b1
  20. net2 = self.w12*input1+self.w22*input2+self.b2
  21. y1 = self.sigmoid(net1)
  22. y2 = self.sigmoid(net2)
  23. net3 = y1*self.w31+y2*self.w32+self.b3
  24. y3 = self.sigmoid(net3)
  25. target = int(input1) & int(input2)
  26. deviation3 = y3*(1-y3)*(target-y3)
  27. deviation1 = self.w31*deviation3*y1*(1-y1)
  28. deviation2 = self.w32*deviation3*y2*(1-y2)
  29. self.w11 += self.learning_rate*deviation1*input1
  30. self.w12 += self.learning_rate*deviation2*input2
  31. self.w21 += self.learning_rate*deviation1*input1
  32. self.w22 += self.learning_rate*deviation2*input2
  33. self.w31 += self.learning_rate*deviation3*y1
  34. self.w32 += self.learning_rate*deviation3*y2
  35. self.b1 += self.learning_rate*deviation1
  36. self.b2 += self.learning_rate*deviation2
  37. self.b3 += self.learning_rate*deviation3
  38.  
  39. # print("input1 :"+str(input1)+" input2 :"+str(input2))
  40. # print("output :"+str(y3))
  41. #print("w11:"+str(self.w11))
  42. #print("w12:"+str(self.w12))
  43. #print("w21:"+str(self.w21))
  44. #print("w22:"+str(self.w22))
  45. #print("w31:"+str(self.w31))
  46. #print("w32:"+str(self.w32))
  47. #print("b1:"+str(self.b1))
  48. #print("b2:"+str(self.b2))
  49. #print("b3:"+str(self.b3))
  50. return y3
  51.  
  52. def sigmoid(self, net):
  53. return (1/(1+math.exp(-net)))
  54.  
  55. if __name__== '__main__':
  56. mlp = MLP()
  57. mlp.train()
Add Comment
Please, Sign In to add comment