I am doing some hobby coding regarding neural networks, and I was wondering if this is a correct and good use of abstract base classes or not.
import abc
import collections
import math
import weakref
class AbstractNeuron(metaclass=abc.ABCMeta):
def __init__(self, weights, f=math.tanh, bias=1, biasWeight=0):
self.weights = weights
self.f = f
self.bias = bias
self.biasWeight = biasWeight
def __call__(self, inputs):
return self.activate(inputs)
@abc.abstractmethod
def activate(self, inputs):
raise NotImplementedError
class MappingNeuron(AbstractNeuron):
def __init__(self, weights=None, f=math.tanh, bias=1, biasWeight=0):
if weights is None:
weights = collections.defaultdict(int)
super().__init__(weights, f, bias, biasWeight)
def activate(self, inputs):
return self.f(math.fsum(inputs[key] * self.weights[key] for key in inputs.keys()) + self.bias * self.biasWeight)
class WeakNeuron(MappingNeuron):
def __init__(self, weights=None, f=math.tanh, bias=1, biasWeight=0):
if weights is None:
weights = weakref.WeakKeyDictionary()
super().__init__(weights, f, bias, biasWeight)
class SequenceNeuron(AbstractNeuron):
def __init__(self, weights, f=math.tanh, bias=1, biasWeight=0):
super().__init__(weights, f, bias, biasWeight)
def activate(self, inputs):
return self.f(math.fsum(inputs[i] * weight for i, weight in enumerate(self.weights)) + self.bias * self.biasWeight)