www.gusucode.com > nnguis 工具箱 matlab 源码程序 > nnguis/+nnet/+genscript/NARScriptGenerator.m

    classdef (Sealed) NARScriptGenerator < nnet.genscript.ScriptGenerator
    % NARSCRIPTGENERATOR - This class is used to generate training scripts
    % for NAR networks in "ntstool" when clicking the "Simple Script" or
    % "Advanced Script" buttons on the "Save Results" page.
    
    % Copyright 2014 The MathWorks, Inc.
    
    properties(Access = private)
        FeedbackName
        TimestepInCell
        SampleByColumn
        TrainPercent
        ValidatePercent
        TestPercent
        HiddenLayerSize
        NumDelayLines
        PredictAhead
        TrainingFunction
        PerformFunction
    end
    
    properties(Access = private, Constant)
        AdvancedCodeForProcessFunctionsForNARNetwork = {
            '% Choose Feedback Pre/Post-Processing Functions', ...
            '% Settings for feedback input are automatically applied to feedback output', ...
            '% For a list of all processing functions type: help nnprocess', ...
            'net.input.processFcns = {''removeconstantrows'',''mapminmax''};', ...
            ''
            };
        AdvancedCodeForMultiStepPredictionForNARNetwork = {
            '% Multi-step Prediction', ...
            '% Sometimes it is useful to simulate a network in open-loop form for as', ...
            '% long as there is known data T, and then switch to closed-loop to perform', ...
            '% multistep prediction. Here The open-loop network is simulated on the', ...
            '% known output series, then the network and its final delay states are', ...
            '% converted to closed-loop form to produce predictions for 5 more', ...
            '% timesteps.', ...
            '[x1,xio,aio,t] = preparets(net,{},{},T);', ...
            '[y1,xfo,afo] = net(x1,xio,aio);', ...
            '[netc,xic,aic] = closeloop(net,xfo,afo);', ...
            '[y2,xfc,afc] = netc(cell(0,5),xic,aic);', ...
            '% Further predictions can be made by continuing simulation starting with', ...
            '% the final input and layer delay states, xfc and afc.', ...
            ''
            };
    end
    
    properties(Access = private, Dependent)
        CodeForInputDataForNARNetwork
        CodeForDataDefinitionForNARNetwork
        CodeForCreationOfNARNetwork
    end
    
    methods
        function this = NARScriptGenerator( state )
            this.FeedbackName = state.feedbackName;
            this.TimestepInCell = state.timeInCell;
            this.SampleByColumn = state.sampleByColumn;
            this.TrainPercent = state.net2.divideParam.trainRatio*100;
            this.ValidatePercent = state.net2.divideParam.valRatio*100;
            this.TestPercent = state.net2.divideParam.testRatio*100;
            this.HiddenLayerSize = state.layerSize;
            this.NumDelayLines = state.delaySize;
            this.TrainingFunction = state.net2.trainFcn;
            this.PerformFunction = state.net2.performFcn;
        end
        
        function code = generateSimpleScript(this)
            code = [
                this.CodeForInputDataForNARNetwork, ...
                this.generateCodeForTrainingFunction(this.TrainingFunction), ...
                this.CodeForCreationOfNARNetwork, ...
                this.generateCodeForDataPreparation('{},{}'), ...
                this.generateCodeForDataDivision(this.TrainPercent, this.TestPercent, this.ValidatePercent, 'time', false), ...
                this.CodeForTrainingAndTestingDynamicNetwork, ...
                this.CodeForViewingNetwork, ...
                this.CommentForPlots, ...
                this.CodeForPlotsForDynamicNetwork, ...
                this.generateCodeForClosedLoop('{}'), ...
                this.generateCodeForStepAheadPrediction('y', '{},{}')
                ];
        end
        
        function code = generateAdvancedScript(this)
            code = [
                this.CodeForInputDataForNARNetwork, ...
                this.generateCodeForTrainingFunction(this.TrainingFunction), ...
                this.CodeForCreationOfNARNetwork, ...
                this.AdvancedCodeForProcessFunctionsForNARNetwork, ...
                this.generateCodeForDataPreparation('{},{}'), ...
                this.generateCodeForDataDivision(this.TrainPercent, this.TestPercent, this.ValidatePercent, 'time', true), ...
                this.generateAdvancedCodeForTrainingDynamicNetwork(this.PerformFunction), ...
                this.CodeForTrainingAndTestingDynamicNetwork, ...
                this.AdvancedCodeForRecalculatedPerformanceForDynamicNetwork, ...
                this.CodeForViewingNetwork, ...
                this.CommentForPlots, ...
                this.CodeForPlotsForDynamicNetwork, ...
                this.generateCodeForClosedLoop('{}'), ...
                this.AdvancedCodeForMultiStepPredictionForNARNetwork, ...
                this.generateCodeForStepAheadPrediction('y', '{},{}'), ...
                this.AdvancedCodeForDeploymentForNAROrTimeDelayNetwork
                ];
        end
        
        function code = get.CodeForInputDataForNARNetwork(this)
            code = [
                {
                '% Solve an Autoregression Time-Series Problem with a NAR Neural Network', ...
                '% Script generated by Neural Time Series app', ...
                this.generateCommentForDateOfCreation(), ...
                '%', ...
                '% This script assumes this variable is defined:', ...
                '%', ...
                ['%   ' this.FeedbackName ' - feedback time series.'], ...
                ''
                }, ...
                this.CodeForDataDefinitionForNARNetwork, ...
                {''}
                ];
        end
        
        function code = get.CodeForDataDefinitionForNARNetwork(this)
            if(this.SampleByColumn && this.TimestepInCell)
                code = ['T = ' this.FeedbackName ';'];
            else
                code = ['T = tonndata(' this.FeedbackName ',' mat2str(this.SampleByColumn) ',' mat2str(this.TimestepInCell) ');'];
            end
        end
        
        function code = get.CodeForCreationOfNARNetwork(this)
            code = {
                '% Create a Nonlinear Autoregressive Network', ...
                ['feedbackDelays = 1:' mat2str(this.NumDelayLines) ';'], ...
                ['hiddenLayerSize = ' mat2str(this.HiddenLayerSize) ';'], ...
                'net = narnet(feedbackDelays,hiddenLayerSize,''open'',trainFcn);', ...
                ''
                };
        end
    end
end