分类感知器实施

时间:2017-07-22 19:00:28

标签: python neural-network perceptron

我在here的Python中编写了Percentron示例。

这是完整的代码

$con = mysqli_connect("localhost","root","","profiles");
$q = mysqli_query($con,"SELECT * FROM users");

if (false === $q) {
    echo mysqli_error();
}else{

    while($row = mysqli_fetch_assoc($q)){
        echo $row['username'];
        if($row['image'] == ""){
            echo "<img width='100' height='100' src='pictures/default.jpg' alt='Default Profile Pic'>";
        } else {
            echo "<img width='100' height='100' src='pictures/".$row['image']."' alt='Profile Pic'>";
        }
        echo "<br>";
    }
}

我希望在图表上绘制的点可以根据预期条件(x坐标> y坐标)分类为红色或蓝色,即高于或低于参考线(y = x)

这似乎不起作用,经过一些迭代后所有点都变红了。

我在这里做错了什么。同样适用于youtube示例。

1 个答案:

答案 0 :(得分:0)

我查看了你的代码和视频,我相信你的代码编写方式,点数开始为绿色,如果他们的猜测与他们的目标匹配,他们会变成红色,如果他们的猜测与目标不符,他们会变成蓝色。当它们的猜测与目标相匹配时,其余的蓝色最终会变为红色。 (改变的重量可能会变成红色到蓝色,但最终会被纠正。)

以下是我对代码的修改,这会减慢进程:添加更多分数;每帧仅处理一个点而不是所有帧:

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)