Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. import random
  2. import time
  3. import math
  4. from decimal import Decimal
  5.  
  6. class Neural_network:
  7. """
  8. Recurent neural network
  9. """
  10. def __init__(self, adjacency_list):
  11. self.input = [0, 0.4, 0.8]
  12. self.neurons = {}
  13. self.expected_output = [0.9, 0, 0]
  14. self.output = []
  15.  
  16. self.active_neurons = []
  17. self.active_dendrites = []
  18. self.ep_neurons = [] # propagation of errors
  19.  
  20. self.time = 0
  21.  
  22. for idx, element in enumerate(adjacency_list):
  23. if element[1] not in self.neurons.keys():
  24. self.add_neuron(element[1])
  25. if element[0] not in self.neurons.keys():
  26. self.add_neuron(element[0])
  27.  
  28. self.neurons[element[1]].add_dendrite(element[0])
  29.  
  30. if element[0][0] =='I' and element[0] not in self.active_dendrites:
  31. self.active_dendrites.append(element[0])
  32.  
  33. self.active_neurons = [key for key,neuron in self.neurons.items() if 'I' in [layer[0] for layer in neuron.dendrites]]
  34. for idx, val in enumerate(self.input):
  35. self.neurons['I'+str(idx+1)].output = val
  36.  
  37. for idx, val in enumerate(self.expected_output):
  38. self.neurons['O'+str(idx+1)].expected_output = val
  39. self.ep_neurons.append('O'+str(idx+1))
  40.  
  41. for neuron in self.ep_neurons:
  42. for dendrite in self.neurons[neuron].dendrites:
  43. if dendrite not in self.ep_neurons:
  44. self.neurons[dendrite].calculate_expected_output(self)
  45. self.ep_neurons.append(dendrite)
  46.  
  47.  
  48.  
  49. def __str__(self):
  50. """
  51. Information about time of working, active_neurons and active_dendrites
  52. """
  53. return 'Time : '+str(self.time) +'\n'+\
  54. 'Active_neurons : '+' '.join(self.active_neurons) +'\n'+\
  55. 'Active_dendrites : '+' '.join(self.active_dendrites) +'\n'+\
  56. 'Output : '+', '.join([el[0]+' '+str(el[1]) for el in self.output]) +'\n'
  57.  
  58.  
  59. def add_neuron(self, index):
  60. """
  61. Add neuron to network
  62. """
  63. self.neurons[index] = Neuron(index)
  64.  
  65.  
  66. def tact(self):
  67. """
  68. Calculate one tact of signal passing and error propagation
  69. """
  70. self.time += 1
  71. for idx in self.active_neurons:
  72. self.neurons[idx].calculate_output(self)
  73.  
  74.  
  75. self.output = [[idx, self.neurons[idx].output] for idx in self.active_neurons if 'O' in idx]
  76. self.active_dendrites = [idx for idx in self.active_neurons if 'O' not in idx]
  77. self.active_neurons = [key for key,neuron in self.neurons.items() if bool(set(self.active_neurons) & set(neuron.dendrites))]
  78.  
  79.  
  80.  
  81.  
  82. class Neuron:
  83. def __init__(self, index):
  84. self.index = index
  85. self.bias = random.random()
  86. self.dendrites = []
  87. self.weights = []
  88. self.output = 0
  89. self.expected_output = 0
  90.  
  91.  
  92. def __str__(self):
  93. return 'Neuron : '+self.index +'\n'+\
  94. 'Dendrites : '+ ' '.join(self.dendrites) +'\n'+\
  95. 'Expected output : '+str(self.expected_output) +'\n'
  96.  
  97.  
  98. def add_dendrite(self, dendrite):
  99. self.dendrites.append(dendrite)
  100. self.weights.append(random.random())
  101.  
  102. def act_function(self, inp):
  103. return 1 / (1 + math.exp(-inp))
  104.  
  105. def calculate_output(self, NN):
  106. self.output = self.act_function( sum([self.weights[index] * NN.neurons[dendrite].output for index, dendrite in enumerate(self.dendrites) if dendrite in NN.active_dendrites]) + self.bias)
  107.  
  108.  
  109. def deact_function(self, inp):
  110. print(inp)
  111. print()
  112. # print(str(inp/(inp-1)))
  113. # tmp = Decimal(str(inp/(inp-1)))
  114. # # return tmp.ln()??????????????????????????????????????
  115. return 0.5
  116.  
  117. def calculate_input(self, axon):
  118. return (self.deact_function(axon.expected_output) - axon.bias) * \
  119. axon.weights[axon.dendrites.index(self.index)] / \
  120. sum([axon.weights[index] for index, dendrite in enumerate(axon.dendrites)])
  121.  
  122. def calculate_expected_output(self, NN):
  123. tmp = []
  124. for axon in [idx for idx in NN.ep_neurons if self.index in NN.neurons[idx].dendrites]:
  125. tmp.append(self.calculate_input(NN.neurons[axon]))
  126. self.expected_output = float(sum(tmp))/len(tmp) if len(tmp) > 0 else float('nan')
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. with open('aj.txt') as f:
  134. aj = [line.split(' ') for line in f.read().split('\n')]
  135. # print(aj[1])
  136.  
  137. NN1 = Neural_network(aj)
  138. while True:
  139. NN1.tact()
  140. print(NN1)
  141. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement