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

📄 nneval_hzh.m

📁 利用神经网络预测时间序列
💻 M
字号:

% NNEVAL_hzh  增加了递归方式预测输出
% ------
%         Validation of ordinary feedforward neural networks.
%
% The predictions are compared to the true outputs, a histogram is shown
% for the prediction errors, and the auto-correlation of the prediction error
% is plotted.
%
% CALL:
%        [Yhat,E,NSSE] = nneval(NetDef,W1,W2,PHI,Y)
%
%
% INPUTS:
%        See for example one of the functions MARQ, RPE, BATBP, INCBP
%
% OUTPUTS:
%         Yhat - Network predictions
%         E    - Prediction errors
%         NSSE - Normalized sum of squared errors
%         tr   -递归预测输出
%         E1   -递归预测误差
%         PI1  -递归Normalized sum of squared errors
% Programmed by : Magnus Norgaard, IAU/IMM Technical University of Denmark
% LastEditDate  : June 15, 1997
 function [y2,E,PI,tr,E1,PI1] = nneval_hzh(NetDef,W1,W2,PHI,Y,noplots)
                                      %PHI为检验时的样本输入,Y为检验时的样本期望输出

%PHI=test(:,1:nlag)';Y=test(:,nlag+1)';                                      
[outputs,N] = size(Y);
[layers,dummy] = size(NetDef);        % Number of hidden layers
L_hidden = find(NetDef(1,:)=='L')';   % Location of linear hidden units
H_hidden = find(NetDef(1,:)=='H')';   % Location of tanh hidden units
L_output = find(NetDef(2,:)=='L')';   % Location of linear output units
H_output = find(NetDef(2,:)=='H')';   % Location of tanh output units
[hidden,inputs] = size(W1);
inputs   = inputs-1;
E        = zeros(outputs,N);
y1       = zeros(hidden,N);
y2       = zeros(outputs,N);

r_y1     = zeros(hidden,1);
r_y2     = zeros(outputs,1);

% ---  Compute network output ---直接预测
    h1 = W1*[PHI;ones(1,N)];  
    y1(H_hidden,:) = pmntanh(h1(H_hidden,:));
    y1(L_hidden,:) = h1(L_hidden,:);
    
    h2 = W2*[y1;ones(1,N)];
    y2(H_output,:) = pmntanh(h2(H_output,:));
    y2(L_output,:) = h2(L_output,:);

    E            = Y - y2;                     % Test error
    PI           = sum(sum(E.*E))/(2*N);       % Sum of squared errors 

%-----递归预测,只用训练样本的最后一列数据-----------------
    
%   lag=test(1,1:nlag);   % last nlag of training data
    lag=PHI(1:inputs,1);
  for i=1:length(PHI), % recursive prediction using only last nlag training data!
                     %用训练数据的最后几个延迟数据进行递归预测,tr是递归预测的结果
     r_h1 = W1*[lag;1];
     r_y1(H_hidden,:) = pmntanh(r_h1(H_hidden,:));
     r_y1(L_hidden,:) = r_h1(L_hidden,:);
     
     r_h2 = W2*[r_y1;1];
     r_y2(H_output,:) = pmntanh(r_h2(H_output,:));
     r_y2(L_output,:) = r_h2(L_output,:);
     
     tr(i)=r_y2;         % i-th prediction
     lag=[lag(2:inputs); tr(i)];
                     %   tr(i)=lag*a';  % i-th prediction
%   lag=[lag(2:nlag) tr(i)];
  end
      E1            = Y - tr;                     % Test error
      PI1           = sum(sum(E1.*E1))/(2*N);       % Sum of squared errors 
    
%-----&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&-----------------   
 if nargin~=6,
  % ---------- Output, Prediction and Prediction error ----------
  si=figure-1;
  for i=1:outputs
    figure(si+i);
    subplot(211)
    plot(Y(i,:),'bx'); hold on
    plot(y2(i,:),'ro');hold on
    plot(tr(i,:));hold off
    if outputs==1,
      title('Measurement data = x     Network output=o')
    else
      title(['Measurement data = x     Network output=o      (output # ',num2str(i) ')'])
    end
    grid

    subplot(212)
    plot(E(i,:));
    title('Prediction error')
    grid
    subplot(111)
    drawnow
  end

  % TEMP-TEMP-TEMP
  % Auto correlation function of error
  for i=1:outputs
    figure(si+outputs+i)
    subplot(211)
    M=min(25,N-1);
    Eauto=xcorr(E(i,:),'coeff');
    Eauto=Eauto(N:2*N-1);
    conf=1.96/sqrt(N);
    plot([0:M],Eauto(1:M+1),'b-'); hold on
    plot([0 M],[conf -conf;conf -conf],'r--');hold off
    xlabel('lag')
    if outputs==1,
      title('Auto correlation function of prediction error')
    else
      title(['Auto correlation function of prediction error      (output # ',num2str(i) ')'])
    end
    grid
    subplot(111)
    drawnow

    % Histogram over errors
    subplot(212)
    hist(E(i,:),20)
    title('Histogram over prediction errors')
    subplot(111)
    drawnow
  end
end

⌨️ 快捷键说明

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