⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 learn_perceptron_batch.m

📁 Perceptron matlab code
💻 M
字号:
function [beta_hat,beta_zero_hat] = Learn_Perceptron_Batch(X,Y,rho,itr_max)

[N,d] = size(X);

beta_hat = zeros(d,1);
beta_hat(1) = 1;

beta_zero_hat = -0.5;


cont = 1;
itr = 0;

PLOT = 1;
if PLOT == 1
    max_x = max(X);
    min_x = min(X);
    X_plot = [min_x;max_x];
    ind_1 = find(Y >0);
    ind_2 = find(Y<=0);
end


while cont == 1
    num_miss = 0;
    itr = itr + 1;
    val_all = (beta_zero_hat + X * beta_hat).*Y;
    ind_e = find(val_all<=0);
    if ~isempty(ind_e)
        val_itr(itr) = sum(val_all(ind_e));
        num_miss = num_miss + 1;
        beta_hat = beta_hat + rho * sum(Y(ind_e,ones(1,d)).*X(ind_e,:))';
        beta_zero_hat = beta_zero_hat + rho * sum(Y(ind_e));
        if PLOT == 1
            figure(1000);
            plot(X(ind_1,1),X(ind_1,2),'rs');
            hold on
            plot(X(ind_2,1),X(ind_2,2),'bo');
            if abs(beta_hat(2)) > abs(beta_hat(1))
                zx = X_plot(:,1);
                yy = -(zx * beta_hat(1) + beta_zero_hat)/beta_hat(2);
                plot(zx,yy,'r-');

            else
                zx = X_plot(:,2);
                yy = -(zx * beta_hat(2) + beta_zero_hat)/beta_hat(1);
                plot(yy,zx,'r-');
            end
            axis([min_x(1) max_x(1) min_x(2) max_x(2)]);
            hold off;
            pause
        end

    else
        val_itr(itr) = 0;
    end
    if num_miss == 0
        if PLOT == 1
            figure(1000);
            plot(X(ind_1,1),X(ind_1,2),'rs');
            hold on
            plot(X(ind_2,1),X(ind_2,2),'bo');
            if abs(beta_hat(2)) > abs(beta_hat(1))
                zx = X_plot(:,1);
                yy = -(zx * beta_hat(1) + beta_zero_hat)/beta_hat(2);
                plot(zx,yy,'r-');

            else
                zx = X_plot(:,2);
                yy = -(zx * beta_hat(2) + beta_zero_hat)/beta_hat(1);
                plot(yy,zx,'r-');
            end
            axis([min_x(1) max_x(1) min_x(2) max_x(2)]);
            hold off;
        end


          cont = 0;
        
    end
    
    
    if itr > itr_max
        cont = 0;
    end

end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -