0% found this document useful (0 votes)
33 views6 pages

Or Gate Noisy

This document describes training a perceptron model to perform an OR operation on noisy data. It initializes weights and biases, defines training examples with added noise, and runs the model through 20 epochs. It tracks the weights, errors, and outputs at each epoch in a table and plots the updated weights and errors over epochs. The final trained weights and bias are printed at the end.

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)
33 views6 pages

Or Gate Noisy

This document describes training a perceptron model to perform an OR operation on noisy data. It initializes weights and biases, defines training examples with added noise, and runs the model through 20 epochs. It tracks the weights, errors, and outputs at each epoch in a table and plots the updated weights and errors over epochs. The final trained weights and bias are printed at the end.

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
You are on page 1/ 6

11/15/23, 2:27 PM OR_noisy

In [1]: import numpy as np


from prettytable import PrettyTable
import matplotlib.pyplot as plt

# Perceptron initialization
np.random.seed(42) # for reproducibility
w = np.random.rand(2) # Two weights for the two inputs
b = np.random.rand()

# Learning rate
alpha = 0.1

# Number of epochs
num_epochs = 20

# Training examples for OR gate


training_data_or = [
(np.array([0, 0]), 0),
(np.array([0, 1]), 1),
(np.array([1, 0]), 1),
(np.array([1, 1]), 1)
]

# Noise level for input and target (adjust as needed)


input_noise_level = 0.1
target_noise_level = 0.1

# Initialize PrettyTable
table = PrettyTable()
table.field_names = ["Epoch", "Noisy Input", "Noisy Target", "Output", "Error", "Up

# Lists to store the weights and errors for plotting


epoch_weights = []
errors = []

# Perceptron learning rule


for epoch in range(num_epochs):
for x, t in training_data_or:
# Add noise to the input and target values
noisy_input = x + np.random.uniform(-input_noise_level, input_noise_level,
noisy_target = t + np.random.uniform(-target_noise_level, target_noise_leve

y = 1 if np.dot(w, noisy_input) + b > 0 else 0 # Step function


error = noisy_target - y
w = w + alpha * error * noisy_input
b = b + alpha * error

# Add row to the table


table.add_row([epoch + 1, str(np.round(noisy_input, 2)), f"{np.round(noisy_

# Append the last weight and error of the epoch to the lists
epoch_weights.append(w.copy())
errors.append(error)

file:///C:/Users/musta/Downloads/OR_noisy.html 1/6
11/15/23, 2:27 PM OR_noisy

# Print the table


table_str = table.get_string()

# Final weights and bias


final_weights_str = f"\nFinal weights: {np.round(w, 2)}"
final_bias_str = f"Final bias: {np.round(b, 2)}"

# Plotting
plt.figure(figsize=(12, 8))

# Plot updated weights


plt.subplot(2, 1, 1)
for i in range(2): # Two weights for the two inputs
plt.plot(range(1, num_epochs + 1), [np.round(weights[i], 2) for weights in epoc
plt.title('Updated Weights Over Epochs')
plt.xlabel('Epoch')
plt.ylabel('Weight Value')
plt.legend()
plt.grid(True)

# Plot errors
plt.subplot(2, 1, 2)
plt.plot(range(1, num_epochs + 1), np.round(errors, 2), marker='o', linestyle='-',
plt.title('Errors Over Epochs')
plt.xlabel('Epoch')
plt.ylabel('Error')
plt.grid(True)

plt.tight_layout()

# Display output
print(table_str)
print(final_weights_str)
print(final_bias_str)
plt.show()

file:///C:/Users/musta/Downloads/OR_noisy.html 2/6
11/15/23, 2:27 PM OR_noisy

+-------+---------------+--------------+--------+-------+-----------------+---------
-----+
| Epoch | Noisy Input | Noisy Target | Output | Error | Updated weights | Updated
bias |
+-------+---------------+--------------+--------+-------+-----------------+---------
-----+
| 1 | [ 0.02 -0.07] | -0.07 | 1 | -1.07 | [0.37 0.96] | 0.63
|
| 1 | [-0.09 1.07] | 1.02 | 1 | 0.02 | [0.37 0.96] | 0.63
|
| 1 | [ 1.04 -0.1 ] | 1.09 | 1 | 0.09 | [0.38 0.96] | 0.64
|
| 1 | [1.07 0.94] | 0.94 | 1 | -0.06 | [0.38 0.95] | 0.63
|
| 2 | [-0.06 -0.04] | 0.0 | 1 | -1.0 | [0.38 0.96] | 0.53
|
| 2 | [-0.01 0.96] | 1.02 | 1 | 0.02 | [0.38 0.96] | 0.53
|
| 2 | [ 0.93 -0.04] | 0.97 | 1 | -0.03 | [0.38 0.96] | 0.53
|
| 2 | [0.99 1.06] | 0.94 | 1 | -0.06 | [0.37 0.95] | 0.52
|
| 3 | [0. 0.02] | -0.09 | 1 | -1.09 | [0.37 0.95] | 0.42
|
| 3 | [0.02 0.93] | 0.91 | 1 | -0.09 | [0.37 0.94] | 0.41
|
| 3 | [1.09 0.09] | 1.06 | 1 | 0.06 | [0.38 0.94] | 0.41
|
| 3 | [0.96 0.92] | 1.04 | 1 | 0.04 | [0.38 0.95] | 0.42
|
| 4 | [-0.01 -0.08] | -0.0 | 1 | -1.0 | [0.38 0.95] | 0.32
|
| 4 | [-0.09 1.08] | 0.95 | 1 | -0.05 | [0.38 0.95] | 0.31
|
| 4 | [ 1.03 -0.04] | 1.0 | 1 | 0.0 | [0.38 0.95] | 0.31
|
| 4 | [1.01 0.94] | 1.09 | 1 | 0.09 | [0.39 0.96] | 0.32
|
| 5 | [0.06 0.09] | 0.08 | 1 | -0.92 | [0.39 0.95] | 0.23
|
| 5 | [0.02 1.08] | 0.92 | 1 | -0.08 | [0.39 0.94] | 0.22
|
| 5 | [ 0.94 -0.09] | 0.97 | 1 | -0.03 | [0.39 0.94] | 0.22
|
| 5 | [0.98 0.95] | 1.07 | 1 | 0.07 | [0.39 0.95] | 0.22
|
| 6 | [-0.03 -0.04] | 0.01 | 1 | -0.99 | [0.4 0.95] | 0.12
|
| 6 | [-0.07 1.06] | 0.91 | 1 | -0.09 | [0.4 0.94] | 0.12
|
| 6 | [1.1 0.05] | 0.94 | 1 | -0.06 | [0.39 0.94] | 0.11
|
| 6 | [0.9 1.06] | 1.04 | 1 | 0.04 | [0.39 0.95] | 0.11
|
| 7 | [0.05 0.05] | -0.09 | 1 | -1.09 | [0.39 0.94] | 0.01
|

file:///C:/Users/musta/Downloads/OR_noisy.html 3/6
11/15/23, 2:27 PM OR_noisy

| 7 | [-0.03 0.92] | 1.07 | 1 | 0.07 | [0.39 0.95] | 0.01


|
| 7 | [ 1.02 -0.03] | 0.91 | 1 | -0.09 | [0.38 0.95] | 0.0
|
| 7 | [0.96 0.97] | 1.05 | 1 | 0.05 | [0.38 0.95] | 0.01
|
| 8 | [0.03 0.08] | -0.01 | 1 | -1.01 | [0.38 0.94] | -0.09
|
| 8 | [-0.08 1.04] | 1.05 | 1 | 0.05 | [0.38 0.95] | -0.09
|
| 8 | [1.01 0.05] | 1.0 | 1 | -0.0 | [0.38 0.95] | -0.09
|
| 8 | [1. 0.99] | 0.91 | 1 | -0.09 | [0.37 0.94] | -0.1
|
| 9 | [-0.08 -0.09] | 0.03 | 0 | 0.03 | [0.37 0.94] | -0.09
|
| 9 | [-0.04 1. ] | 1.08 | 1 | 0.08 | [0.37 0.95] | -0.09
|
| 9 | [ 0.95 -0.02] | 1.05 | 1 | 0.05 | [0.37 0.95] | -0.08
|
| 9 | [0.95 0.92] | 0.96 | 1 | -0.04 | [0.37 0.94] | -0.08
|
| 10 | [-0.07 0.09] | 0.06 | 0 | 0.06 | [0.37 0.95] | -0.08
|
| 10 | [0.03 1.07] | 1.06 | 1 | 0.06 | [0.37 0.95] | -0.07
|
| 10 | [0.94 0.08] | 1.01 | 1 | 0.01 | [0.37 0.95] | -0.07
|
| 10 | [1.06 1.08] | 0.96 | 1 | -0.04 | [0.37 0.95] | -0.07
|
| 11 | [-0.08 -0.05] | -0.01 | 0 | -0.01 | [0.37 0.95] | -0.08
|
| 11 | [0.06 1.07] | 0.9 | 1 | -0.1 | [0.37 0.94] | -0.09
|
| 11 | [ 1. -0.02] | 0.94 | 1 | -0.06 | [0.36 0.94] | -0.09
|
| 11 | [0.92 0.97] | 1.09 | 1 | 0.09 | [0.37 0.95] | -0.08
|
| 12 | [-0.04 0. ] | 0.04 | 0 | 0.04 | [0.37 0.95] | -0.08
|
| 12 | [-0.03 1.09] | 1.09 | 1 | 0.09 | [0.37 0.96] | -0.07
|
| 12 | [ 0.95 -0. ] | 0.96 | 1 | -0.04 | [0.37 0.96] | -0.07
|
| 12 | [0.96 0.91] | 1.02 | 1 | 0.02 | [0.37 0.96] | -0.07
|
| 13 | [ 0. -0.09] | -0.04 | 0 | -0.04 | [0.37 0.96] | -0.08
|
| 13 | [0.08 0.95] | 0.93 | 1 | -0.07 | [0.37 0.95] | -0.08
|
| 13 | [1. 0.1] | 0.95 | 1 | -0.05 | [0.36 0.95] | -0.09
|
| 13 | [1.03 1.05] | 0.95 | 1 | -0.05 | [0.36 0.95] | -0.09
|
| 14 | [ 0.05 -0.03] | 0.03 | 0 | 0.03 | [0.36 0.95] | -0.09
|

file:///C:/Users/musta/Downloads/OR_noisy.html 4/6
11/15/23, 2:27 PM OR_noisy

| 14 | [0.03 1.01] | 0.92 | 1 | -0.08 | [0.36 0.94] | -0.1


|
| 14 | [ 1.07 -0.04] | 0.94 | 1 | -0.06 | [0.35 0.94] | -0.11
|
| 14 | [0.91 1.02] | 1.04 | 1 | 0.04 | [0.35 0.94] | -0.1
|
| 15 | [-0.1 0. ] | -0.05 | 0 | -0.05 | [0.35 0.94] | -0.11
|
| 15 | [0.03 0.93] | 1.04 | 1 | 0.04 | [0.35 0.95] | -0.1
|
| 15 | [0.98 0.09] | 0.93 | 1 | -0.07 | [0.35 0.94] | -0.11
|
| 15 | [0.97 0.92] | 1.08 | 1 | 0.08 | [0.35 0.95] | -0.1
|
| 16 | [ 0.08 -0.05] | 0.03 | 0 | 0.03 | [0.35 0.95] | -0.1
|
| 16 | [0.06 1.01] | 1.01 | 1 | 0.01 | [0.35 0.95] | -0.1
|
| 16 | [ 0.95 -0.08] | 1.08 | 1 | 0.08 | [0.36 0.95] | -0.09
|
| 16 | [1.08 1.03] | 0.97 | 1 | -0.03 | [0.36 0.95] | -0.09
|
| 17 | [-0.03 0.05] | 0.08 | 0 | 0.08 | [0.36 0.95] | -0.09
|
| 17 | [0.08 1.06] | 1.03 | 1 | 0.03 | [0.36 0.95] | -0.08
|
| 17 | [ 0.92 -0.07] | 1.08 | 1 | 0.08 | [0.37 0.95] | -0.07
|
| 17 | [1.02 0.9 ] | 0.92 | 1 | -0.08 | [0.36 0.94] | -0.08
|
| 18 | [ 0.03 -0.1 ] | -0.07 | 0 | -0.07 | [0.36 0.95] | -0.09
|
| 18 | [0.01 1.04] | 1.03 | 1 | 0.03 | [0.36 0.95] | -0.09
|
| 18 | [0.94 0.04] | 0.95 | 1 | -0.05 | [0.35 0.95] | -0.09
|
| 18 | [0.97 1.05] | 1.03 | 1 | 0.03 | [0.36 0.95] | -0.09
|
| 19 | [0.07 0.03] | 0.01 | 0 | 0.01 | [0.36 0.95] | -0.09
|
| 19 | [-0.08 0.97] | 0.95 | 1 | -0.05 | [0.36 0.95] | -0.09
|
| 19 | [0.95 0.09] | 0.98 | 1 | -0.02 | [0.35 0.95] | -0.09
|
| 19 | [1.08 1.03] | 1.06 | 1 | 0.06 | [0.36 0.95] | -0.09
|
| 20 | [0. 0.02] | -0.0 | 0 | -0.0 | [0.36 0.95] | -0.09
|
| 20 | [-0.06 1.04] | 0.96 | 1 | -0.04 | [0.36 0.95] | -0.09
|
| 20 | [0.9 0.03] | 0.94 | 1 | -0.06 | [0.35 0.95] | -0.1
|
| 20 | [1.09 1.09] | 1.08 | 1 | 0.08 | [0.36 0.96] | -0.09
|
+-------+---------------+--------------+--------+-------+-----------------+---------
-----+

file:///C:/Users/musta/Downloads/OR_noisy.html 5/6
11/15/23, 2:27 PM OR_noisy

Final weights: [0.36 0.96]


Final bias: -0.09

In [ ]:

file:///C:/Users/musta/Downloads/OR_noisy.html 6/6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy