Created
November 12, 2015 18:27
-
-
Save NikolayIT/d3eba2ccf7b0a29c471d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Encog; | |
| using Encog.ML.Data; | |
| using Encog.ML.Data.Basic; | |
| using Encog.ML.Train; | |
| using Encog.Neural.Networks; | |
| using Encog.Neural.Networks.Layers; | |
| using Encog.Neural.Networks.Training.Propagation.Resilient; | |
| using System; | |
| using System.Collections.Generic; | |
| using Encog.Engine.Network.Activation; | |
| namespace EncogDotNetPlay | |
| { | |
| internal class Program | |
| { | |
| static Func<double, double, double> sum = (a, b) => a + b; | |
| static Func<double, double, double> mult = (a, b) => a * b; | |
| private static void Main() | |
| { | |
| var rand = new Random(); | |
| var op = sum; | |
| var trainingData = new List<IMLDataPair>(); | |
| for (var i = 0; i < 1000; i++) | |
| { | |
| var first = rand.Next(1, 100); | |
| var second = rand.Next(1, 100); | |
| var input = new double[] { first, second }; | |
| var output = new[] { op(first, second) }; | |
| var mldatapair = new BasicMLDataPair(new BasicMLData(input), new BasicMLData(output)); | |
| trainingData.Add(mldatapair); | |
| } | |
| // create a neural network, without using a factory | |
| var network = new BasicNetwork(); | |
| for (var i = 0; i < 1; i++) | |
| { | |
| //network.AddLayer(new BasicLayer(new ActivationLOG(), true, 2)); | |
| //network.AddLayer(new BasicLayer(new ActivationLOG(), false, 1)); | |
| //network.AddLayer(new BasicLayer(new ActivationElliott(), true, 2)); | |
| network.AddLayer(new BasicLayer(null, true, 2)); | |
| network.AddLayer(new BasicLayer(null, false, 1)); | |
| } | |
| network.Structure.FinalizeStructure(); | |
| network.Reset(); | |
| // train the neural network | |
| IMLTrain train = new ResilientPropagation(network, new BasicMLDataSet(trainingData)); | |
| var epoch = 1; | |
| do | |
| { | |
| train.Iteration(); | |
| Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error); | |
| epoch++; | |
| } while (train.Error > 0.0000001 && epoch < 10000); | |
| train.FinishTraining(); | |
| // test the neural network | |
| Console.WriteLine(@"Neural Network Results:"); | |
| foreach (var pair in trainingData) | |
| { | |
| var output = network.Compute(pair.Input); | |
| Console.WriteLine(pair.Input + @", actual=" + output + @", ideal=" + pair.Ideal[0]); | |
| } | |
| var data = new List<BasicMLData> | |
| { | |
| new BasicMLData(new[] {1.0, 2.0}), | |
| new BasicMLData(new[] {11.0, 21.0}), | |
| new BasicMLData(new[] {1.0, -20.0}), | |
| new BasicMLData(new[] {-20.0, -20.0}), | |
| new BasicMLData(new[] {-20.0, 20.0}), | |
| new BasicMLData(new[] {20.0, -20.0}), | |
| new BasicMLData(new[] {20.0, 20.0}), | |
| new BasicMLData(new[] {1337.0, 5015}) | |
| }; | |
| foreach (var item in data) | |
| { | |
| var output = network.Compute(item); | |
| Console.WriteLine(item + @", actual=" + output + $" {op(item[0], item[1])}"); | |
| } | |
| EncogFramework.Instance.Shutdown(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
xwvwxv