在 MATLAB 中创建列车感知器以进行性别分类
我正在编写一个感知器来学习对面部图片中的性别进行分类。我对 MATLAB 非常陌生,所以我需要很多帮助。我有几个问题:
我正在尝试编写函数代码:
函数 [y] = 测试集(x,w) %y = 符号(sigma(x*w-阈值))
其中 y 是预测结果,x 是作为一个非常大的矩阵放入的训练/测试集,w 是方程的权重。
%
之后的部分是我想要编写的内容,但我不知道如何在 MATLAB 代码中编写它。有什么想法吗?我正在尝试编写第二个函数:
函数 [err] = testerror(x,w,y) %err = sigma(max(0,-w*x*y))
w、x 和 y 具有与上述相同的值,err 是我的误差函数,我试图通过感知器的步骤将其最小化。
我正在尝试在感知器中创建一个步骤,通过在原始方程上使用梯度下降来降低误差百分比。有谁知道如何使用梯度下降来增加 w ,以便使用 if then 语句最小化误差函数?
如果这可以帮助您回答这些问题,我可以提供到目前为止的代码。
谢谢你!
编辑----------------------
好的,所以我仍在为此编写代码,并希望在完成后将其提出更完整的东西。我现在最大的问题是:
我有以下功能:
function [y] = testset(x,w)
y = sign(sum(x*w-threshold))
现在我知道我应该输入一个阈值,但无法弄清楚我应该输入什么作为阈值!有什么想法吗?
编辑----------------------------
这就是我到目前为止所拥有的。仍然需要对其进行更改,但我非常感谢您的意见,尤其是有关结构的意见,以及有关进行需要进行的更改的建议!
function [y] = Perceptron_Aviva(X,w)
y = sign(sum(X*w-1));
end
function [err] = testerror(X,w,y)
err = sum(max(0,-w*X*y));
end
%function [w] = perceptron(X,Y,w_init)
%w = w_init;
%end
%------------------------------
% input samples
X = X_train;
% output class [-1,+1];
Y = y_train;
% init weigth vector
w_init = zeros(size(X,1));
w = w_init;
%---------------------------------------------
loopcounter = 0
while abs(err) > 0.1 && loopcounter < 100
for j=1:size(X,1)
approx_y(j) = Perceptron_Aviva(X(j),w(j))
err = testerror(X(j),w(j),approx_y(j))
if err > 0 %wrong (structure is correct, test is wrong)
w(j) = w(j) - 0.1 %wrong
elseif err < 0 %wrong
w(j) = w(j) + 0.1 %wrong
end
% -----------
% if sign(w'*X(:,j)) ~= Y(j) %wrong decision?
% w = w + X(:,j) * Y(j); %then add (or subtract) this point to w
end
I am coding a perceptron to learn to categorize gender in pictures of faces. I am very very new to MATLAB, so I need a lot of help. I have a few questions:
I am trying to code for a function:
function [y] = testset(x,w) %y = sign(sigma(x*w-threshold))
where y is the predicted results, x is the training/testing set put in as a very large matrix, and w is weight on the equation. The part after the
%
is what I am trying to write, but I do not know how to write this in MATLAB code. Any ideas out there?I am trying to code a second function:
function [err] = testerror(x,w,y) %err = sigma(max(0,-w*x*y))
w, x, and y have the same values as stated above, and err is my function of error, which I am trying to minimize through the steps of the perceptron.
I am trying to create a step in my perceptron to lower the percent of error by using gradient descent on my original equation. Does anyone know how I can increment w using gradient descent in order to minimize the error function using an if then statement?
I can put up the code I have up till now if that would help you answer these questions.
Thank you!
edit--------------------------
OK, so I am still working on the code for this, and would like to put it up when I have something more complete. My biggest question right now is:
I have the following function:
function [y] = testset(x,w)
y = sign(sum(x*w-threshold))
Now I know that I am supposed to put a threshold in, but cannot figure out what I am supposed to put in as the threshold! any ideas out there?
edit----------------------------
this is what I have so far. Changes still need to be made to it, but I would appreciate input, especially regarding structure, and advice for making the changes that need to be made!
function [y] = Perceptron_Aviva(X,w)
y = sign(sum(X*w-1));
end
function [err] = testerror(X,w,y)
err = sum(max(0,-w*X*y));
end
%function [w] = perceptron(X,Y,w_init)
%w = w_init;
%end
%------------------------------
% input samples
X = X_train;
% output class [-1,+1];
Y = y_train;
% init weigth vector
w_init = zeros(size(X,1));
w = w_init;
%---------------------------------------------
loopcounter = 0
while abs(err) > 0.1 && loopcounter < 100
for j=1:size(X,1)
approx_y(j) = Perceptron_Aviva(X(j),w(j))
err = testerror(X(j),w(j),approx_y(j))
if err > 0 %wrong (structure is correct, test is wrong)
w(j) = w(j) - 0.1 %wrong
elseif err < 0 %wrong
w(j) = w(j) + 0.1 %wrong
end
% -----------
% if sign(w'*X(:,j)) ~= Y(j) %wrong decision?
% w = w + X(:,j) * Y(j); %then add (or subtract) this point to w
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以阅读我前段时间做的这个问题。
我使用 matlab 代码和函数感知器
,它是从代码 (@Itamar Katz) 调用的,就像(随机数据):
我想这可以给你一个想法来制作你所描述的功能。
将预期结果与实际结果(类)进行误差比较
you can read this question I did some time ago.
I uses a matlab code and a function perceptron
and it is called from code (@Itamar Katz) like (random data):
I guess this can give you an idea to make the functions you described.
To the error compare the expected result with the real result (class)
假设您的数据集是 X(数据点)和 Y(类标签)。
创建一个感知器。
如果您想创建 MLP,则:
其中 NN 是网络架构,即指定每个隐藏层神经元数量的数组。例如,
对应的网络在第一层有 5 个神经元,在第二层有 3 个神经元,在第三隐藏层有 2 个神经元。
Assume your dataset is X, the datapoins, and Y, the labels of the classes.
creates a perceptron.
If you want to create an MLP then:
where NN is the network architecture, i.e. an array that designates the number of neurons at each hidden layer. For example
will correspond to an network with 5 neurons at the first layers, 3 at the second and 2 a the third hidden layer.
你所说的阈值是机器学习术语中的偏差。这应该作为用户的输入,因为它在训练期间使用。
另外,我想知道为什么你不使用内置的 matlab 函数。即 newp 或 newff。例如
,然后您可以设置对象 ff 的属性来完成选择梯度下降等工作。
Well what you call threshold is the Bias in machine learning nomenclature. This should be left as an input for the user because it is used during training.
Also, I wonder why you are not using the builtin matlab functions. i.e newp or newff. e.g.
Then you can set the properties of the object ff to do your job for selecting gradient descent and so on.