File:Perceptron convergence theorem.svg
Summary
Description |
English: Desc
Matplotlibimport numpy as np
import matplotlib.pyplot as plt
# Initialize variables
ws = []
gamma = 0.01
R = 1
# Generate random points within the circle of radius R
points = []
while len(points) < 100:
x, y = np.random.uniform(-R, R, 2)
if x**2 + y**2 <= R**2:
points.append((x, y))
# Remove points with x < gamma
points = [point for point in points if point[0] >= gamma]
# Initialize w
ws = []
w = np.array([-0.1, 0.0])
ws.append(w.copy())
# Perceptron learning algorithm
convergence_flag = False
while not convergence_flag:
np.random.shuffle(points)
convergence_flag = True
for v in points:
if np.dot(w, v) <= 0:
w += v
ws.append(w.copy())
convergence_flag = False
# Plot the items
fig, ax = plt.subplots(figsize=(10,10))
# Vertical dotted line at x=gamma
ax.axvline(x=gamma, linestyle='--', color='r', label='x=gamma')
# Circle with radius R centered at (0, 0)
circle = plt.Circle((0, 0), R, fill=False, color='b', linestyle='-', label='Circle')
ax.add_artist(circle)
# Jagged line connecting entries in ws
points = np.array(points)
ax.scatter(points[:,0], points[:,1])
ws = np.array(ws)
for i in range(1, ws.shape[0]):
ax.arrow(ws[i-1, 0], ws[i-1, 1], ws[i, 0] - ws[i-1, 0], ws[i, 1] - ws[i-1, 1], color='g', linewidth=0.5,
head_width=0.03, head_length=0.04, length_includes_head=True)
# Set axis limits
# ax.set_xlim(-R, R)
# ax.set_ylim(-R, R)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
plt.gca().set_aspect('equal')
plt.savefig("perceptron convergence.svg") |
Date | |
Source | Own work |
Author | Cosmia Nebula |
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
- You are free:
- to share – to copy, distribute and transmit the work
- to remix – to adapt the work
- Under the following conditions:
- attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
- share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.