/// <summary> /// Checks whether the network configuration is suitable for the specified type of the network output. /// </summary> /// <param name="outputType">The type of the network output.</param> /// <param name="netCfg">The network configuration.</param> public static void CheckNetCfg(TNRNet.OutputType outputType, INonRecurrentNetworkSettings netCfg) { switch (outputType) { case TNRNet.OutputType.Probabilistic: { if (netCfg.GetType() != typeof(FeedForwardNetworkSettings)) { throw new ArgumentException($"Incorrect network configuration. It must be the Feed forward network configuration.", "netCfg"); } if (((FeedForwardNetworkSettings)netCfg).OutputActivationCfg.GetType() != typeof(AFAnalogSoftMaxSettings)) { throw new ArgumentException($"Feed forward network must have the SoftMax output activation.", "netCfg"); } if (((FeedForwardNetworkSettings)netCfg).TrainerCfg.GetType() != typeof(RPropTrainerSettings)) { throw new ArgumentException($"Feed forward network must have associated the RProp trainer.", "netCfg"); } } break; case TNRNet.OutputType.Real: { if (netCfg.GetType() != typeof(FeedForwardNetworkSettings)) { throw new ArgumentException($"Incorrect network configuration. It must be the Feed forward network configuration.", "netCfg"); } } break; default: break; } return; }
//Constructor /// <summary> /// Creates an instance ready to start building trained non-recurrent network /// </summary> /// <param name="networkName">Name of the network to be built</param> /// <param name="networkSettings">Network configuration (FeedForwardNetworkSettings or ParallelPerceptronSettings object)</param> /// <param name="foldNum">Current fold number</param> /// <param name="numOfFolds">Total number of the folds</param> /// <param name="foldNetworkNum">Current fold network number</param> /// <param name="numOfFoldNetworks">Total number of the fold networks</param> /// <param name="trainingBundle">Bundle of predictors and ideal values to be used for training purposes</param> /// <param name="testingBundle">Bundle of predictors and ideal values to be used for testing purposes</param> /// <param name="binBorder">If specified, it indicates that the whole network output is binary and specifies numeric border where GE network output is decided as a 1 and LT output as a 0.</param> /// <param name="rand">Random generator to be used (optional)</param> /// <param name="controller">Regression controller (optional)</param> public TrainedNetworkBuilder(string networkName, INonRecurrentNetworkSettings networkSettings, int foldNum, int numOfFolds, int foldNetworkNum, int numOfFoldNetworks, VectorBundle trainingBundle, VectorBundle testingBundle, double binBorder = double.NaN, Random rand = null, RegressionControllerDelegate controller = null ) { _networkName = networkName; _networkSettings = networkSettings; _foldNum = foldNum; _numOfFolds = numOfFolds; _foldNetworkNum = foldNetworkNum; _numOfFoldNetworks = numOfFoldNetworks; //Check num of output values is 1 if (trainingBundle.OutputVectorCollection[0].Length != 1) { throw new InvalidOperationException($"Only single output value is allowed."); } _trainingBundle = trainingBundle; _testingBundle = testingBundle; _binBorder = binBorder; _rand = rand ?? new Random(0); _controller = controller ?? DefaultRegressionController; return; }
/// <summary> /// Creates new network and associated trainer. /// </summary> /// <param name="settings">Non-recurrent-network settings</param> /// <param name="trainingInputVectors">Collection of training input samples</param> /// <param name="trainingOutputVectors">Collection of training output (desired) samples</param> /// <param name="rand">Random object to be used</param> /// <param name="net">Created network</param> /// <param name="trainer">Created associated trainer</param> public static void CreateNetworkAndTrainer(INonRecurrentNetworkSettings settings, List <double[]> trainingInputVectors, List <double[]> trainingOutputVectors, Random rand, out INonRecurrentNetwork net, out INonRecurrentNetworkTrainer trainer ) { if (IsFF(settings)) { //Feed forward network FeedForwardNetworkSettings netCfg = (FeedForwardNetworkSettings)settings; FeedForwardNetwork ffn = new FeedForwardNetwork(trainingInputVectors[0].Length, trainingOutputVectors[0].Length, netCfg); net = ffn; if (netCfg.TrainerCfg.GetType() == typeof(QRDRegrTrainerSettings)) { trainer = new QRDRegrTrainer(ffn, trainingInputVectors, trainingOutputVectors, (QRDRegrTrainerSettings)netCfg.TrainerCfg, rand); } else if (netCfg.TrainerCfg.GetType() == typeof(RidgeRegrTrainerSettings)) { trainer = new RidgeRegrTrainer(ffn, trainingInputVectors, trainingOutputVectors, (RidgeRegrTrainerSettings)netCfg.TrainerCfg); } else if (netCfg.TrainerCfg.GetType() == typeof(ElasticRegrTrainerSettings)) { trainer = new ElasticRegrTrainer(ffn, trainingInputVectors, trainingOutputVectors, (ElasticRegrTrainerSettings)netCfg.TrainerCfg); } else if (netCfg.TrainerCfg.GetType() == typeof(RPropTrainerSettings)) { trainer = new RPropTrainer(ffn, trainingInputVectors, trainingOutputVectors, (RPropTrainerSettings)netCfg.TrainerCfg, rand); } else { throw new ArgumentException($"Unknown trainer {netCfg.TrainerCfg}"); } } else if (IsPP(settings)) { //Parallel perceptron network //Check output if (trainingOutputVectors[0].Length != 1) { throw new InvalidOperationException($"Can't create ParallelPerceptron. Only single output value is allowed."); } ParallelPerceptronSettings netCfg = (ParallelPerceptronSettings)settings; ParallelPerceptron ppn = new ParallelPerceptron(trainingInputVectors[0].Length, netCfg); net = ppn; trainer = new PDeltaRuleTrainer(ppn, trainingInputVectors, trainingOutputVectors, netCfg.PDeltaRuleTrainerCfg, rand); } else { throw new InvalidOperationException($"Unknown network settings"); } net.RandomizeWeights(rand); return; }
//Constructor /// <summary> /// Creates an initialized instance. /// </summary> /// <param name="networkName">The name of the network to be built.</param> /// <param name="networkCfg">The configuration of the network to be built.</param> /// <param name="networkOutput">The type of output of the network to be built.</param> /// <param name="trainingBundle">The bundle of input and ideal vectors to be used for the network training.</param> /// <param name="testingBundle">The bundle of input and ideal vectors to be used for the network testing.</param> /// <param name="rand">The random generator to be used (optional).</param> /// <param name="controller">The build process controller (optional).</param> public TNRNetBuilder(string networkName, INonRecurrentNetworkSettings networkCfg, TNRNet.OutputType networkOutput, VectorBundle trainingBundle, VectorBundle testingBundle, Random rand = null, BuildControllerDelegate controller = null ) { _networkName = networkName; NonRecurrentNetUtils.CheckNetCfg(networkOutput, networkCfg); _networkCfg = networkCfg; _networkOutput = networkOutput; NonRecurrentNetUtils.CheckData(_networkOutput, trainingBundle); _trainingBundle = trainingBundle; NonRecurrentNetUtils.CheckData(_networkOutput, testingBundle); _testingBundle = testingBundle; _rand = rand ?? new Random(0); _controller = controller ?? DefaultNetworkBuildController; return; }
/// <summary> /// Determines if given settings object is ParallelPerceptronSettings /// </summary> /// <param name="settings">Non-recurrent-network settings</param> public static bool IsPP(INonRecurrentNetworkSettings settings) { return(settings.GetType() == typeof(ParallelPerceptronSettings)); }
/// <summary> /// Determines if given settings object is FeedForwardNetworkSettings /// </summary> /// <param name="settings">Non-recurrent-network settings</param> public static bool IsFF(INonRecurrentNetworkSettings settings) { return(settings.GetType() == typeof(FeedForwardNetworkSettings)); }