Skip to content
Closed
Prev Previous commit
Next Next commit
Update simple_adaline.py
  • Loading branch information
IBR-41379 authored Oct 10, 2023
commit 38a8a0b2de85f4a3801ddf541e69edbc9a986678
122 changes: 71 additions & 51 deletions neural_network/simple_adaline.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,86 @@
# This program implements the AND and OR gates using the Adaline algorithm.
def weight_change_or(
weight: list[float], threshold: float, learning_rate: float
) -> list[float]:
output = weight[0] * 0 + weight[1] * 0

def weight_change_or(weight: list[float], threshold: float, learning_rate: float) -> list[float]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/simple_adaline.py, please provide doctest for the function weight_change_or

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/simple_adaline.py, please provide doctest for the function weight_change_or

"""
This function updates the weights for the OR gate using the Adaline algorithm.

Args:
weight (list[float]): The weights for the Adaline algorithm.
threshold (float): The threshold value for the Adaline algorithm.
learning_rate (float): The learning rate for the Adaline algorithm.

Returns:
list[float]: The updated weights for the OR gate.
>>> weight_change_or([1.2, 0.6], 1, 0.5)
[1.2, 1.1]
"""
output = weight[0]*0 + weight[1]*0
if output <= threshold:
output_left = weight[0] * 0 + weight[1] * 1
output_left = weight[0]*0 + weight[1]*1
if output_left >= threshold:
output_left_down = weight[0] * 1 + weight[1] * 0
output_left_down = weight[0]*1 + weight[1]*0
if output_left_down >= threshold:
output_all = weight[0] * 1 + weight[1] * 1
output_all = weight[0]*1 + weight[1]*1
if output_all >= threshold:
return weight
else:
weight[0] = weight[0] + learning_rate * 1 * 1
weight[1] = weight[1] + learning_rate * 1 * 1
weight[0] = weight[0] + learning_rate*1*1
weight[1] = weight[1] + learning_rate*1*1
return weight_change_or(weight, threshold, learning_rate)
else:
weight[0] = weight[0] + learning_rate * 1 * 1
weight[1] = weight[1] + learning_rate * 1 * 0
weight[0] = weight[0] + learning_rate*1*1
weight[1] = weight[1] + learning_rate*1*0
return weight_change_or(weight, threshold, learning_rate)
else:
weight[0] = weight[0] + learning_rate * 1 * 0
weight[1] = weight[1] + learning_rate * 1 * 1
weight[0] = weight[0] + learning_rate*1*0
weight[1] = weight[1] + learning_rate*1*1
return weight_change_or(weight, threshold, learning_rate)
else:
threshold += learning_rate
return weight_change_or(weight, threshold, learning_rate)


def weight_change_and(
weight: list[float], threshold: float, learning_rate: float
) -> list[float]:
output = weight[0] * 0 + weight[1] * 0
def weight_change_and(weight: list[float], threshold: float, learning_rate: float) -> list[float]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/simple_adaline.py, please provide doctest for the function weight_change_and

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/simple_adaline.py, please provide doctest for the function weight_change_and

"""
This function updates the weights for the AND gate using the Adaline algorithm.

Args:
weight (list[float]): The weights for the Adaline algorithm.
threshold (float): The threshold value for the Adaline algorithm.
learning_rate (float): The learning rate for the Adaline algorithm.

Returns:
list[float]: The updated weights for the AND gate.
>>> weight_change_and([1.2, 0.6], 1, 0.5)
[0.7, 0.1]
"""
output = weight[0]*0 + weight[1]*0
if output <= threshold:
output_left = weight[0] * 0 + weight[1] * 1
output_left = weight[0]*0 + weight[1]*1
if output_left <= threshold:
output_left_down = weight[0] * 1 + weight[1] * 0
output_left_down = weight[0]*1 + weight[1]*0
if output_left_down <= threshold:
output_all = weight[0] * 1 + weight[1] * 1
output_all = weight[0]*1 + weight[1]*1
if output_all >= threshold:
return weight
else:
weight[0] = weight[0] + (learning_rate * 1 * 1)
weight[1] = weight[1] + (learning_rate * 1 * 1)
weight[0] = weight[0] + (learning_rate*1*1)
weight[1] = weight[1] + (learning_rate*1*1)
return weight_change_and(weight, threshold, learning_rate)
else:
weight[0] = weight[0] - (learning_rate * 1 * 1)
weight[1] = weight[1] - (learning_rate * 1 * 0)
weight[0] = weight[0] - (learning_rate*1*1)
weight[1] = weight[1] - (learning_rate*1*0)
return weight_change_and(weight, threshold, learning_rate)
else:
weight[0] = weight[0] - (learning_rate * 1 * 0)
weight[1] = weight[1] - (learning_rate * 1 * 1)
weight[0] = weight[0] - (learning_rate*1*0)
weight[1] = weight[1] - (learning_rate*1*1)
return weight_change_and(weight, threshold, learning_rate)
else:
threshold += learning_rate
return weight_change_and(weight, threshold, learning_rate)


def and_gate(
weight: list[float],
input_a: int,
input_b: int,
threshold: float,
learning_rate: float,
) -> int:
def and_gate(weight: list[float], input_a: int, input_b: int, threshold: float, learning_rate: float) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/simple_adaline.py, please provide doctest for the function and_gate

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/simple_adaline.py, please provide doctest for the function and_gate

"""
This function implements the AND gate using the Adaline algorithm.

Expand All @@ -76,22 +93,24 @@ def and_gate(

Returns:
int: The output of the AND gate.
>>> and_gate([1.2, 0.6], 0, 0, 1, 0.5)
0
>>> and_gate([1.2, 0.6], 0, 1, 1, 0.5)
0
>>> and_gate([1.2, 0.6], 1, 0, 1, 0.5)
0
>>> and_gate([1.2, 0.6], 1, 1, 1, 0.5)
1
"""
weight = weight_change_and(weight, threshold, learning_rate)
output = weight[0] * input_a + weight[1] * input_b
output = weight[0]*input_a + weight[1]*input_b
if output >= threshold:
return 1
else:
return 0


def or_gate(
weight: list[float],
input_a: int,
input_b: int,
threshold: float,
learning_rate: float,
) -> int:
def or_gate(weight: list[float], input_a: int, input_b: int, threshold: float, learning_rate: float) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/simple_adaline.py, please provide doctest for the function or_gate

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/simple_adaline.py, please provide doctest for the function or_gate

"""
This function implements the OR gate using the Adaline algorithm.

Expand All @@ -104,27 +123,28 @@ def or_gate(

Returns:
int: The output of the OR gate.
>>> or_gate([1.2, 0.6], 0, 0, 1, 0.5)
0
>>> or_gate([1.2, 0.6], 0, 1, 1, 0.5)
1
>>> or_gate([1.2, 0.6], 1, 0, 1, 0.5)
1
>>> or_gate([1.2, 0.6], 1, 1, 1, 0.5)
1
"""
weight = weight_change_or(weight, threshold, learning_rate)
output = weight[0] * input_a + weight[1] * input_b
output = weight[0]*input_a + weight[1]*input_b
if output >= threshold:
return 1
else:
return 0


weight = [1.2, 0.6]
weight2 = [1.2, 0.6]
threshold = 1
learning_rate = 0.5
input_a, input_b = input("Input the value of A and B:").split()
input_a = int(input_a)
input_b = int(input_b)
print(
"\nThe output of OR is:",
or_gate(weight, input_a, input_b, threshold, learning_rate),
)
print(
"\nThe output of AND is:",
and_gate(weight2, input_a, input_b, threshold, learning_rate),
)
print("\nThe output of OR is:", or_gate(weight, input_a, input_b, threshold, learning_rate))
print("\nThe output of AND is:", and_gate(weight2, input_a, input_b, threshold, learning_rate))