Skip to content

Instantly share code, notes, and snippets.

@mcapodici
Created April 18, 2023 22:49
Show Gist options
  • Save mcapodici/254dba15c87c3518a2d1ed3a96f22735 to your computer and use it in GitHub Desktop.
Save mcapodici/254dba15c87c3518a2d1ed3a96f22735 to your computer and use it in GitHub Desktop.
Weightspace for perceptron training
# Based on https://www.cs.toronto.edu/~rgrosse/courses/csc321_2018/homeworks/hw2.pdf, 1. b.
import numpy
import pylab
import random
# Training Data
x = numpy.array([
[1, -2],
[0, -1]
])
N = len(x)
i = 0
w = [0, 0]
t = [1, -1]
lastnudge = N - 1
path = numpy.array([w])
for safety in range(1, 100):
zi = numpy.matmul(numpy.transpose(w), x[i])
if zi * t[i] <= 0:
#print ("Nudge")
w = w + t[i] * x[i]
#print (w)
lastnudge = i
path = numpy.append(path, [w], axis=0)
else:
# print("No nudge")
if (lastnudge == i):
break
i = (i + 1) % N
#plotting stuff:
pylab.title("Weight Space of Perceptron Training ($n = " + str(len(path)) + "$ steps)")
pylab.plot(path[:,0],path[:,1])
pylab.xlim(-5, 5)
pylab.ylim(-5, 5)
pylab.axhline(0, color='black', linewidth=.5)
pylab.axvline(0, color='black', linewidth=.5)
pylab.savefig("rand_walk"+str(n)+".png",bbox_inches="tight",dpi=600)
pylab.show()
@mcapodici
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment