/// <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 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); 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; }