The First Neuromorphic Artifical. Nervous Net in Python

TagtalLabs
3 min readJun 8, 2024

--

The original implementatio is in Java with AspectJ extension.

https://www.researchgate.net/publication/331274514_Aspect-Oriented_a_Candidate_for_the_Biologically_Inspired_Programming_Paradigm_for_Neural_Networks_and_Evolvable_Software

To leverage the AI related Python libs, let’s reimplement it in Python.

To better simulate the behavior of AspectJ, we can further abstract the AONeuron class to make it similar to AspectJ’s Aspect class. We will create a base Aspect class that can be inherited by AONeuron, SensoryNeuron, and MotorNeuron.

Improved Aspect Class

class Aspect:

. def __init__(self):

. pass

. def pointcut(self, join_point):

. def decorator(func):

. def wrapper(*args, **kwargs):

. return func(*args, **kwargs)

. return wrapper

. return decorator

. def before_advice(self, join_point):

. def decorator(func):

. def wrapper(*args, **kwargs):

. self.before(join_point)

. return func(*args, **kwargs)

. return wrapper

. return decorator

. def after_advice(self, join_point):

. def decorator(func):

. def wrapper(*args, **kwargs):

. result = func(*args, **kwargs)

. self.after(join_point)

. return result

. return wrapper

. return decorator

. def around_advice(self, join_point):

. def decorator(func):

. def wrapper(*args, **kwargs):

. self.before(join_point)

. result = func(*args, **kwargs)

. self.after(join_point)

. return result

. return wrapper

. return decorator

. def before(self, join_point):

. pass

. def after(self, join_point):

. pass

Improved AONeuron Class

class AONeuron(Aspect):

. def __init__(self):

. super().__init__()

. self.transmitter_to_release = False

. self.transmitter_received = False

. def axon_pointcut(self):

. return self.pointcut(“axon”)

. def dendrite_pointcut(self):

. return self.pointcut(“dendrite”)

. def axon_advice(self):

. def decorator(func):

. def wrapper(*args, **kwargs):

. return self.transmitter_received

. return wrapper

. return decorator

. def dendrite_advice(self):

. def decorator(func):

. def wrapper(*args, **kwargs):

. result = func(*args, **kwargs)

. self.transmitter_received = result

. return result

. return wrapper

. return decorator

SensoryNeuron Class

class SensoryNeuron(AONeuron):

. def __init__(self):

. super().__init__()

MotorNeuron Class

class MotorNeuron(AONeuron):

. def __init__(self):

. super().__init__()

. def axon_advice(self):

. def decorator(func):

. def wrapper(*args, **kwargs):

. self.transmitter_received = self.receptor()

. if self.transmitter_received:

. self.transmitter_to_release = True

. else:

. self.transmitter_to_release = False

. return self.transmitter_to_release

. return wrapper

. return decorator

. def receptor(self):

. transmitter_received = False

. # Simulate receiving transmitter logic

. return transmitter_received

Effector Class

class Effector:

. def __init__(self):

. self.state = False

. self.transmitter = False

. def receptor(self):

. transmitter_received = False

. # Simulate receiving transmitter logic

. return transmitter_received

. def emit(self):

. self.transmitter = self.state

. return self.transmitter

. def contract(self):

. self.state = True

. # Logic to contract the muscle

. def relax(self):

. self.state = False

. # Logic to relax the muscle

. def go(self):

. if self.receptor():

. self.contract()

. else:

. self.relax()

Integrating Neurons with Effector

effector = Effector()

sensory_neuron = SensoryNeuron()

motor_neuron = MotorNeuron()

# Apply pointcuts and advices to the effector’s methods

effector_emit = sensory_neuron.dendrite_pointcut()(sensory_neuron.dendrite_advice()(effector.emit))

effector_go = motor_neuron.axon_pointcut()(motor_neuron.axon_advice()(effector.go))

# Simulate the network

effector.state = True. # Initial state

effector.transmitter = effector_emit()

print(f”Effector Transmitter: {effector.transmitter}”)

effector_go()

print(f”Effector State: {effector.state}”)

Explanation

Aspect Class: Now includes before, after, and around advice methods that can be specifically implemented in subclasses.

AONeuron Class: Inherits from the Aspect class and defines neuron-specific pointcuts and advices.

SensoryNeuron and MotorNeuron Classes: Inherit from AONeuron and use or override the advice methods as needed.

Effector Class: Contains methods to simulate muscle cell behavior.

Integration: Pointcuts and advices are applied to the effector’s methods to simulate the neural network behavior.

With these improvements, the Aspect class is closer to AspectJ’s structure and provides better extensibility and code organization.

--

--