/// <summary> /// Builds a training set. /// </summary> /// <param name="lags"></param> /// <param name="leaps"></param> /// <returns> /// The training set. /// </returns> public TrainingSet BuildTrainingSet(int[] lags, int[] leaps) { TrainingSet trainingSet = new TrainingSet(lags.Length, leaps.Length); // The following assumes that lags and leaps are ordered in ascending fashion. int maxLag = lags[0]; int maxLeap = leaps[leaps.Length - 1]; // Add training patterns into the training set. for (int i = -maxLag; i < Length - maxLeap; i++) { // Build the input vector. double[] inputVector = new double[lags.Length]; for (int j = 0; j < inputVector.Length; j++) { inputVector[j] = data[i + lags[j]]; } // Build the output vector. double[] outputVector = new double[leaps.Length]; for (int j = 0; j < outputVector.Length; j++) { outputVector[j] = data[i + leaps[j]]; } // Build a training pattern and add it to the training set. TrainingPattern trainingPattern = new TrainingPattern(inputVector, outputVector); trainingSet.Add(trainingPattern); } return(trainingSet); }
/// <summary> /// Test a teacher. /// </summary> /// <param name="testNumber">The number of the test.</param> /// <param name="testDescription">The description of the test.</param> /// <param name="teacher">The teacher to test.</param> /// <param name="network">The network to train.</param> /// <param name="maxIterationCount">The maximum number of iterations.</param> /// <param name="maxTolerableNetworkError">The maximum tolerable network error.</param> private static void Test(int testNumber, string testDescription, ITeacher teacher, INetwork network, int maxIterationCount, double maxTolerableNetworkError) { // Print the number of the test and its description. Console.WriteLine("Test " + testNumber + " : " + testDescription); // Run the teacher (i.e. train the network). DateTime startTime = DateTime.Now; TrainingLog trainingLog = teacher.Train(network, maxIterationCount, maxTolerableNetworkError); DateTime endTime = DateTime.Now; // Print the results. Console.WriteLine("Test " + testNumber + " : Duration : " + (endTime - startTime)); Console.WriteLine("Test " + testNumber + " : Number of iterations taken : " + trainingLog.IterationCount); Console.WriteLine("Test " + testNumber + " : Network error achieved : " + trainingLog.NetworkError); foreach (TrainingPattern trainingPattern in teacher.TrainingSet.TrainingPatterns) { double[] outputVector = network.Evaluate(trainingPattern.InputVector); Console.WriteLine( TrainingPattern.VectorToString(trainingPattern.InputVector) + " -> " + TrainingPattern.VectorToString(outputVector) ); } }
static void Main(string[] args) { Console.WriteLine("{0:.10}, {1}", "Hello", "World"); // Step 1 : Alternative A : Building a training set manually // --------------------------------------------------------- int inputVectorLength = 2; int outputVectorLength = 1; TrainingSet trainingSet = new TrainingSet(inputVectorLength, outputVectorLength); TrainingPattern trainingPattern = new TrainingPattern(new double[2] { 0.0, 0.0 }, new double[1] { 0.0 }); trainingSet.Add(trainingPattern); trainingPattern = new TrainingPattern(new double[2] { 0.0, 1.0 }, new double[1] { 1.0 }); trainingSet.Add(trainingPattern); trainingPattern = new TrainingPattern(new double[2] { 1.0, 0.0 }, new double[1] { 1.0 }); trainingSet.Add(trainingPattern); trainingPattern = new TrainingPattern(new double[2] { 1.0, 1.0 }, new double[1] { 0.0 }); trainingSet.Add(trainingPattern); // Step 2 : Building a blueprint of a network // ------------------------------------------ LayerBlueprint inputLayerBlueprint = new LayerBlueprint(inputVectorLength); ActivationLayerBlueprint[] hiddenLayerBlueprints = new ActivationLayerBlueprint[1]; hiddenLayerBlueprints[0] = new ActivationLayerBlueprint(2, new LogisticActivationFunction()); ActivationLayerBlueprint outputLayerBlueprint = new ActivationLayerBlueprint(outputVectorLength, new LogisticActivationFunction()); NetworkBlueprint networkBlueprint = new NetworkBlueprint(inputLayerBlueprint, hiddenLayerBlueprints, outputLayerBlueprint); // Step 3 : Building a network // --------------------------- Network network = new Network(networkBlueprint); Console.WriteLine(network.ToString()); // Step 4 : Building a teacher // --------------------------- ITeacher teacher = new AntColonyOptimizationTeacher(trainingSet, null, null); // Step 5 : Training the network // ----------------------------- int maxIterationCount = 10000; double maxTolerableNetworkError = 1e-3; TrainingLog trainingLog = teacher.Train(network, maxIterationCount, maxTolerableNetworkError); Console.WriteLine("Number of runs used : " + trainingLog.RunCount); Console.WriteLine("Number of iterations used : " + trainingLog.IterationCount); Console.WriteLine("Minimum network error achieved : " + trainingLog.NetworkError); // Step 6 : Using the trained network // ---------------------------------- foreach (TrainingPattern tp in trainingSet.TrainingPatterns) { double[] inputVector = tp.InputVector; double[] outputVector = network.Evaluate(inputVector); Console.WriteLine(tp.ToString() + " -> " + TrainingPattern.VectorToString(outputVector)); } }
/// <summary> /// Trains the neural network using a single pattern /// </summary> /// <param name="pattern">the pattern to feed the network</param> /// <param name="η">The learning rate</param> /// <returns></returns> float train(TrainingPattern pattern, float η) { RunForward(pattern.Inputs); return(deltaAndBackpropagate(pattern.Outputs, η)); }