public static FeedForwardNetwork Cross(int _Seed, FeedForwardNetwork _A, FeedForwardNetwork _B) { Random R = new Random(_Seed); FeedForwardNetwork Answer = new FeedForwardNetwork(_A); int ChromosomesCount = 0; for (int i = 1; i < _A.Layers.Count; i++) { for (int j = 0; j < _A.Layers[i].Neurons.Count; j++) { ChromosomesCount += _A.Layers[i].Neurons[j].Inputs.Count; } } int Range = R.Next(ChromosomesCount); for (int i = 1; i < _A.Layers.Count; i++) { for (int j = 0; j < _A.Layers[i].Neurons.Count; j++) { for (int k = 0; k < _A.Layers[i].Neurons[j].Inputs.Count; k++) { if (i + j + k >= Range) { Answer.Layers[i].Neurons[j].Inputs[k].Weight = _B.Layers[i].Neurons[j].Inputs[k].Weight; } else { Answer.Layers[i].Neurons[j].Inputs[k].Weight = _A.Layers[i].Neurons[j].Inputs[k].Weight; } } } } return(Answer); }
public ExamingNetworkMaster() { Console.WriteLine("Welcome to Examing Network Master"); Console.WriteLine("Now you have to load existing network or create new"); FeedForwardNetwork network = new FeedForwardNetwork(); Console.WriteLine("Do you want to load existing network?"); if (Console.ReadLine() == "Yes") { network = FeedForwardNetwork.Load(Console.ReadLine()); } else { network = new FeedForwardNetwork(new BipolarSigmoidFunction(), Environment.TickCount, 1, 7, 1); Console.WriteLine("New neural network created successfully"); } BackPropagationLearning teacher = new BackPropagationLearning(network); teacher.LearningRate = 0.03; Console.WriteLine("Let's teach it"); Teaching(teacher); Console.WriteLine("Now it's time to exam this network(:"); Examing(network); Console.WriteLine("Do you want to save this network?"); if (Console.ReadLine() == "Yes") { Console.WriteLine("Enter filename: "); network.Save(Console.ReadLine()); } Console.WriteLine("Press enter to exit..."); Console.ReadKey(); }
/// <summary> /// Convenience function to train a vanilla feed forward neural network /// </summary> /// <param name="trainingContext">The training context to use</param> /// <param name="lap">Linear algebra provider</param> /// <param name="trainingData">Training data provider</param> /// <param name="testData">Test data provider</param> /// <param name="layerDescriptor">The layer descriptor</param> /// <param name="hiddenLayerSize">The size of the single hidden layer</param> /// <param name="numEpochs">Number of epochs to train for</param> /// <returns>A trained feed forward model</returns> public static FeedForwardNetwork TrainNeuralNetwork( this ITrainingContext trainingContext, ILinearAlgebraProvider lap, ITrainingDataProvider trainingData, ITrainingDataProvider testData, LayerDescriptor layerDescriptor, int hiddenLayerSize, int numEpochs ) { Console.WriteLine($"Training a {trainingData.InputSize}x{hiddenLayerSize}x{trainingData.OutputSize} neural network..."); FeedForwardNetwork bestModel = null; using (var trainer = lap.NN.CreateBatchTrainer(layerDescriptor, trainingData.InputSize, hiddenLayerSize, trainingData.OutputSize)) { float bestScore = 0; trainingContext.EpochComplete += c => { var testError = trainer.Execute(testData).Select(d => trainingContext.ErrorMetric.Compute(d.Output, d.ExpectedOutput)).Average(); var flag = false; if (testError > bestScore) { bestScore = testError; bestModel = trainer.NetworkInfo; flag = true; } trainingContext.WriteScore(testError, trainingContext.ErrorMetric.DisplayAsPercentage, flag); }; trainer.Train(trainingData, numEpochs, trainingContext); } return(bestModel); }
public OddTest() { Console.WriteLine("Welcome to Neural Odd Test Example"); FeedForwardNetwork network = new FeedForwardNetwork(new SigmoidFunction(), 1, 10, 1); BackPropagationLearning teacher = new BackPropagationLearning(network); teacher.LearningRate = 0.1; Random R = new Random(Environment.TickCount); Console.WriteLine("Training..."); for (int i = 0; i < 1000; i++) { Console.Write("[T] Input >> "); int Input = R.Next(1024); Console.WriteLine(Input); teacher.Teach(Binary(Input), (Input % 2 == 0) ? (new double[] { 0 }) : (new double[] { 1 })); Console.Write("[T] Output >> "); Console.WriteLine(network.Launch(Binary(Input))[0].ToString()); } Console.WriteLine("Examing..."); Console.Write("[E] Input >> "); string userString = Console.ReadLine(); while (userString != "stop") { int Number = int.Parse(userString.Split(' ')[0]); Console.Write("[E] Output >> "); Print(network.Launch(Binary(Number))); Console.Write("[E] Input >> "); userString = Console.ReadLine(); } Console.WriteLine(); }
public void LoadButtonLeftClick() { Pacmans = new List <Pacman>(); for (int i = 0; i < Directory.GetFiles(LoadFolder, "pacman*.nnw").Length; i++) { Pacmans.Add(new Pacman(new Vector2(R.Next(gameBounds.Width), R.Next(gameBounds.Height)))); Pacmans[Pacmans.Count - 1].Network = FeedForwardNetwork.Load(LoadFolder + "\\pacman" + i.ToString() + ".nnw"); } Notifications.Push("Loading completed"); }
/// <summary> /// Initialise the weights of a neural network. /// </summary> /// <param name="network"></param> public void Initialise(NeuralNetworkBase network) { FeedForwardNetwork feedForward = (FeedForwardNetwork)network; for (int i = 0; i < matrices.Length; i++) { FeedForwardLayer.Dense layer = (FeedForwardLayer.Dense)feedForward.Layers[i]; layer.Weights = matrices[i]; } }
/// <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; }
public IStandardExecution CreateFeedForward(FeedForwardNetwork network) { var layer = new List <StandardFeedForward>(); foreach (var item in network.Layer) { layer.Add(_ReadFeedForward(item)); } return(new FeedForwardExecution(_lap, layer)); }
public void Train(ITrainingDataProvider trainingData, int numEpochs, ITrainingContext context) { _bestScore = _GetScore(context); Console.WriteLine(context.ErrorMetric.DisplayAsPercentage ? "Initial score: {0:P}" : "Initial score: {0}", _bestScore); _bestOutput = null; context.EpochComplete += OnEpochComplete; _trainer.Train(trainingData, numEpochs, context); context.EpochComplete -= OnEpochComplete; // ensure best values are current ApplyBestParams(); }
public void FeedForwardNetworkTooSmallTest() { FeedForwardNetwork network = new FeedForwardNetwork(2); Assert.ThrowsException <FeedForwardNetworkTooSmallException>(delegate() { network.ComputeOutputs(new Matrix(new[, ] { { 0.5 }, { 0.8 } })); }); }
// MARK: public methods /// <summary> /// Use this backpropagation trainer to /// </summary> /// <param name="network"></param> /// <param name="dataSet"></param> public void Train(FeedForwardNetwork network, DataSet dataSet) { Network = network; DataSet = dataSet; TrainingSetup(); if (!IsReady()) { throw new OperationCanceledException( "The trainer does not yet have the required information to start training."); } StartTraining(); }
public void FeedForwardNetworkTest() { FeedForwardNetwork network = new FeedForwardNetwork(2); network.AddLayer(2); network.AddLayer(1); Matrix output = network.ComputeOutputs(new Matrix(new[, ] { { 1.0 }, { 0.0 } })); Assert.IsNotNull(output); }
/// <summary> /// Runs the example code. /// </summary> public void Run() { //Create configuration of the feed forward network having Identity output layer and two LeakyReLU hidden layers //with associated resilient back propagation trainer configuration const int HiddenLayerSize = 3; HiddenLayerSettings hiddenLayerCfg = new HiddenLayerSettings(HiddenLayerSize, new LeakyReLUSettings()); FeedForwardNetworkSettings ffNetCfg = new FeedForwardNetworkSettings(new IdentitySettings(), new HiddenLayersSettings(hiddenLayerCfg, hiddenLayerCfg), new RPropTrainerSettings(2, 200) ); //Collect training data VectorBundle trainingData = CreateTrainingData(); //Create network instance //We specify 2 input values, 3 output values and previously prepared network structure configuration FeedForwardNetwork ffNet = new FeedForwardNetwork(2, 3, ffNetCfg); //Training _log.Write("Training"); _log.Write("--------"); //Create trainer instance RPropTrainer trainer = new RPropTrainer(ffNet, trainingData.InputVectorCollection, trainingData.OutputVectorCollection, (RPropTrainerSettings)ffNetCfg.TrainerCfg, new Random(0) ); //Training loop while (trainer.Iteration() && trainer.MSE > 1e-6) { _log.Write($" Attempt {trainer.Attempt} / Epoch {trainer.AttemptEpoch,3} Mean Squared Error = {Math.Round(trainer.MSE, 8).ToString(CultureInfo.InvariantCulture)}", false); } _log.Write(string.Empty); //Training is done //Display network computation results _log.Write("Trained network computations:"); _log.Write("-----------------------------"); foreach (double[] input in trainingData.InputVectorCollection) { double[] results = ffNet.Compute(input); _log.Write($" Input {input[0]} {input[1]} Results: AND={Math.Round(results[0])} OR={Math.Round(results[1])} XOR={Math.Round(results[2])}"); } _log.Write(string.Empty); //Finished return; } //Run
static void Main(string[] args) { Pokedex pD = new Pokedex(); BattleHandler b = new BattleHandler(); FeedForwardNetwork f = new FeedForwardNetwork(pD); NEATNetwork n = new NEATNetwork(pD); bool finished = false; while (!finished) { Console.Write("\nWhat do you want to do, select and confirm with enter:\n\n[1] Train FeedForward\n[2] Train NEAT\n[3] Test FeedForward\n[4] Test NEAT\n[5] Quit"); string option; option = Console.ReadLine(); switch (option) { case "1": f.train(); break; case "2": n.train(); break; case "3": f.test(); break; case "4": n.test(); break; case "5": finished = true; break; default: Console.Write("\n\nNot a valid option..."); break; } Console.Write("\nPress any key to continue..."); Console.ReadKey(); Console.Clear(); } }
/// <summary> /// Trains FF network to solve boolean algebra. It shows how to do it on the lowest level, /// without use of TNRNetBuilder. /// </summary> private void FullyManualLearning() { _log.Write("Example of a FF network low level training:"); //Create FF network configuration. FeedForwardNetworkSettings ffNetCfg = CreateFFNetConfig(); _log.Write($"Network configuration xml:"); _log.Write(ffNetCfg.GetXml(true).ToString()); //Collect training data VectorBundle trainingData = CreateTrainingData(); //Create network instance //We specify 2 input values, 3 output values and previously prepared network structure configuration FeedForwardNetwork ffNet = new FeedForwardNetwork(2, //The number of input values 3, //The number of output values ffNetCfg //Network structure and a trainer ); //Training _log.Write(string.Empty); _log.Write(" Training"); _log.Write(string.Empty); //Create the trainer instance RPropTrainer trainer = new RPropTrainer(ffNet, trainingData.InputVectorCollection, trainingData.OutputVectorCollection, (RPropTrainerSettings)ffNetCfg.TrainerCfg, new Random(0) ); //Training loop while (trainer.Iteration()) { _log.Write($" Attempt {trainer.Attempt} / Epoch {trainer.AttemptEpoch,3} Mean Squared Error = {Math.Round(trainer.MSE, 8).ToString(CultureInfo.InvariantCulture)}", true); //Check training exit condition if (trainer.MSE < 1e-7) { break; } } _log.Write(string.Empty); //Training is done //Display the network computation results DisplayNetworkComputations(ffNet); //Finished return; }
private static void CreateNetAndTreainer(ReadoutLayerSettings.ReadoutUnitSettings settings, List <double[]> trainingPredictorsCollection, List <double[]> trainingIdealOutputsCollection, Random rand, out INonRecurrentNetwork net, out INonRecurrentNetworkTrainer trainer ) { if (settings.NetType == ReadoutLayerSettings.ReadoutUnitSettings.ReadoutUnitNetworkType.FF) { FeedForwardNetworkSettings netCfg = (FeedForwardNetworkSettings)settings.NetSettings; FeedForwardNetwork ffn = new FeedForwardNetwork(trainingPredictorsCollection[0].Length, 1, netCfg); net = ffn; if (netCfg.TrainerCfg.GetType() == typeof(QRDRegrTrainerSettings)) { trainer = new QRDRegrTrainer(ffn, trainingPredictorsCollection, trainingIdealOutputsCollection, (QRDRegrTrainerSettings)netCfg.TrainerCfg, rand); } else if (netCfg.TrainerCfg.GetType() == typeof(RidgeRegrTrainerSettings)) { trainer = new RidgeRegrTrainer(ffn, trainingPredictorsCollection, trainingIdealOutputsCollection, (RidgeRegrTrainerSettings)netCfg.TrainerCfg, rand); } else if (netCfg.TrainerCfg.GetType() == typeof(ElasticRegrTrainerSettings)) { trainer = new ElasticRegrTrainer(ffn, trainingPredictorsCollection, trainingIdealOutputsCollection, (ElasticRegrTrainerSettings)netCfg.TrainerCfg); } else if (netCfg.TrainerCfg.GetType() == typeof(RPropTrainerSettings)) { trainer = new RPropTrainer(ffn, trainingPredictorsCollection, trainingIdealOutputsCollection, (RPropTrainerSettings)netCfg.TrainerCfg, rand); } else { throw new ArgumentException($"Unknown trainer {netCfg.TrainerCfg}"); } } else { ParallelPerceptronSettings netCfg = (ParallelPerceptronSettings)settings.NetSettings; ParallelPerceptron ppn = new ParallelPerceptron(trainingPredictorsCollection[0].Length, netCfg); net = ppn; trainer = new PDeltaRuleTrainer(ppn, trainingPredictorsCollection, trainingIdealOutputsCollection, netCfg.PDeltaRuleTrainerCfg, rand); } net.RandomizeWeights(rand); return; }
public void ManualExaming(FeedForwardNetwork _network) { Console.Write("[E] Inputs >> "); string userString = Console.ReadLine(); while (userString != "stop") { double[] Inputs = ParseDoubles(userString); double[] Outputs = _network.Launch(Inputs); Console.Write("[E] Outputs >> "); for (int i = 0; i < Outputs.Length; i++) { Console.Write(Outputs[i] + " "); } Console.WriteLine(); Console.Write("[E] Inputs >> "); userString = Console.ReadLine(); } }
public static int Mutate(int _Seed, FeedForwardNetwork _Example) { Random R = new Random(_Seed); int ChromosomesMutated = 0; for (int i = 1; i < _Example.Layers.Count; i++) { for (int j = 0; j < _Example.Layers[i].Neurons.Count; j++) { for (int k = 0; k < _Example.Layers[i].Neurons[j].Inputs.Count; k++) { if (R.Next(100) == 23) { _Example.Layers[i].Neurons[j].Inputs[k].Weight += (2 * R.NextDouble() - 1); ChromosomesMutated++; } } } } return(ChromosomesMutated); }
private static void CreateNetAndTreainer(ReadoutLayerSettings.ReadoutUnitSettings settings, List <double[]> trainingPredictorsCollection, List <double[]> trainingIdealOutputsCollection, Random rand, out INonRecurrentNetwork net, out INonRecurrentNetworkTrainer trainer ) { if (settings.NetType == ReadoutLayerSettings.ReadoutUnitSettings.ReadoutUnitNetworkType.FF) { FeedForwardNetworkSettings netCfg = (FeedForwardNetworkSettings)settings.NetSettings; FeedForwardNetwork ffn = new FeedForwardNetwork(trainingPredictorsCollection[0].Length, 1, netCfg); net = ffn; switch (netCfg.RegressionMethod) { case FeedForwardNetworkSettings.TrainingMethodType.Linear: trainer = new LinRegrTrainer(ffn, trainingPredictorsCollection, trainingIdealOutputsCollection, settings.RegressionAttemptEpochs, rand, netCfg.LinRegrTrainerCfg); break; case FeedForwardNetworkSettings.TrainingMethodType.Resilient: trainer = new RPropTrainer(ffn, trainingPredictorsCollection, trainingIdealOutputsCollection, netCfg.RPropTrainerCfg); break; default: throw new ArgumentException($"Not supported regression method {netCfg.RegressionMethod}"); } } else { ParallelPerceptronSettings netCfg = (ParallelPerceptronSettings)settings.NetSettings; ParallelPerceptron ppn = new ParallelPerceptron(trainingPredictorsCollection[0].Length, netCfg); net = ppn; trainer = new PDeltaRuleTrainer(ppn, trainingPredictorsCollection, trainingIdealOutputsCollection, netCfg.PDeltaRuleTrainerCfg); } net.RandomizeWeights(rand); return; }
private void OnEpochComplete(ITrainingContext context) { var score = _GetScore(context); var flag = false; var errorMetric = context.ErrorMetric; if ((errorMetric.HigherIsBetter && score > _bestScore) || (!errorMetric.HigherIsBetter && score < _bestScore)) { _bestScore = score; _bestOutput = _trainer.NetworkInfo; flag = true; } if ((context.CurrentEpoch % _reportCadence) == 0) { context.WriteScore(score, errorMetric.DisplayAsPercentage, flag); } if (flag) { using (var stream = new FileStream(_dataFile, FileMode.Create, FileAccess.Write)) Serializer.Serialize(stream, _bestOutput); _noChange = 0; } else { ++_noChange; } if (_autoAdjustOnNoChangeCount.HasValue && _noChange >= _autoAdjustOnNoChangeCount.Value) { _noChange = 0; ApplyBestParams(); context.ReduceTrainingRate(); Console.WriteLine("Reducing training rate to " + context.TrainingRate); } }
/// <summary> /// Create a feedforward neural network, train it, and run some test examples. /// </summary> public FeedForwardRegression() { // Define the function we are trying to model in the range [0, 1] double TestFunction(double x) { return(Math.Sqrt(x) + 0.3 * Math.Sin(6 * Math.Sqrt(x))); } // Create the data set // Frame the problem as a regression problem; one input (x) and one output (y) DataSet.Regression dataSet = new DataSet.Regression(1, 1); // Populate data set - data point takes a double array for inputs and outputs Random random = new Random(421); for (int i = 0; i < 10000; i++) { double nextDouble = random.NextDouble(); // In the range [0, 1] dataSet.AddDataPoint(new double[] { nextDouble }, new double[] { TestFunction(nextDouble) }); } // Split the data points randomly into training, validation, and test sets in the ratio // 7:2:1 dataSet.AssignDataPoints(0.7, 0.2, 0.1); // Create feedforward network // Create a network with one input and one output FeedForwardNetwork network = new FeedForwardNetwork(1, 1); // Add five hidden nodes, and connect them to the output layer using sigmoid activation network.AddHiddenLayer(32, new ActivationFunction.Sigmoid()) .AddOutputLayer(new ActivationFunction.Sigmoid()); // Set up the backpropagation trainer // Create the batch selector - here, select a random mini-batch from the training set // with 32 examples in it DataPoint[] select(DataSet data) => data.GetRandomTrainingSubset(32); BackpropagationTrainer trainer = new BackpropagationTrainer() { // Set the learning rate LearningRate = 0.1, // Initialise network weights using a uniform distribution initialiser = new Initialiser.Uniform(-0.01, 0.01, false), // Update weights after the whole batch rather than after each point stochastic = false, // Train on the whole training set each iteration batchSelector = new BackpropagationTrainer.BatchSelector(select), // Use squared error as the loss function lossFunction = new LossFunction.SquaredError(), // Log training data every 5000 epochs; access this with trainer.evaluations evaluationFrequency = 50 }; // Add a termination condition to the trainer; it will stop after 1200 epochs trainer.terminationConditions.Add(new TerminationCondition.EpochLimit(1200)); // Troubleshoot trainer (this will notify you of any missing required settings) foreach (string s in trainer.Troubleshoot()) { Console.WriteLine(); } // Start training // Train the network on the data set trainer.Train(network, dataSet); // Print training data to the console List <double[]> evals = trainer.evaluations; foreach (double[] arr in evals) { Console.WriteLine("epoch={0}, training error={1}, " + "validation error={2}", arr[0], arr[1], arr[2]); } Console.WriteLine(); // Test // Print the test set loss Console.WriteLine("Mean test loss per point: " + trainer.TestLoss() + "\n"); // Print 64 samples from the test set foreach (DataPoint dataPoint in dataSet.GetRandomTestSubset(64)) { // Feed the network a test point and record the output double output = network.GetOutput(Matrix.ToColumnMatrix(dataPoint.input))[0, 0]; Console.WriteLine("({0}) -> {1} : expected {2}", dataPoint.input[0], output, dataPoint.output[0]); } Console.WriteLine(); // If training takes a long time, this will notify you when it finishes Console.Beep(880, 2000); }
public BackPropagationLearning(FeedForwardNetwork _Network) { Network = _Network; }
public void AutoExaming(FeedForwardNetwork _network) { }
public void Examing(FeedForwardNetwork _network) { ManualExaming(_network); }
/// <summary> /// Create a feedforward neural network, train it, and run some test examples. /// </summary> public FeedForwardClassification() { // Create the data set // Create a data set for classification with two independent variables DataSet.Classification dataSet = new DataSet.Classification(2); // Populate data set - data point takes a double array for inputs and an integer class // as output Random random = new Random(386); for (int i = 0; i < 10000; i++) { double d1 = random.NextDouble(); // In the range [0, 1] double d2 = random.NextDouble(); int category; if ((d1 < 0.5) && (d2 < 0.5)) { category = 0; } else if ((d1 < 0.5) && (d2 >= 0.5)) { category = 1; } else if (d2 < 0.5) { category = 2; } else { category = 3; } dataSet.AddDataPoint(new double[] { d1, d2 }, category); } dataSet.OneHotAll(); // Convert all data to one hot // Split the data points randomly into training, validation, and test sets in the ratio // 7:2:1 dataSet.AssignDataPoints(0.7, 0.2, 0.1); dataSet.OneHotAll(); // Create feedforward network // Create a network with two inputs and four outputs FeedForwardNetwork network = new FeedForwardNetwork(2, 4); // Add five hidden nodes with sigmoid activation, and connect them to the output layer // using softmax activation network.AddHiddenLayer(5, new ActivationFunction.Sigmoid()) .AddOutputLayer(new ActivationFunction.Softmax()); // Set up backpropagation trainer // Create the batch selector - here, select a random mini-batch from the training set // with 32 examples in it DataPoint[] select(DataSet data) => data.GetRandomTrainingSubset(32); BackpropagationTrainer trainer = new BackpropagationTrainer() { // Set the learning rate LearningRate = 0.1, // Initialise network weights using a uniform distribution initialiser = new Initialiser.Uniform(-0.01, 0.01, false), // Update weights after the whole batch rather than after each point stochastic = false, // Train on the whole training set each iteration batchSelector = new BackpropagationTrainer.BatchSelector(select), // Use squared error as the loss function lossFunction = new LossFunction.NegativeLogProb(), // Log training data every 5000 epochs; access this with trainer.evaluations evaluationFrequency = 5 }; // Add a termination condition to the trainer; it will stop after 1200 epochs trainer.terminationConditions.Add(new TerminationCondition.EpochLimit(1)); // Troubleshoot trainer (this will notify you of any missing required settings) foreach (string s in trainer.Troubleshoot()) { Console.WriteLine(); } // Start training // Train the network on the data set trainer.Train(network, dataSet); // Print training data to the console List <double[]> evals = trainer.evaluations; foreach (double[] arr in evals) { Console.WriteLine("epoch={0}, training error={1}, " + "validation error={2}", arr[0], arr[1], arr[2]); } Console.WriteLine(); }
/// <summary> /// Create a feedforward neural network, train it, and run some test examples. /// </summary> public FeedForwardXor() { // Create the data set // Frame the problem as a regression problem; two inputs (x, y) and one output (z) DataSet.Regression dataSet = new DataSet.Regression(2, 1); // Populate data set - data point takes a double array for inputs and outputs dataSet.AddDataPoint(new double[] { 0, 0 }, new double[] { 0 }); dataSet.AddDataPoint(new double[] { 1, 0 }, new double[] { 1 }); dataSet.AddDataPoint(new double[] { 0, 1 }, new double[] { 1 }); dataSet.AddDataPoint(new double[] { 1, 1 }, new double[] { 0 }); // Split the data points randomly into training, validation, and test sets // Here, put all data points into the training set dataSet.AssignDataPoints(1, 0, 0); // Create feedforward network // Create a network with two inputs and one output FeedForwardNetwork network = new FeedForwardNetwork(2, 1); // Add five hidden nodes, and connect them to the output layer using sigmoid activation network.AddHiddenLayer(5, new ActivationFunction.Sigmoid()) .AddOutputLayer(new ActivationFunction.Sigmoid()); // Set up the backpropagation trainer // Create the batch selector - here, select the whole training set, but this can be // used to select mini-batches, single points, etc. DataPoint[] select(DataSet data) => data.TrainingSet; BackpropagationTrainer trainer = new BackpropagationTrainer() { // Set the learning rate LearningRate = 0.09, // Initialise network weights using a uniform distribution initialiser = new Initialiser.Uniform(-0.2, 0.2, false), // Update weights after the whole batch rather than after each point stochastic = false, // Train on the whole training set each iteration batchSelector = new BackpropagationTrainer.BatchSelector(select), // Use squared error as the loss function lossFunction = new LossFunction.SquaredError(), // Log training data every 5000 epochs; access this with trainer.evaluations evaluationFrequency = 8000 }; // Add a termination condition to the trainer; it will stop after 50,000 epochs trainer.terminationConditions.Add(new TerminationCondition.EpochLimit(80000)); // Troubleshoot trainer (this will notify you of any missing required settings) foreach (string s in trainer.Troubleshoot()) { Console.WriteLine(); } // Start training // Train the network on the data set trainer.Train(network, dataSet); // Print training data to the console List <double[]> evals = trainer.evaluations; foreach (double[] arr in evals) { Console.WriteLine("epoch={0}, training error={1}, " + "validation error={2}", arr[0], arr[1], arr[2]); } Console.WriteLine(); // Test foreach (DataPoint dataPoint in dataSet.TrainingSet) { // Feed the network a test point and record the output double output = network.GetOutput(Matrix.ToColumnMatrix(dataPoint.input))[0, 0]; Console.WriteLine("({0}, {1}) -> {2} : expected {3}", dataPoint.input[0], dataPoint.input[1], output, dataPoint.output[0]); } Console.WriteLine(); }