File size: 5,384 Bytes
b0b4225 bc7a1f8 b0b4225 bc7a1f8 b0b4225 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
import os
# Heaviside step function
def heaviside_step(x):
return np.where(x >= 0, 1, 0)
# Perceptron Learning Algorithm with detailed logging
def perceptron_learning(X, D, w, learning_rate=0.1, max_epochs=100):
epoch_errors = []
weight_history = [w.copy()]
epoch = 0
print(f"Initial weights: {w}")
print(f"Learning rate: {learning_rate}")
print(f"Input patterns X:\n{X}")
print(f"Desired outputs D: {D}")
while epoch < max_epochs:
total_error = 0
print(f"\nEpoch {epoch + 1}")
for i in range(len(X)):
x_i = X[i]
d_i = D[i]
y_i = heaviside_step(np.dot(w, x_i))
error = d_i - y_i
total_error += abs(error)
print(f" Pattern {i + 1}: x = {x_i}, d = {d_i}, y = {y_i}, error = {error}")
if error != 0:
w_old = w.copy()
w = w + learning_rate * error * x_i
print(f" Weights updated: {w_old} -> {w}")
else:
print(" No weight update needed")
error_rate = (total_error / len(X)) * 100
epoch_errors.append(error_rate)
weight_history.append(w.copy())
print(f" Total error: {total_error}")
print(f" Error rate: {error_rate}%")
print(f" Current weights: {w}")
if total_error == 0:
print("Convergence achieved!")
break
epoch += 1
return w, np.array(weight_history), epoch_errors, epoch
# Plot Results
def plot_results(X, D, w_final, epoch_errors, weight_history):
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(18, 6))
# Decision Boundary Plot
for i in range(len(D)):
color = 'blue' if D[i] == 1 else 'red'
label = '1' if D[i] == 1 and i == 0 else ('0' if D[i] == 0 and i == 1 else "")
ax1.scatter(X[i][1], X[i][2], color=color, label=label, s=100)
x1_vals = np.linspace(-0.5, 1.5, 100)
if w_final[2] != 0:
x2_vals = -(w_final[0] + w_final[1] * x1_vals) / w_final[2]
ax1.plot(x1_vals, x2_vals, label='Decision Boundary', color='green')
elif w_final[1] != 0:
ax1.axvline(x=-w_final[0] / w_final[1], color='k', linestyle='--', label='Vertical Decision Boundary')
ax1.set_xlim(-0.5, 1.5)
ax1.set_ylim(-0.5, 1.5)
ax1.set_title("Final Decision Boundary")
ax1.set_xlabel('x1')
ax1.set_ylabel('x2')
ax1.legend()
ax1.grid(True)
# Error Rate Plot
ax2.plot(range(len(epoch_errors)), epoch_errors, marker='o', linestyle='-', color='b')
ax2.set_title("Error Rate Over Epochs")
ax2.set_xlabel("Epoch")
ax2.set_ylabel("Error Rate (%)")
ax2.grid(True)
# Weight History Plot
epochs = range(len(weight_history))
ax3.plot(epochs, weight_history[:, 0], 'r', label='w0 (bias)')
ax3.plot(epochs, weight_history[:, 1], 'g', label='w1 (x1)')
ax3.plot(epochs, weight_history[:, 2], 'b', label='w2 (x2)')
ax3.set_title("Weight History")
ax3.set_xlabel("Epoch")
ax3.set_ylabel("Weight Value")
ax3.legend()
ax3.grid(True)
plt.tight_layout()
return fig
# Main function for running the perceptron
def run_perceptron(w0, w1, w2, learning_rate, max_epochs):
# Input patterns for OR operator
X = np.array([[1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]])
D = np.array([0, 1, 1, 1]) # Desired outputs
# Initial weights
w_initial = np.array([w0, w1, w2], dtype=float)
# Train the perceptron
w_final, weight_history, epoch_errors, epochs = perceptron_learning(X, D, w_initial, learning_rate, max_epochs)
# Plot the results
fig = plot_results(X, D, w_final, epoch_errors, weight_history)
# Save the figure to a file
if not os.path.exists('temp'):
os.makedirs('temp')
fig_path = 'temp/perceptron_results.png'
plt.savefig(fig_path, dpi=300, bbox_inches='tight')
plt.close(fig)
# Format weight history as a string
weight_history_str = "\n".join([f"Epoch {i}: {w}" for i, w in enumerate(weight_history)])
# Final summary
summary = f"Final weights: {w_final}\nConverged in {epochs} epochs"
# Link to article
link_article = "Read more about how to apply the learning algorithm to a perceptron here: "
link_article += "https://danopt.substack.com/p/applying-and-visualizing-the-learning"
return fig_path, summary, weight_history_str, link_article
# Gradio Interface
iface = gr.Interface(
fn=run_perceptron,
inputs=[
gr.Slider(-1, 1, 1, label="Initial w0 (bias)"),
gr.Slider(-1, 1, -1, label="Initial w1"),
gr.Slider(-1, 1, -1, label="Initial w2"),
gr.Slider(0.01, 1, 0.1, label="Learning Rate"),
gr.Slider(1, 1000, 100, step=1, label="Max Epochs")
],
outputs=[
gr.Image(type="filepath", label="Results"),
gr.Textbox(label="Summary"),
gr.Textbox(label="Detailed Weight History", lines=10),
gr.Markdown(label="Info")
],
title="Perceptron Learning for OR Operator",
description="Adjust the initial weights, learning rate, and maximum epochs to see how the perceptron learns the OR operator. You can download the graph or view it in fullscreen mode."
)
if __name__ == "__main__":
iface.launch() |