/// <summary> /// Creates and trains the State Machine readout layer. /// </summary> /// <param name="regressionInput"> /// RegressionInput object prepared by PrepareRegressionData function /// </param> /// <param name="regressionController"> /// Optional. see Regression.RegressionCallbackDelegate /// </param> /// <param name="regressionControllerData"> /// Optional custom object to be passed to regressionController together with other standard information /// </param> public ResultComparativeBundle BuildReadoutLayer(RegressionInput regressionInput, ReadoutUnit.RegressionCallbackDelegate regressionController = null, Object regressionControllerData = null ) { //Readout layer instance RL = new ReadoutLayer(_settings.ReadoutLayerConfig); //Optional mapper of predictors to readout units ReadoutLayer.PredictorsMapper mapper = null; if (_settings.MapperConfig != null) { //Create empty instance of the mapper mapper = new ReadoutLayer.PredictorsMapper(NP.NumOfPredictors); //Expand list of predicting neurons to array of predictor origin StateMachineSettings.MapperSettings.PoolRef[] neuronPoolRefCollection = new StateMachineSettings.MapperSettings.PoolRef[NP.NumOfPredictors]; int idx = 0; foreach (Reservoir.PredictorNeuron pn in NP.PredictorNeuronCollection) { neuronPoolRefCollection[idx] = new StateMachineSettings.MapperSettings.PoolRef { _reservoirInstanceIdx = pn.Neuron.Placement.ReservoirID, _poolIdx = pn.Neuron.Placement.PoolID }; ++idx; if (pn.UseSecondaryPredictor) { neuronPoolRefCollection[idx] = neuronPoolRefCollection[idx - 1]; ++idx; } } //Iterate readout units having specific predictors mapping foreach (string readoutUnitName in _settings.MapperConfig.Map.Keys) { bool[] switches = new bool[NP.NumOfPredictors]; switches.Populate(false); foreach (StateMachineSettings.MapperSettings.PoolRef allowedPool in _settings.MapperConfig.Map[readoutUnitName]) { //Enable specific predictors from allowed pool (origin) for (int i = 0; i < neuronPoolRefCollection.Length; i++) { if (neuronPoolRefCollection[i]._reservoirInstanceIdx == allowedPool._reservoirInstanceIdx && neuronPoolRefCollection[i]._poolIdx == allowedPool._poolIdx) { switches[i] = true; } } } //Add mapping to mapper mapper.Add(readoutUnitName, switches); } } //Training return(RL.Build(regressionInput.PreprocessedData, regressionController, regressionControllerData, mapper )); }
public static CommonOutputs.RegressionOutput CreateRegressionEnsemble(IHostEnvironment env, RegressionInput input) { Contracts.CheckValue(env, nameof(env)); var host = env.Register("CombineModels"); host.CheckValue(input, nameof(input)); host.CheckNonEmpty(input.Models, nameof(input.Models)); GetPipeline(host, input, out IDataView startingData, out RoleMappedData transformedData); var args = new RegressionEnsembleTrainer.Arguments(); switch (input.ModelCombiner) { case ScoreCombiner.Median: args.OutputCombiner = new MedianFactory(); break; case ScoreCombiner.Average: args.OutputCombiner = new AverageFactory(); break; default: throw host.Except("Unknown combiner kind"); } var trainer = new RegressionEnsembleTrainer(host, args); var ensemble = trainer.CombineModels(input.Models.Select(pm => pm.Predictor as IPredictorProducing <float>)); var predictorModel = new PredictorModel(host, transformedData, startingData, ensemble); var output = new CommonOutputs.RegressionOutput { PredictorModel = predictorModel }; return(output); }
/// <summary> /// Creates and trains the State Machine readout layer. /// Function uses specific mapping of predictors to readout units, if available. /// Function also rejects unusable predictors having no reasonable fluctuation of values. /// </summary> /// <param name="regressionInput"> /// RegressionInput object prepared by PrepareRegressionData function /// </param> /// <param name="regressionController"> /// Optional. see Regression.RegressionCallbackDelegate /// </param> /// <param name="regressionControllerData"> /// Optional custom object to be passed to regressionController together with other standard information /// </param> public ResultBundle BuildReadoutLayer(RegressionInput regressionInput, ReadoutUnit.RegressionCallbackDelegate regressionController = null, Object regressionControllerData = null ) { //Readout layer instance RL = new ReadoutLayer(_settings.ReadoutLayerConfig); //Create empty instance of the mapper ReadoutLayer.PredictorsMapper mapper = new ReadoutLayer.PredictorsMapper(PredictorGeneralSwitchCollection); if (_settings.MapperCfg != null) { //Expand list of predicting neurons to array of predictor origin StateMachineSettings.MapperSettings.AllowedPool[] neuronPoolRefCollection = new StateMachineSettings.MapperSettings.AllowedPool[NP.NumOfPredictors]; int idx = 0; foreach (HiddenNeuron neuron in NP.PredictorNeuronCollection) { StateMachineSettings.MapperSettings.AllowedPool poolRef = new StateMachineSettings.MapperSettings.AllowedPool { _reservoirInstanceIdx = neuron.Placement.ReservoirID, _poolIdx = neuron.Placement.PoolID }; for (int i = 0; i < neuron.PredictorsCfg.NumOfEnabledPredictors; i++) { neuronPoolRefCollection[idx] = poolRef; ++idx; } } //Iterate all readout units foreach (string readoutUnitName in _settings.ReadoutLayerConfig.OutputFieldNameCollection) { bool[] switches = new bool[NP.NumOfPredictors]; //Initially allow all valid predictors PredictorGeneralSwitchCollection.CopyTo(switches, 0); //Exists specific mapping? if (_settings.MapperCfg != null && (_settings.MapperCfg.PoolsMap.ContainsKey(readoutUnitName) || _settings.MapperCfg.RoutedInputFieldsMap.ContainsKey(readoutUnitName))) { //Routed input fields if (_settings.MapperCfg.RoutedInputFieldsMap.ContainsKey(readoutUnitName)) { //Initially disable all routed input fields for (int i = NP.PredictorNeuronCollection.Count; i < NP.NumOfPredictors; i++) { switches[i] = false; } //Enable enabled routed input fields List <int> enabledRoutedFieldsIdxs = _settings.MapperCfg.RoutedInputFieldsMap[readoutUnitName]; for (int i = 0; i < enabledRoutedFieldsIdxs.Count; i++) { switches[NP.PredictorNeuronCollection.Count + enabledRoutedFieldsIdxs[i]] = PredictorGeneralSwitchCollection[NP.PredictorNeuronCollection.Count + enabledRoutedFieldsIdxs[i]]; } } //Neuron predictors if (_settings.MapperCfg.PoolsMap.ContainsKey(readoutUnitName)) { //Initially disable all neuron predictors for (int i = 0; i < NP.PredictorNeuronCollection.Count; i++) { switches[i] = false; } //Enable allowed neuron predictors foreach (StateMachineSettings.MapperSettings.AllowedPool allowedPool in _settings.MapperCfg.PoolsMap[readoutUnitName]) { //Enable specific predictors from allowed pool (origin) for (int i = 0; i < NP.PredictorNeuronCollection.Count; i++) { if (neuronPoolRefCollection[i]._reservoirInstanceIdx == allowedPool._reservoirInstanceIdx && neuronPoolRefCollection[i]._poolIdx == allowedPool._poolIdx) { //Enable predictor if it is valid switches[i] = PredictorGeneralSwitchCollection[i]; } } } } } //Add mapping to mapper mapper.Add(readoutUnitName, switches); } } //Training return(RL.Build(regressionInput.PreprocessedData, regressionController, regressionControllerData, mapper )); }