神经网络框架

神经网络算法可以搭建模型做数据预测

1. 神经网络结构  ([4,4,4,2],四层网络结构)

 

 

 

 

 

 

 

2. 示例代码(可自定义网络结构)

2.1 训练函数

function [w,J_cost] = ccTrain(weight,x,y)
% 多层神经网络,多输出
% 注:初始输入的权值矩阵第一层神经元个数必须等于输入数据x的特征个数
% 利用神经网络搭建模型来预测鲍鱼的年龄
% 数据来源:load abalone_dataset.mat
%% 1.初始化准备
[Mx,~] = size(x);
nlayers = length(weight);

eval(['a.a',num2str(1),' = ','[];']);
for n = 2:nlayers
    initialweight = rand(weight(n),weight(n-1));
    initialdw = zeros(weight(n),weight(n-1));
    initialz  = [];
    initiala  = [];
    initiald  = [];
    initiale  = [];
    
    eval(['w.w',num2str(n-1),' = ','initialweight;']);
    eval(['dw.dw',num2str(n-1),' = ','initialdw;']);
    eval(['z.z',num2str(n),' = ','initialz;']);
    eval(['a.a',num2str(n),' = ','initiala;']);
    eval(['delta.d',num2str(n),' = ','initiald;']);
    eval(['e.e',num2str(n),' = ','initiale;']);
end
weightnames = fieldnames(w);
dwnames = fieldnames(dw);
znames = fieldnames(z);
anames = fieldnames(a);
deltanames = fieldnames(delta);
enames = fieldnames(e);
%% 2.参数设置与迭代
alpha = 0.8;
J_cost = [];
J = [];

%% 3. 全局迭代  
Maxiteration = 10000;
for iteration = 1:Maxiteration
    %%  3.1 dw, a, z, delta 初始化
    eval(['a.a',num2str(1),' = ','[];']);
    for n1 = 2:nlayers
        initialdw = zeros(weight(n1),weight(n1-1));
        initialz  = [];
        initiala  = [];
        initiald  = [];
        eval(['dw.dw',num2str(n1-1),' = ','initialdw;']);
        eval(['z.z',num2str(n1),' = ','initialz;']);
        eval(['a.a',num2str(n1),' = ','initiala;']);
        eval(['delta.d',num2str(n1),' = ','initiald;']);
    end
    %% 3.2 批次处理
    for sample = 1:Mx
        
        zz = [];    % 前向数据传递
        aa = x(sample,:)';              % aa = x 列向量
        a = setfield(a,anames{1},aa);   % a.a1 = x
        for layer = 2:nlayers                           % layer = 4 时
            ww = getfield(w,weightnames{layer-1});      % ww = w.w3
            zz = ww * aa;                               % zz = ww * aa
            aa = sigmoid(zz);                           % aa = sigmoid(zz)
            z = setfield(z,znames{layer-1},zz);         % z.z4 = zz
            a = setfield(a,anames{layer},aa);           % a.a4 = aa         
        end      
        

    
        for layer = nlayers:-1:2    % 反向误差传播

             if layer == nlayers                        % layer = 4时
                 aa = getfield(a,anames{layer});        % aa = a.a4
                 ee0 = y(sample,:)' - aa;                % ee = error
                 delta_delta = aa.*(1-aa) .* ee0;          % delta_delta = aa.*(1-aa) .* error
                 e = setfield(e,enames{layer-1},ee0);      % e.e4 = ee
                 delta = setfield(delta,deltanames{layer-1},delta_delta);
                 ww = getfield(w,weightnames{layer-1});  % ww = w.w3
             else
                 aa = getfield(a,anames{layer});       % aa = a.a3
                 ee = transpose(ww) * delta_delta;       % ee = ww' * delta_delta
                 delta_delta = aa.*(1-aa) .* ee;         % delta_delta = aa.*(1-aa) .* ee
                 e = setfield(e,enames{layer-1},ee);     % e.e3 = ee
                 delta = setfield(delta,deltanames{layer-1},delta_delta); % delta.d2 = delta_delta
                 ww = getfield(w,weightnames{layer-1});  % ww = w.w3
             end
           %% 一个批次(包含所有值)中的权值误差累加统计 % layer = 4时
            ddww = getfield(dw,dwnames{layer - 1});     % ddww = dw.dw3
            aaa = getfield(a,anames{layer-1});
            ddw = alpha * delta_delta .* transpose(aaa);            % ddw = alpha * delta_delta .* aa
            dw = setfield(dw,dwnames{layer-1},ddww + ddw); % dw.dw2 = ddww + ddw
        end
        error = ee0;
        J(sample) = power(sum(error.*error),0.5);   % 误差计算
        J_cost(iteration,1) = power(sum(J.*J),0.5);
    end
    
    for layer = 1:nlayers-1                             % 调整权值 layer = 3时
        dw_average = getfield(dw,dwnames{layer})/Mx;    % dw_average = dw.dw3/110
        w_needed = getfield(w,weightnames{layer});      % w_needeed = w.w3
        w = setfield(w,weightnames{layer},dw_average + w_needed); % w = dw_average + w_needed
    end
    
    if rem(iteration,100) == 0
       fprintf('训练进度:%d/%d,约为:%d %%\n',iteration,Maxiteration,100*iteration/Maxiteration);
    end
end
end

 

 

    2.2 预测函数

function yy = ccPrediction(w,x)

% validation = [rdata(41:50,:);rdata(121:150,:)];
% x = validation(:,1:4);
[M,~] = size(x);
names = fieldnames(w);
nlayers = length(names);
for i = 1:M
    a = x(i,:)';
    for j = 1:nlayers
        ww = getfield(w,names{j});
        z = ww * a;
        a = sigmoid(z);
    end
    yy(i,:) = a;
end
end

 

 

2.3 训练函数和预测函数中调用的激活函数

 

function y = sigmoid(x)
y = 1./(1 + exp(-x));
end

 

posted @ 2019-10-19 17:02  CarreyB  阅读(1333)  评论(0编辑  收藏  举报