我想编写一个matlab函数,使用一个单层感知器找到2个可分离点集的线性分类器的方程。我有2个文件:
脚本文件 - run.m:
x_1 = [3, 3, 2, 4, 5];
y_1 = [3, 4, 5, 2, 2];
x_2 = [6, 7, 5, 9, 8];
y_2 = [3, 3, 4, 2, 5];
target_array = [0 0 0 0 0 1 1 1 1 1];
[ func ] = classify_perceptron([x_1 x_2; y_1 y_2], target_array);
x = -2:10;
y = arrayfun(func, x);
plot(x_1, y_1, 'o', x_2, y_2, 'X', x, y);
axis([-2, 10, -2, 10]);
classify_perceptron.m
function [ func ] = classify_perceptron( points, target )
% points - matrix of x,y coordinates
% target - array of expected results
% func - function handler which appropriately classifies a point
% given by x, y arguments supplied to this function
target_arr = target;
weights = rand(1, 2);
translation = rand();
for i=1:size(points, 2)
flag = true;
while flag
result = weights * points(:, i) + translation;
y = result > 0;
e = target_arr(1, i) - y;
if e ~= 0
weights = weights + (e * points(:, i))';
translation = translation + e;
else
flag = false;
end
end
end
func = @(x)(-(translation + (weights(1, 1) * x)) / weights(1, 2));
return
end
问题在于我不知道我在哪里犯了导致错误结果的错误。看起来线的斜率是正确的,但是平移应该更大一些。我真的很感谢指出我正确的方向。我得到的结果如下图所示:
答案 0 :(得分:0)
好的,所以我取得了重大进展。万一有人遇到同样的问题我会向你提出解决方案。通过添加变量learning_rate = 0.1
并将迭代迭代的循环打包到另一个循环中来解决问题,该循环迭代次数与变量epochs (e.g. 300)
中指定的次数相同。