/// <summary> /// Prepares input for regression stage of State Machine training. /// All input patterns are processed by internal reservoirs and the corresponding network predictors are recorded. /// </summary> /// <param name="dataSet"> /// The bundle containing known sample input patterns and desired output vectors /// </param> /// <param name="informativeCallback"> /// Function to be called after each processed input. /// </param> /// <param name="userObject"> /// The user object to be passed to informativeCallback. /// </param> public RegressionStageInput PrepareRegressionStageInput(PatternBundle dataSet, PredictorsCollectionCallbackDelegate informativeCallback = null, Object userObject = null ) { if (_settings.InputConfig.FeedingType == CommonEnums.InputFeedingType.Continuous) { throw new Exception("This version of PrepareRegressionStageInput function is not useable for continuous input feeding."); } //RegressionStageInput allocation RegressionStageInput rsi = new RegressionStageInput { PredictorsCollection = new List <double[]>(dataSet.InputPatternCollection.Count), IdealOutputsCollection = new List <double[]>(dataSet.OutputVectorCollection.Count) }; //Reset the internal states and statistics Reset(true); //Collection for (int dataSetIdx = 0; dataSetIdx < dataSet.InputPatternCollection.Count; dataSetIdx++) { //Push input data into the network double[] predictors = PushInput(dataSet.InputPatternCollection[dataSetIdx]); rsi.PredictorsCollection.Add(predictors); //Add desired outputs rsi.IdealOutputsCollection.Add(dataSet.OutputVectorCollection[dataSetIdx]); //Informative callback informativeCallback?.Invoke(dataSet.InputPatternCollection.Count, dataSetIdx + 1, userObject); } //Collect reservoirs statistics rsi.ReservoirStatCollection = CollectReservoirInstancesStatatistics(); return(rsi); }
/// <summary> /// Prepares input for Readout Layer training. /// All input patterns are processed by internal reservoirs and the corresponding network predictors are recorded. /// </summary> /// <param name="patternBundle"> /// The bundle containing known sample input patterns and desired output vectors /// </param> /// <param name="informativeCallback"> /// Function to be called after each processed input. /// </param> /// <param name="userObject"> /// The user object to be passed to informativeCallback. /// </param> public VectorBundle InitializeAndPreprocessBundle(PatternBundle patternBundle, PredictorsCollectionCallbackDelegate informativeCallback = null, Object userObject = null ) { //Check correctness if (_settings.InputConfig.FeedingType == CommonEnums.InputFeedingType.Continuous) { throw new Exception("Called incorrect version of InitializeAndPreprocessBundle function for continuous input feeding."); } //Reset the internal states and also statistics Reset(true); //Initialize normalizers and normalize input data List <List <double[]> > nrmInputPatternCollection = NormalizeInputPatternCollection(patternBundle.InputPatternCollection); //Allocate output bundle VectorBundle outputBundle = new VectorBundle(patternBundle.InputPatternCollection.Count); //Collection for (int dataSetIdx = 0; dataSetIdx < nrmInputPatternCollection.Count; dataSetIdx++) { //Push input data into the network double[] predictors = PushInput(nrmInputPatternCollection[dataSetIdx], true); outputBundle.InputVectorCollection.Add(predictors); //Add desired outputs outputBundle.OutputVectorCollection.Add(patternBundle.OutputVectorCollection[dataSetIdx]); //Informative callback informativeCallback?.Invoke(patternBundle.InputPatternCollection.Count, dataSetIdx + 1, userObject); } return(outputBundle); }
/// <summary> /// Prepares input for regression stage of State Machine training for the classification or hybrid task type. /// All input patterns are processed by internal reservoirs and the corresponding network predictors are recorded. /// </summary> /// <param name="dataSet"> /// The bundle containing known sample input patterns and desired output vectors /// </param> /// <param name="informativeCallback"> /// Function to be called after each processed input. /// </param> /// <param name="userObject"> /// The user object to be passed to informativeCallback. /// </param> public RegressionStageInput PrepareRegressionStageInput(PatternBundle dataSet, PredictorsCollectionCallbackDelegate informativeCallback = null, Object userObject = null ) { if (_settings.TaskType == CommonEnums.TaskType.Prediction) { throw new Exception("This version of PrepareRegressionStageInput function is useable only for the classification or hybrid task type."); } //RegressionStageInput allocation RegressionStageInput rsi = new RegressionStageInput(); rsi.PredictorsCollection = new List <double[]>(dataSet.InputPatternCollection.Count); rsi.IdealOutputsCollection = new List <double[]>(dataSet.OutputVectorCollection.Count); //Collection for (int dataSetIdx = 0; dataSetIdx < dataSet.InputPatternCollection.Count; dataSetIdx++) { //Push input data into the network double[] predictors = PushInput(dataSet.InputPatternCollection[dataSetIdx]); rsi.PredictorsCollection.Add(predictors); //Add desired outputs rsi.IdealOutputsCollection.Add(dataSet.OutputVectorCollection[dataSetIdx]); //Informative callback if (informativeCallback != null) { informativeCallback(dataSet.InputPatternCollection.Count, dataSetIdx + 1, userObject); } } //Collect reservoirs statistics rsi.ReservoirStatCollection = CollectReservoirInstancesStatatistics(); return(rsi); }
/// <summary> /// Prepares input for regression stage of State Machine training. /// All input patterns are processed by internal reservoirs and the corresponding network predictors are recorded. /// </summary> /// <param name="patternBundle"> /// The bundle containing known sample input patterns and desired output vectors /// </param> /// <param name="informativeCallback"> /// Function to be called after each processed input. /// </param> /// <param name="userObject"> /// The user object to be passed to informativeCallback. /// </param> public RegressionInput PrepareRegressionData(PatternBundle patternBundle, NeuralPreprocessor.PredictorsCollectionCallbackDelegate informativeCallback = null, Object userObject = null ) { return(new RegressionInput(NP.InitializeAndPreprocessBundle(patternBundle, informativeCallback, userObject), NP.CollectStatatistics(), NP.NumOfNeurons, NP.NumOfInternalSynapses )); }
/// <summary> /// Prepares input for regression stage of State Machine training. /// All input patterns are processed by internal reservoirs and the corresponding network predictors are recorded. /// </summary> /// <param name="patternBundle"> /// The bundle containing known sample input patterns and desired output vectors /// </param> /// <param name="informativeCallback"> /// Function to be called after each processed input. /// </param> /// <param name="userObject"> /// The user object to be passed to informativeCallback. /// </param> public RegressionInput PrepareRegressionData(PatternBundle patternBundle, NeuralPreprocessor.PredictorsCollectionCallbackDelegate informativeCallback = null, Object userObject = null ) { VectorBundle preprocessedData = NP.InitializeAndPreprocessBundle(patternBundle, informativeCallback, userObject); InitPredictorsGeneralSwitches(preprocessedData.InputVectorCollection); return(new RegressionInput(preprocessedData, NP.CollectStatatistics(), NP.NumOfNeurons, NP.NumOfInternalSynapses, NumOfUnusedPredictors )); }
/// <summary> /// Performs specified demo case. /// Loads and prepares sample data, trains State Machine and displayes results /// </summary> /// <param name="log">Into this interface are written output messages</param> /// <param name="demoCaseParams">An instance of DemoSettings.CaseSettings to be performed</param> public static void PerformDemoCase(IOutputLog log, DemoSettings.CaseSettings demoCaseParams) { //For demo purposes is allowed only the normalization range (-1, 1) Interval normRange = new Interval(-1, 1); log.Write(" Performing demo case " + demoCaseParams.Name, false); //Bundle normalizer object BundleNormalizer bundleNormalizer = null; //Prediction input vector (relevant only for time series prediction task) double[] predictionInputVector = null; //Instantiate an State Machine StateMachine stateMachine = new StateMachine(demoCaseParams.stateMachineCfg, normRange); //Prepare regression stage input object log.Write(" ", false); StateMachine.RegressionStageInput rsi = null; if (demoCaseParams.stateMachineCfg.TaskType == CommonEnums.TaskType.Prediction) { //Time series prediction task //Load data bundle from csv file TimeSeriesBundle data = TimeSeriesDataLoader.Load(demoCaseParams.FileName, demoCaseParams.stateMachineCfg.InputFieldNameCollection, demoCaseParams.stateMachineCfg.ReadoutLayerConfig.OutputFieldNameCollection, normRange, demoCaseParams.NormalizerReserveRatio, true, demoCaseParams.SingleNormalizer, out bundleNormalizer, out predictionInputVector ); rsi = stateMachine.PrepareRegressionStageInput(data, demoCaseParams.NumOfBootSamples, PredictorsCollectionCallback, log); } else { //Classification or hybrid task //Load data bundle from csv file PatternBundle data = PatternDataLoader.Load(demoCaseParams.stateMachineCfg.TaskType == CommonEnums.TaskType.Classification, demoCaseParams.FileName, demoCaseParams.stateMachineCfg.InputFieldNameCollection, demoCaseParams.stateMachineCfg.ReadoutLayerConfig.OutputFieldNameCollection, normRange, demoCaseParams.NormalizerReserveRatio, true, out bundleNormalizer ); rsi = stateMachine.PrepareRegressionStageInput(data, PredictorsCollectionCallback, log); } //Report reservoirs statistics ReportReservoirsStatistics(rsi.ReservoirStatCollection, log); //Regression stage log.Write(" Regression stage", false); //Training - State Machine regression stage ValidationBundle vb = stateMachine.RegressionStage(rsi, RegressionControl, log); //Perform prediction if the task type is Prediction double[] predictionOutputVector = null; if (demoCaseParams.stateMachineCfg.TaskType == CommonEnums.TaskType.Prediction) { predictionOutputVector = stateMachine.Compute(predictionInputVector); //Values are normalized so they have to be denormalized bundleNormalizer.NaturalizeOutputVector(predictionOutputVector); } //Display results //Report training (regression) results and prediction log.Write(" Results", false); List <ReadoutLayer.ClusterErrStatistics> clusterErrStatisticsCollection = stateMachine.ClusterErrStatisticsCollection; //Classification results for (int outputIdx = 0; outputIdx < demoCaseParams.stateMachineCfg.ReadoutLayerConfig.OutputFieldNameCollection.Count; outputIdx++) { ReadoutLayer.ClusterErrStatistics ces = clusterErrStatisticsCollection[outputIdx]; if (demoCaseParams.stateMachineCfg.TaskType == CommonEnums.TaskType.Classification) { //Classification task report log.Write(" OutputField: " + demoCaseParams.stateMachineCfg.ReadoutLayerConfig.OutputFieldNameCollection[outputIdx], false); log.Write(" Num of bin 0 samples: " + ces.BinaryErrStat.BinValErrStat[0].NumOfSamples.ToString(), false); log.Write(" Bad bin 0 classif.: " + ces.BinaryErrStat.BinValErrStat[0].Sum.ToString(CultureInfo.InvariantCulture), false); log.Write(" Bin 0 error rate: " + ces.BinaryErrStat.BinValErrStat[0].ArithAvg.ToString(CultureInfo.InvariantCulture), false); log.Write(" Bin 0 accuracy: " + (1 - ces.BinaryErrStat.BinValErrStat[0].ArithAvg).ToString(CultureInfo.InvariantCulture), false); log.Write(" Num of bin 1 samples: " + ces.BinaryErrStat.BinValErrStat[1].NumOfSamples.ToString(), false); log.Write(" Bad bin 1 classif.: " + ces.BinaryErrStat.BinValErrStat[1].Sum.ToString(CultureInfo.InvariantCulture), false); log.Write(" Bin 1 error rate: " + ces.BinaryErrStat.BinValErrStat[1].ArithAvg.ToString(CultureInfo.InvariantCulture), false); log.Write(" Bin 1 accuracy: " + (1 - ces.BinaryErrStat.BinValErrStat[1].ArithAvg).ToString(CultureInfo.InvariantCulture), false); log.Write(" Total num of samples: " + ces.BinaryErrStat.TotalErrStat.NumOfSamples.ToString(), false); log.Write(" Total bad classif.: " + ces.BinaryErrStat.TotalErrStat.Sum.ToString(CultureInfo.InvariantCulture), false); log.Write(" Total error rate: " + ces.BinaryErrStat.TotalErrStat.ArithAvg.ToString(CultureInfo.InvariantCulture), false); log.Write(" Total accuracy: " + (1 - ces.BinaryErrStat.TotalErrStat.ArithAvg).ToString(CultureInfo.InvariantCulture), false); } else { //Prediction task report log.Write(" OutputField: " + demoCaseParams.stateMachineCfg.ReadoutLayerConfig.OutputFieldNameCollection[outputIdx], false); log.Write(" Predicted next value: " + predictionOutputVector[outputIdx].ToString(CultureInfo.InvariantCulture), false); log.Write(" Total num of samples: " + ces.PrecissionErrStat.NumOfSamples.ToString(), false); log.Write(" Total Max Real Err: " + (bundleNormalizer.OutputFieldNormalizerRefCollection[outputIdx].ComputeNaturalSpan(ces.PrecissionErrStat.Max)).ToString(CultureInfo.InvariantCulture), false); log.Write(" Total Avg Real Err: " + (bundleNormalizer.OutputFieldNormalizerRefCollection[outputIdx].ComputeNaturalSpan(ces.PrecissionErrStat.ArithAvg)).ToString(CultureInfo.InvariantCulture), false); } log.Write(" ", false); } log.Write(" ", false); return; }
/// <summary> /// Performs one demo case. /// Loads and prepares sample data, trains State Machine and displayes results /// </summary> /// <param name="log">Into this interface are written output messages</param> /// <param name="demoCaseParams">An instance of DemoSettings.CaseSettings to be performed</param> public static void PerformDemoCase(IOutputLog log, DemoSettings.CaseSettings demoCaseParams) { log.Write(" Performing demo case " + demoCaseParams.Name, false); //Bundle normalizer object BundleNormalizer bundleNormalizer = null; //Prediction input vector (relevant only for input continuous feeding) double[] predictionInputVector = null; //Instantiate the State Machine StateMachine stateMachine = new StateMachine(demoCaseParams.StateMachineCfg); //Prepare input object for regression stage log.Write(" ", false); StateMachine.RegressionStageInput rsi = null; List <string> outputFieldNameCollection = (from rus in demoCaseParams.StateMachineCfg.ReadoutLayerConfig.ReadoutUnitCfgCollection select rus.Name).ToList(); List <CommonEnums.TaskType> outputFieldTaskCollection = (from rus in demoCaseParams.StateMachineCfg.ReadoutLayerConfig.ReadoutUnitCfgCollection select rus.TaskType).ToList(); if (demoCaseParams.StateMachineCfg.InputConfig.FeedingType == CommonEnums.InputFeedingType.Continuous) { //Continuous input feeding //Load data bundle from csv file TimeSeriesBundle data = TimeSeriesBundle.LoadFromCsv(demoCaseParams.FileName, demoCaseParams.StateMachineCfg.InputConfig.ExternalFieldNameCollection(), outputFieldNameCollection, outputFieldTaskCollection, StateMachine.DataRange, demoCaseParams.NormalizerReserveRatio, true, out bundleNormalizer, out predictionInputVector ); rsi = stateMachine.PrepareRegressionStageInput(data, PredictorsCollectionCallback, log); } else { //Patterned input feeding //Load data bundle from csv file PatternBundle data = PatternBundle.LoadFromCsv(demoCaseParams.FileName, demoCaseParams.StateMachineCfg.InputConfig.ExternalFieldNameCollection(), outputFieldNameCollection, outputFieldTaskCollection, StateMachine.DataRange, demoCaseParams.NormalizerReserveRatio, true, out bundleNormalizer ); rsi = stateMachine.PrepareRegressionStageInput(data, PredictorsCollectionCallback, log); } //Report statistics of the State Machine's reservoirs ReportReservoirsStatistics(rsi.ReservoirStatCollection, log); //Regression stage log.Write(" Regression stage", false); //Perform the regression ValidationBundle vb = stateMachine.RegressionStage(rsi, RegressionControl, log); //Perform prediction if the input feeding is continuous (we know the input but we don't know the ideal output) double[] predictionOutputVector = null; if (demoCaseParams.StateMachineCfg.InputConfig.FeedingType == CommonEnums.InputFeedingType.Continuous) { predictionOutputVector = stateMachine.Compute(predictionInputVector); //Values are normalized so they have to be denormalized bundleNormalizer.NaturalizeOutputVector(predictionOutputVector); } //Display results //Report training (regression) results and prediction log.Write(" Results", false); List <ReadoutLayer.ClusterErrStatistics> clusterErrStatisticsCollection = stateMachine.ClusterErrStatisticsCollection; //Results for (int outputIdx = 0; outputIdx < demoCaseParams.StateMachineCfg.ReadoutLayerConfig.ReadoutUnitCfgCollection.Count; outputIdx++) { ReadoutLayer.ClusterErrStatistics ces = clusterErrStatisticsCollection[outputIdx]; if (demoCaseParams.StateMachineCfg.ReadoutLayerConfig.ReadoutUnitCfgCollection[outputIdx].TaskType == CommonEnums.TaskType.Classification) { //Classification task report log.Write(" OutputField: " + demoCaseParams.StateMachineCfg.ReadoutLayerConfig.ReadoutUnitCfgCollection[outputIdx].Name, false); log.Write(" Num of bin 0 samples: " + ces.BinaryErrStat.BinValErrStat[0].NumOfSamples.ToString(), false); log.Write(" Bad bin 0 classif.: " + ces.BinaryErrStat.BinValErrStat[0].Sum.ToString(CultureInfo.InvariantCulture), false); log.Write(" Bin 0 error rate: " + ces.BinaryErrStat.BinValErrStat[0].ArithAvg.ToString(CultureInfo.InvariantCulture), false); log.Write(" Bin 0 accuracy: " + (1 - ces.BinaryErrStat.BinValErrStat[0].ArithAvg).ToString(CultureInfo.InvariantCulture), false); log.Write(" Num of bin 1 samples: " + ces.BinaryErrStat.BinValErrStat[1].NumOfSamples.ToString(), false); log.Write(" Bad bin 1 classif.: " + ces.BinaryErrStat.BinValErrStat[1].Sum.ToString(CultureInfo.InvariantCulture), false); log.Write(" Bin 1 error rate: " + ces.BinaryErrStat.BinValErrStat[1].ArithAvg.ToString(CultureInfo.InvariantCulture), false); log.Write(" Bin 1 accuracy: " + (1 - ces.BinaryErrStat.BinValErrStat[1].ArithAvg).ToString(CultureInfo.InvariantCulture), false); log.Write(" Total num of samples: " + ces.BinaryErrStat.TotalErrStat.NumOfSamples.ToString(), false); log.Write(" Total bad classif.: " + ces.BinaryErrStat.TotalErrStat.Sum.ToString(CultureInfo.InvariantCulture), false); log.Write(" Total error rate: " + ces.BinaryErrStat.TotalErrStat.ArithAvg.ToString(CultureInfo.InvariantCulture), false); log.Write(" Total accuracy: " + (1 - ces.BinaryErrStat.TotalErrStat.ArithAvg).ToString(CultureInfo.InvariantCulture), false); } else { //Forecast task report log.Write(" OutputField: " + demoCaseParams.StateMachineCfg.ReadoutLayerConfig.ReadoutUnitCfgCollection[outputIdx].Name, false); log.Write(" Predicted next value: " + predictionOutputVector[outputIdx].ToString(CultureInfo.InvariantCulture), false); log.Write(" Total num of samples: " + ces.PrecissionErrStat.NumOfSamples.ToString(), false); log.Write(" Total Max Real Err: " + (bundleNormalizer.OutputFieldNormalizerRefCollection[outputIdx].ComputeNaturalSpan(ces.PrecissionErrStat.Max)).ToString(CultureInfo.InvariantCulture), false); log.Write(" Total Avg Real Err: " + (bundleNormalizer.OutputFieldNormalizerRefCollection[outputIdx].ComputeNaturalSpan(ces.PrecissionErrStat.ArithAvg)).ToString(CultureInfo.InvariantCulture), false); } log.Write(" ", false); } log.Write(" ", false); return; }
/// <summary> /// Performs one demo case. /// Loads and prepares sample data, trains State Machine and displayes results /// </summary> /// <param name="log">Into this interface are written output messages</param> /// <param name="demoCaseParams">An instance of DemoSettings.CaseSettings to be performed</param> public static void PerformDemoCase(IOutputLog log, DemoSettings.CaseSettings demoCaseParams) { log.Write(" Performing demo case " + demoCaseParams.Name, false); log.Write(" ", false); //Instantiate the State Machine StateMachine stateMachine = new StateMachine(demoCaseParams.StateMachineCfg); //Prepare input object for regression stage StateMachine.RegressionInput rsi = null; //Prediction input vector (relevant only for input continuous feeding) double[] predictionInputVector = null; if (demoCaseParams.StateMachineCfg.NeuralPreprocessorConfig.InputConfig.FeedingType == NeuralPreprocessor.InputFeedingType.Continuous) { //Continuous input feeding //Load data bundle from csv file VectorBundle data = VectorBundle.LoadFromCsv(demoCaseParams.FileName, demoCaseParams.StateMachineCfg.NeuralPreprocessorConfig.InputConfig.ExternalFieldNameCollection(), demoCaseParams.StateMachineCfg.ReadoutLayerConfig.OutputFieldNameCollection, out predictionInputVector ); rsi = stateMachine.PrepareRegressionData(data, PredictorsCollectionCallback, log); } else { //Patterned input feeding //Load data bundle from csv file PatternBundle data = PatternBundle.LoadFromCsv(demoCaseParams.FileName, demoCaseParams.StateMachineCfg.NeuralPreprocessorConfig.InputConfig.ExternalFieldNameCollection(), demoCaseParams.StateMachineCfg.ReadoutLayerConfig.OutputFieldNameCollection ); rsi = stateMachine.PrepareRegressionData(data, PredictorsCollectionCallback, log); } //Report key statistics of the State Machine's reservoirs string statisticsReport = rsi.CreateReport(4); log.Write(statisticsReport); log.Write(string.Empty); //Regression stage - building of trained readout layer log.Write(" Regression stage (training of readout layer)", false); //Perform the regression ResultBundle rcb = stateMachine.BuildReadoutLayer(rsi, RegressionControl, log); log.Write(string.Empty); //Report training (regression) results log.Write(" Training results", false); string trainingReport = stateMachine.RL.GetTrainingResultsReport(6); log.Write(trainingReport); log.Write(string.Empty); //Perform prediction if the input feeding is continuous (we know the input but we don't know the ideal output) if (demoCaseParams.StateMachineCfg.NeuralPreprocessorConfig.InputConfig.FeedingType == NeuralPreprocessor.InputFeedingType.Continuous) { double[] predictionOutputVector = stateMachine.Compute(predictionInputVector); string predictionReport = stateMachine.RL.GetForecastReport(predictionOutputVector, 6); log.Write(" Forecasts", false); log.Write(predictionReport); log.Write(string.Empty); } return; }