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()