在 MATLAB 中创建列车感知器以进行性别分类

发布于 2024-10-27 02:26:01 字数 1926 浏览 4 评论 0原文

我正在编写一个感知器来学习对面部图片中的性别进行分类。我对 MATLAB 非常陌生,所以我需要很多帮助。我有几个问题:

  1. 我正在尝试编写函数代码:

    函数 [y] = 测试集(x,w)  
    %y = 符号(sigma(x*w-阈值))
    

    其中 y 是预测结果,x 是作为一个非常大的矩阵放入的训练/测试集,w 是方程的权重。 % 之后的部分是我想要编写的内容,但我不知道如何在 MATLAB 代码中编写它。有什么想法吗?

  2. 我正在尝试编写第二个函数:

    函数 [err] = testerror(x,w,y)  
    %err = sigma(max(0,-w*x*y))
    

    w、x 和 y 具有与上述相同的值,err 是我的误差函数,我试图通过感知器的步骤将其最小化。

  3. 我正在尝试在感知器中创建一个步骤,通过在原始方程上使用梯度下降来降低误差百分比。有谁知道如何使用梯度下降来增加 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:

  1. 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?

  2. 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.

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我还不会笑 2024-11-03 02:26:01

你可以阅读我前段时间做的这个问题

我使用 matlab 代码和函数感知器

function [w] = perceptron(X,Y,w_init)

w = w_init;
for iteration = 1 : 100  %<- in practice, use some stopping criterion!
  for ii = 1 : size(X,2)         %cycle through training set
    if sign(w'*X(:,ii)) ~= Y(ii) %wrong decision?
      w = w + X(:,ii) * Y(ii);   %then add (or subtract) this point to w
    end
  end
  sum(sign(w'*X)~=Y)/size(X,2)   %show misclassification rate
end

,它是从代码 (@Itamar Katz) 调用的,就像(随机数据):

% input samples
X1=[rand(1,100);rand(1,100);ones(1,100)];   % class '+1'
X2=[rand(1,100);1+rand(1,100);ones(1,100)]; % class '-1'
X=[X1,X2];

% output class [-1,+1];
Y=[-ones(1,100),ones(1,100)];

% init weigth vector
w=[.5 .5 .5]';

% call perceptron
wtag=perceptron(X,Y,w);
% predict
ytag=wtag'*X;


% plot prediction over origianl data
figure;hold on
plot(X1(1,:),X1(2,:),'b.')
plot(X2(1,:),X2(2,:),'r.')

plot(X(1,ytag<0),X(2,ytag<0),'bo')
plot(X(1,ytag>0),X(2,ytag>0),'ro')
legend('class -1','class +1','pred -1','pred +1')

我想这可以给你一个想法来制作你所描述的功能。
将预期结果与实际结果(类)进行误差比较

you can read this question I did some time ago.

I uses a matlab code and a function perceptron

function [w] = perceptron(X,Y,w_init)

w = w_init;
for iteration = 1 : 100  %<- in practice, use some stopping criterion!
  for ii = 1 : size(X,2)         %cycle through training set
    if sign(w'*X(:,ii)) ~= Y(ii) %wrong decision?
      w = w + X(:,ii) * Y(ii);   %then add (or subtract) this point to w
    end
  end
  sum(sign(w'*X)~=Y)/size(X,2)   %show misclassification rate
end

and it is called from code (@Itamar Katz) like (random data):

% input samples
X1=[rand(1,100);rand(1,100);ones(1,100)];   % class '+1'
X2=[rand(1,100);1+rand(1,100);ones(1,100)]; % class '-1'
X=[X1,X2];

% output class [-1,+1];
Y=[-ones(1,100),ones(1,100)];

% init weigth vector
w=[.5 .5 .5]';

% call perceptron
wtag=perceptron(X,Y,w);
% predict
ytag=wtag'*X;


% plot prediction over origianl data
figure;hold on
plot(X1(1,:),X1(2,:),'b.')
plot(X2(1,:),X2(2,:),'r.')

plot(X(1,ytag<0),X(2,ytag<0),'bo')
plot(X(1,ytag>0),X(2,ytag>0),'ro')
legend('class -1','class +1','pred -1','pred +1')

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)

心房敞 2024-11-03 02:26:01

假设您的数据集是 X(数据点)和 Y(类标签)。

f=newp(X,Y)

创建一个感知器。

如果您想创建 MLP,则:

f=newff(X,Y,NN)

其中 NN 是网络架构,即指定每个隐藏层神经元数量的数组。例如,

NN=[5 3 2]

对应的网络在第一层有 5 个神经元,在第二层有 3 个神经元,在第三隐藏层有 2 个神经元。

Assume your dataset is X, the datapoins, and Y, the labels of the classes.

f=newp(X,Y)

creates a perceptron.

If you want to create an MLP then:

f=newff(X,Y,NN)

where NN is the network architecture, i.e. an array that designates the number of neurons at each hidden layer. For example

NN=[5 3 2]

will correspond to an network with 5 neurons at the first layers, 3 at the second and 2 a the third hidden layer.

甜扑 2024-11-03 02:26:01

你所说的阈值是机器学习术语中的偏差。这应该作为用户的输入,因为它在训练期间使用。

另外,我想知道为什么你不使用内置的 matlab 函数。即 newp 或 newff。例如

ff=newp(X,Y)

,然后您可以设置对象 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.

ff=newp(X,Y)

Then you can set the properties of the object ff to do your job for selecting gradient descent and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文