0% found this document useful (0 votes)
6 views4 pages

AND - Gate - Biased

This document describes training a perceptron model to perform an AND operation on two binary inputs. It initializes weights and biases randomly, then trains the model over 10 epochs on examples of (input, target) pairs using a learning rate of 0.1. It tracks the weights, errors, and outputs over the epochs and displays the results in a table. The final trained weights and bias are printed, and plots of the updated weights and errors over iterations are shown.

Uploaded by

mostafa98999
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

AND - Gate - Biased

This document describes training a perceptron model to perform an AND operation on two binary inputs. It initializes weights and biases randomly, then trains the model over 10 epochs on examples of (input, target) pairs using a learning rate of 0.1. It tracks the weights, errors, and outputs over the epochs and displays the results in a table. The final trained weights and bias are printed, and plots of the updated weights and errors over iterations are shown.

Uploaded by

mostafa98999
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

11/15/23, 12:21 AM AND_Gate

In [1]: import numpy as np


import [Link] as plt
from prettytable import PrettyTable

# Perceptron initialization
[Link](42) # for reproducibility
w = [Link](2) # Use two weights for the two inputs
b = [Link]()

# Learning rate
alpha = 0.1

# Number of epochs
num_epochs = 10

# Training examples for AND gate


training_data_and = [([Link]([0, 0]), 0), ([Link]([0, 1]), 0), ([Link]([1, 0]

# Lists to store the weights and errors for plotting


epoch_weights = []
errors = []
output_data = PrettyTable()

# Set column names for output table


output_data.field_names = ['Epoch', 'Input', 'Target', 'Output', 'Error', 'Updated

# Perceptron learning rule


for epoch in range(num_epochs):
# Reset weights at the beginning of each epoch
epoch_weights.append([Link]())

for x, t in training_data_and:
y = 1 if [Link](w, x) + b > 0 else 0 # Step function
error = t - y
w = w + alpha * error * x
b = b + alpha * error

# Append the error to the list


[Link](error)

output_data.add_row([epoch + 1, str(x), t, y, error, str([Link](w, 3)), f

# Print output table


print(output_data)

# Final weights and bias


print("\nFinal weights and bias:")
print(f" w = {[Link](w, 3)}")
print(f" b = {b:.3f}")

# Plotting per epoch


[Link](figsize=(12, 4))

# Plot updated weights

[Link] 1/4
11/15/23, 12:21 AM AND_Gate

[Link](1, 2, 1)
for i in range(2): # Two weights for the two inputs
[Link](range(1, num_epochs + 1), [weights[i] for weights in epoch_weights], m
[Link]('Updated Weights Over Epochs')
[Link]('Epoch')
[Link]('Weight Value')
[Link]()
[Link](True)

# Plot errors
[Link](1, 2, 2)
[Link](range(1, len(errors) + 1), errors, marker='o', linestyle='-', color='red')
[Link]('Errors Over Iterations')
[Link]('Iteration')
[Link]('Error')
[Link](True)

plt.tight_layout()
[Link]()

[Link] 2/4
11/15/23, 12:21 AM AND_Gate

+-------+-------+--------+--------+-------+-----------------+--------------+
| Epoch | Input | Target | Output | Error | Updated weights | Updated bias |
+-------+-------+--------+--------+-------+-----------------+--------------+
| 1 | [0 0] | 0 | 1 | -1 | [0.375 0.951] | 0.632 |
| 1 | [0 1] | 0 | 1 | -1 | [0.375 0.851] | 0.532 |
| 1 | [1 0] | 0 | 1 | -1 | [0.275 0.851] | 0.432 |
| 1 | [1 1] | 1 | 1 | 0 | [0.275 0.851] | 0.432 |
| 2 | [0 0] | 0 | 1 | -1 | [0.275 0.851] | 0.332 |
| 2 | [0 1] | 0 | 1 | -1 | [0.275 0.751] | 0.232 |
| 2 | [1 0] | 0 | 1 | -1 | [0.175 0.751] | 0.132 |
| 2 | [1 1] | 1 | 1 | 0 | [0.175 0.751] | 0.132 |
| 3 | [0 0] | 0 | 1 | -1 | [0.175 0.751] | 0.032 |
| 3 | [0 1] | 0 | 1 | -1 | [0.175 0.651] | -0.068 |
| 3 | [1 0] | 0 | 1 | -1 | [0.075 0.651] | -0.168 |
| 3 | [1 1] | 1 | 1 | 0 | [0.075 0.651] | -0.168 |
| 4 | [0 0] | 0 | 0 | 0 | [0.075 0.651] | -0.168 |
| 4 | [0 1] | 0 | 1 | -1 | [0.075 0.551] | -0.268 |
| 4 | [1 0] | 0 | 0 | 0 | [0.075 0.551] | -0.268 |
| 4 | [1 1] | 1 | 1 | 0 | [0.075 0.551] | -0.268 |
| 5 | [0 0] | 0 | 0 | 0 | [0.075 0.551] | -0.268 |
| 5 | [0 1] | 0 | 1 | -1 | [0.075 0.451] | -0.368 |
| 5 | [1 0] | 0 | 0 | 0 | [0.075 0.451] | -0.368 |
| 5 | [1 1] | 1 | 1 | 0 | [0.075 0.451] | -0.368 |
| 6 | [0 0] | 0 | 0 | 0 | [0.075 0.451] | -0.368 |
| 6 | [0 1] | 0 | 1 | -1 | [0.075 0.351] | -0.468 |
| 6 | [1 0] | 0 | 0 | 0 | [0.075 0.351] | -0.468 |
| 6 | [1 1] | 1 | 0 | 1 | [0.175 0.451] | -0.368 |
| 7 | [0 0] | 0 | 0 | 0 | [0.175 0.451] | -0.368 |
| 7 | [0 1] | 0 | 1 | -1 | [0.175 0.351] | -0.468 |
| 7 | [1 0] | 0 | 0 | 0 | [0.175 0.351] | -0.468 |
| 7 | [1 1] | 1 | 1 | 0 | [0.175 0.351] | -0.468 |
| 8 | [0 0] | 0 | 0 | 0 | [0.175 0.351] | -0.468 |
| 8 | [0 1] | 0 | 0 | 0 | [0.175 0.351] | -0.468 |
| 8 | [1 0] | 0 | 0 | 0 | [0.175 0.351] | -0.468 |
| 8 | [1 1] | 1 | 1 | 0 | [0.175 0.351] | -0.468 |
| 9 | [0 0] | 0 | 0 | 0 | [0.175 0.351] | -0.468 |
| 9 | [0 1] | 0 | 0 | 0 | [0.175 0.351] | -0.468 |
| 9 | [1 0] | 0 | 0 | 0 | [0.175 0.351] | -0.468 |
| 9 | [1 1] | 1 | 1 | 0 | [0.175 0.351] | -0.468 |
| 10 | [0 0] | 0 | 0 | 0 | [0.175 0.351] | -0.468 |
| 10 | [0 1] | 0 | 0 | 0 | [0.175 0.351] | -0.468 |
| 10 | [1 0] | 0 | 0 | 0 | [0.175 0.351] | -0.468 |
| 10 | [1 1] | 1 | 1 | 0 | [0.175 0.351] | -0.468 |
+-------+-------+--------+--------+-------+-----------------+--------------+

Final weights and bias:


w = [0.175 0.351]
b = -0.468

[Link] 3/4
11/15/23, 12:21 AM AND_Gate

In [ ]:

[Link] 4/4

You might also like