分类感知器实现
Classification perceptron implementation
我在 here 的 Python 中编写了 Percentron 示例。
这里是完整的代码
import matplotlib.pyplot as plt
import random as rnd
import matplotlib.animation as animation
NUM_POINTS = 5
LEANING_RATE=0.1
fig = plt.figure() # an empty figure with no axes
ax1 = fig.add_subplot(1,1,1)
plt.xlim(0, 120)
plt.ylim(0, 120)
points = []
weights = [rnd.uniform(-1,1),rnd.uniform(-1,1),rnd.uniform(-1,1)]
circles = []
plt.plot([x for x in range(100)], [x for x in range(100)])
for i in range(NUM_POINTS):
x = rnd.uniform(1, 100)
y = rnd.uniform(1, 100)
circ = plt.Circle((x, y), radius=1, fill=False, color='g')
ax1.add_patch(circ)
points.append((x,y,1))
circles.append(circ)
def activation(val):
if val >= 0:
return 1
else:
return -1;
def guess(pt):
vsum = 0
#x and y and bias weights
vsum = vsum + pt[0] * weights[0]
vsum = vsum + pt[1] * weights[1]
vsum = vsum + pt[2] * weights[2]
gs = activation(vsum)
return gs;
def animate(i):
for i in range(NUM_POINTS):
pt = points[i]
if pt[0] > pt[1]:
target = 1
else:
target = -1
gs = guess(pt)
error = target - gs
if target == gs:
circles[i].set_color('r')
else:
circles[i].set_color('b')
#adjust weights
weights[0] = weights[0] + (pt[0] * error * LEANING_RATE)
weights[1] = weights[1] + (pt[1] * error * LEANING_RATE)
weights[2] = weights[2] + (pt[2] * error * LEANING_RATE)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
我希望绘制在图表上的点根据预期条件(x 坐标 > y 坐标)将自己分类为红色或蓝色,即高于或低于参考线 (y=x)
这似乎不起作用,经过一些迭代后所有点都变成红色。
我在这里做错了什么。同样适用于 youtube 示例。
我看了你的代码和视频,我相信你的代码是这样写的,点一开始是绿色的,如果他们的猜测与他们的目标相符,他们就会变成红色,如果他们的猜测与目标不相符,他们就会变蓝。当他们的猜测与目标相符时,剩余的蓝色最终会变成红色。 (不断变化的权重可能会将红色变成蓝色,但最终会得到纠正。)
以下是我对您的代码进行的返工,通过以下方式减慢了流程:添加更多点;每帧只处理一个点而不是所有点:
import random as rnd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
NUM_POINTS = 100
LEARNING_RATE = 0.1
X, Y = 0, 1
fig = plt.figure() # an empty figure with no axes
ax1 = fig.add_subplot(1, 1, 1)
plt.xlim(0, 120)
plt.ylim(0, 120)
plt.plot([x for x in range(100)], [y for y in range(100)])
weights = [rnd.uniform(-1, 1), rnd.uniform(-1, 1)]
points = []
circles = []
for i in range(NUM_POINTS):
x = rnd.uniform(1, 100)
y = rnd.uniform(1, 100)
points.append((x, y))
circle = plt.Circle((x, y), radius=1, fill=False, color='g')
circles.append(circle)
ax1.add_patch(circle)
def activation(val):
if val >= 0:
return 1
return -1
def guess(point):
vsum = 0
# x and y and bias weights
vsum += point[X] * weights[X]
vsum += point[Y] * weights[Y]
return activation(vsum)
def train(point, error):
# adjust weights
weights[X] += point[X] * error * LEARNING_RATE
weights[Y] += point[Y] * error * LEARNING_RATE
point_index = 0
def animate(frame):
global point_index
point = points[point_index]
if point[X] > point[Y]:
answer = 1 # group A (X > Y)
else:
answer = -1 # group B (Y > X)
guessed = guess(point)
if answer == guessed:
circles[point_index].set_color('r')
else:
circles[point_index].set_color('b')
train(point, answer - guessed)
point_index = (point_index + 1) % NUM_POINTS
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()
我放弃了特殊的 0,0 输入修复,因为它不适用于此示例。
最重要的是,如果一切正常,它们 应该 都变成红色。如果你想让颜色反映分类,那么你可以改变这个条款:
if answer == guessed:
circles[point_index].set_color('r' if answer == 1 else 'b')
else:
circles[point_index].set_color('g')
train(point, answer - guessed)
我在 here 的 Python 中编写了 Percentron 示例。
这里是完整的代码
import matplotlib.pyplot as plt
import random as rnd
import matplotlib.animation as animation
NUM_POINTS = 5
LEANING_RATE=0.1
fig = plt.figure() # an empty figure with no axes
ax1 = fig.add_subplot(1,1,1)
plt.xlim(0, 120)
plt.ylim(0, 120)
points = []
weights = [rnd.uniform(-1,1),rnd.uniform(-1,1),rnd.uniform(-1,1)]
circles = []
plt.plot([x for x in range(100)], [x for x in range(100)])
for i in range(NUM_POINTS):
x = rnd.uniform(1, 100)
y = rnd.uniform(1, 100)
circ = plt.Circle((x, y), radius=1, fill=False, color='g')
ax1.add_patch(circ)
points.append((x,y,1))
circles.append(circ)
def activation(val):
if val >= 0:
return 1
else:
return -1;
def guess(pt):
vsum = 0
#x and y and bias weights
vsum = vsum + pt[0] * weights[0]
vsum = vsum + pt[1] * weights[1]
vsum = vsum + pt[2] * weights[2]
gs = activation(vsum)
return gs;
def animate(i):
for i in range(NUM_POINTS):
pt = points[i]
if pt[0] > pt[1]:
target = 1
else:
target = -1
gs = guess(pt)
error = target - gs
if target == gs:
circles[i].set_color('r')
else:
circles[i].set_color('b')
#adjust weights
weights[0] = weights[0] + (pt[0] * error * LEANING_RATE)
weights[1] = weights[1] + (pt[1] * error * LEANING_RATE)
weights[2] = weights[2] + (pt[2] * error * LEANING_RATE)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
我希望绘制在图表上的点根据预期条件(x 坐标 > y 坐标)将自己分类为红色或蓝色,即高于或低于参考线 (y=x)
这似乎不起作用,经过一些迭代后所有点都变成红色。
我在这里做错了什么。同样适用于 youtube 示例。
我看了你的代码和视频,我相信你的代码是这样写的,点一开始是绿色的,如果他们的猜测与他们的目标相符,他们就会变成红色,如果他们的猜测与目标不相符,他们就会变蓝。当他们的猜测与目标相符时,剩余的蓝色最终会变成红色。 (不断变化的权重可能会将红色变成蓝色,但最终会得到纠正。)
以下是我对您的代码进行的返工,通过以下方式减慢了流程:添加更多点;每帧只处理一个点而不是所有点:
import random as rnd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
NUM_POINTS = 100
LEARNING_RATE = 0.1
X, Y = 0, 1
fig = plt.figure() # an empty figure with no axes
ax1 = fig.add_subplot(1, 1, 1)
plt.xlim(0, 120)
plt.ylim(0, 120)
plt.plot([x for x in range(100)], [y for y in range(100)])
weights = [rnd.uniform(-1, 1), rnd.uniform(-1, 1)]
points = []
circles = []
for i in range(NUM_POINTS):
x = rnd.uniform(1, 100)
y = rnd.uniform(1, 100)
points.append((x, y))
circle = plt.Circle((x, y), radius=1, fill=False, color='g')
circles.append(circle)
ax1.add_patch(circle)
def activation(val):
if val >= 0:
return 1
return -1
def guess(point):
vsum = 0
# x and y and bias weights
vsum += point[X] * weights[X]
vsum += point[Y] * weights[Y]
return activation(vsum)
def train(point, error):
# adjust weights
weights[X] += point[X] * error * LEARNING_RATE
weights[Y] += point[Y] * error * LEARNING_RATE
point_index = 0
def animate(frame):
global point_index
point = points[point_index]
if point[X] > point[Y]:
answer = 1 # group A (X > Y)
else:
answer = -1 # group B (Y > X)
guessed = guess(point)
if answer == guessed:
circles[point_index].set_color('r')
else:
circles[point_index].set_color('b')
train(point, answer - guessed)
point_index = (point_index + 1) % NUM_POINTS
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()
我放弃了特殊的 0,0 输入修复,因为它不适用于此示例。
最重要的是,如果一切正常,它们 应该 都变成红色。如果你想让颜色反映分类,那么你可以改变这个条款:
if answer == guessed:
circles[point_index].set_color('r' if answer == 1 else 'b')
else:
circles[point_index].set_color('g')
train(point, answer - guessed)