48 lines
1011 B
Python
48 lines
1011 B
Python
|
|
# This is from wikipedia pseudocode implementation of a single
|
|
# ThresholdLogic Unit.
|
|
# done to make me understand OOP the Python way
|
|
|
|
|
|
# no need for brackets if not inheriting
|
|
class ThresholdLogicUnit:
|
|
|
|
# define members in init
|
|
def __init__(self, threshold, weights):
|
|
self.threshold = threshold
|
|
self.weights = weights
|
|
|
|
|
|
# If a function has to make use of member variables
|
|
# it has to have self as param
|
|
def fire(self,inputs):
|
|
tots = 0
|
|
#for i in range(0,inputs.size()):
|
|
for val, weight in zip(inputs, self.weights):
|
|
if val:
|
|
tots+= weight
|
|
|
|
return tots > self.threshold
|
|
|
|
|
|
def main():
|
|
# data
|
|
weights = [0.5, -0.2, 0.8]
|
|
threshold = 1.0
|
|
|
|
# Instantiate the class
|
|
tlu = ThresholdLogicUnit(threshold, weights)
|
|
|
|
# Test
|
|
test_inputs = [1, 1, 0]
|
|
result = tlu.fire(test_inputs)
|
|
|
|
print(f"The unit fired: {result}")
|
|
|
|
# The "Guard"
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
|
|
|
|
|