1. 程式人生 > >Spam classification(垃圾郵件分類)—SVM、Logistic分類、SEA-Logistic(深度網路)分類

Spam classification(垃圾郵件分類)—SVM、Logistic分類、SEA-Logistic(深度網路)分類

<span style="color:#3333ff;font-size:18px; font-weight: bold; font-family: 'Times New Roman';">%STEP 2: 初始化引數和load資料
</span><span style="font-family:Times New Roman;font-size:14px;">clear all;
clc;
load('D:\機器學習課程\作業三\spamData.mat');
Xtrain=Xtrain';
ytrain(ytrain==0) = 2;
Xtest=Xtest';
ytest(ytest == 0) = 2; % Remap 0 to 10
inputSize  = 57;
numLabels  = 2;
a=[57 50 45 40 35 30 25 20 15 10 5];
sparsityParam = 0.1;
lambda = 3e-3;       % weight decay parameter
beta = 3;            % weight of sparsity penalty term
numClasses = 2;     % Number of classes (MNIST images fall into 10 classes)
lambda = 1e-4; % Weight decay parameter
%% ======================================================================
%STEP 2: 訓練自學習層SAE
for i=1:11
    hiddenSize = a(i);
    theta = initializeParameters(hiddenSize, inputSize);
    %-------------------------------------------------------------------
    opttheta = theta;
    addpath minFunc/
    options.Method = 'lbfgs';
    options.maxIter = 400;
    options.display = 'on';
    [opttheta, loss] = minFunc( @(p) sparseAutoencoderCost(p, ...
        inputSize, hiddenSize, ...
        lambda, sparsityParam, ...
        beta, Xtrain), ...
        theta, options);
    trainFeatures = feedForwardAutoencoder(opttheta, hiddenSize, inputSize, ...
        Xtrain);
    %% ================================================
    %STEP 3: 訓練Softmax分類器
    saeSoftmaxTheta = 0.005 * randn(hiddenSize * numClasses, 1);
    softmaxLambda = 1e-4;
    numClasses = 2;
    softoptions = struct;
    softoptions.maxIter = 500;
    softmaxModel = softmaxTrain(hiddenSize,numClasses,softmaxLambda,...
        trainFeatures,ytrain,softoptions);
    theta_new = softmaxModel.optTheta(:);
    %% ============================================================
    stack = cell(1,1);
    stack{1}.w = reshape(opttheta(1:hiddenSize * inputSize), hiddenSize, inputSize);
    stack{1}.b =opttheta(2*hiddenSize*inputSize+1:2*hiddenSize*inputSize+hiddenSize);
    [stackparams, netconfig] = stack2params(stack);
    stackedAETheta = [theta_new;stackparams];
    addpath minFunc/;
    options = struct;
    options.Method = 'lbfgs';
    options.maxIter = 400;
    options.display = 'on';
    [stackedAEOptTheta,cost] =  minFunc(@(p)stackedAECost(p,inputSize,hiddenSize,numClasses, netconfig,lambda, Xtrain, ytrain),stackedAETheta,options);
    %% =================================================================
    %STEP 4: 測試
    [pred] = stackedAEPredict(stackedAEOptTheta, inputSize, hiddenSize, ...
        numClasses, netconfig, Xtest);
    acc = mean(ytest(:) == pred(:));
    fprintf('Accuracy = %0.3f%%\n', acc * 100);
    result(i)=acc * 100;
end</span><span style="color:#3333ff;font-size:18px; font-weight: bold; font-family: 'Times New Roman';">
</span>
stackedAEPredict.m