Or Gate Noisy
Or Gate Noisy
# 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
# Initialize PrettyTable
table = PrettyTable()
table.field_names = ["Epoch", "Noisy Input", "Noisy Target", "Output", "Error", "Up
# 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
# Plotting
plt.figure(figsize=(12, 8))
# 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
file:///C:/Users/musta/Downloads/OR_noisy.html 4/6
11/15/23, 2:27 PM OR_noisy
file:///C:/Users/musta/Downloads/OR_noisy.html 5/6
11/15/23, 2:27 PM OR_noisy
In [ ]:
file:///C:/Users/musta/Downloads/OR_noisy.html 6/6