collapse/collapse.py
2021-10-03 13:12:26 +02:00

40 lines
1.2 KiB
Python

from sympy import *
import math
class Model():
def __init__(self):
self.layers = []
def append(self, layer):
if len(self.layers)!=0:
layer._connect(self.layers[-1])
self.layers.append(layer)
def collapse(self):
act = symbols(' '.join(['x'+str(i) for i in range(self.layers[0].length)]))
for layer in self.layers[1:]:
act = layer.activate(act)
return list(act)
class Layer():
def __init__(self, length):
self.length = length
self.layNum = 0
def _connect(self, prev):
self.prev = prev
self.weights = []
self.layNum = self.prev.layNum + 1
for w in range(self.length * (self.prev.length+1)):
self.weights.append(symbols('w'+str(self.layNum)+'_'+str(w)))
def activate(self, inp):
act = []
for neuron in range(self.length):
accum = self.weights[(self.prev.length+1)*neuron]*self.prev.length
for iNeuron in range(self.prev.length):
accum += inp[iNeuron]*self.weights[(self.prev.length+1)*neuron+iNeuron+1]
a = 1/(1+math.e**(-(accum)/self.prev.length))
act.append(a)
return act