Skip to content

Instantly share code, notes, and snippets.

@DamienAllonsius
Created July 4, 2019 14:42
Show Gist options
  • Save DamienAllonsius/db35932f30e4b2cf84a8661ee759f438 to your computer and use it in GitHub Desktop.
Save DamienAllonsius/db35932f30e4b2cf84a8661ee759f438 to your computer and use it in GitHub Desktop.
perceptron
import numpy as np
from matplotlib.pyplot import plot
N = 1000
x_p = np.random.rand(N)
y_p = np.random.rand(N)
p = zip(x_p, y_p)
x_m = -np.random.rand(N)
y_m = -np.random.rand(N)
m = zip(x_m, y_m)
plot(x_m, y_m, "*")
plot(x_p, y_p, "o")
w = [np.random.rand(), np.random.rand()]
learning_rate = 0.1
for _ in range(N):
sign = np.random.randint(0,2)
if sign:
point = next(p)
else:
point = next(m)
sign = -1
if sign * (point[0] * w[0] + point[1] * w[1]) < 0:
w[0] = w[0] + learning_rate * sign * point[0]
w[1] = w[1] + learning_rate * sign * point[1]
size = 1.5
plot([0,-w[1]*size], [0, w[0]*size], "blue", linewidth=3)
plot([0,w[1]*size], [0, -w[0]*size], "blue", linewidth=3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment