Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- import math
- from decimal import Decimal
- class Neural_network:
- """
- Recurent neural network
- """
- def __init__(self, adjacency_list):
- self.input = [0, 0.4, 0.8]
- self.neurons = {}
- self.expected_output = [0.9, 0, 0]
- self.output = []
- self.active_neurons = []
- self.active_dendrites = []
- self.ep_neurons = [] # propagation of errors
- self.time = 0
- for idx, element in enumerate(adjacency_list):
- if element[1] not in self.neurons.keys():
- self.add_neuron(element[1])
- if element[0] not in self.neurons.keys():
- self.add_neuron(element[0])
- self.neurons[element[1]].add_dendrite(element[0])
- if element[0][0] =='I' and element[0] not in self.active_dendrites:
- self.active_dendrites.append(element[0])
- self.active_neurons = [key for key,neuron in self.neurons.items() if 'I' in [layer[0] for layer in neuron.dendrites]]
- for idx, val in enumerate(self.input):
- self.neurons['I'+str(idx+1)].output = val
- for idx, val in enumerate(self.expected_output):
- self.neurons['O'+str(idx+1)].expected_output = val
- self.ep_neurons.append('O'+str(idx+1))
- for neuron in self.ep_neurons:
- for dendrite in self.neurons[neuron].dendrites:
- if dendrite not in self.ep_neurons:
- self.neurons[dendrite].calculate_expected_output(self)
- self.ep_neurons.append(dendrite)
- def __str__(self):
- """
- Information about time of working, active_neurons and active_dendrites
- """
- return 'Time : '+str(self.time) +'\n'+\
- 'Active_neurons : '+' '.join(self.active_neurons) +'\n'+\
- 'Active_dendrites : '+' '.join(self.active_dendrites) +'\n'+\
- 'Output : '+', '.join([el[0]+' '+str(el[1]) for el in self.output]) +'\n'
- def add_neuron(self, index):
- """
- Add neuron to network
- """
- self.neurons[index] = Neuron(index)
- def tact(self):
- """
- Calculate one tact of signal passing and error propagation
- """
- self.time += 1
- for idx in self.active_neurons:
- self.neurons[idx].calculate_output(self)
- self.output = [[idx, self.neurons[idx].output] for idx in self.active_neurons if 'O' in idx]
- self.active_dendrites = [idx for idx in self.active_neurons if 'O' not in idx]
- self.active_neurons = [key for key,neuron in self.neurons.items() if bool(set(self.active_neurons) & set(neuron.dendrites))]
- class Neuron:
- def __init__(self, index):
- self.index = index
- self.bias = random.random()
- self.dendrites = []
- self.weights = []
- self.output = 0
- self.expected_output = 0
- def __str__(self):
- return 'Neuron : '+self.index +'\n'+\
- 'Dendrites : '+ ' '.join(self.dendrites) +'\n'+\
- 'Expected output : '+str(self.expected_output) +'\n'
- def add_dendrite(self, dendrite):
- self.dendrites.append(dendrite)
- self.weights.append(random.random())
- def act_function(self, inp):
- return 1 / (1 + math.exp(-inp))
- def calculate_output(self, NN):
- 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)
- def deact_function(self, inp):
- print(inp)
- print()
- # print(str(inp/(inp-1)))
- # tmp = Decimal(str(inp/(inp-1)))
- # # return tmp.ln()??????????????????????????????????????
- return 0.5
- def calculate_input(self, axon):
- return (self.deact_function(axon.expected_output) - axon.bias) * \
- axon.weights[axon.dendrites.index(self.index)] / \
- sum([axon.weights[index] for index, dendrite in enumerate(axon.dendrites)])
- def calculate_expected_output(self, NN):
- tmp = []
- for axon in [idx for idx in NN.ep_neurons if self.index in NN.neurons[idx].dendrites]:
- tmp.append(self.calculate_input(NN.neurons[axon]))
- self.expected_output = float(sum(tmp))/len(tmp) if len(tmp) > 0 else float('nan')
- with open('aj.txt') as f:
- aj = [line.split(' ') for line in f.read().split('\n')]
- # print(aj[1])
- NN1 = Neural_network(aj)
- while True:
- NN1.tact()
- print(NN1)
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement