Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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();
}
}
}
@chenafa

This comment has been minimized.

Copy link

chenafa commented Oct 4, 2018

xwvwxv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.