Ejemplo n.º 1
0
        public void TestBias()
        {
            double input = 0.3;
            double inpToOutWeight = 0.4;
            double biasToOutWeight = 0.5;

            var sigmoid = new SigmoidFunction();
            var nw = new Network(sigmoid, false);

            var inputNode = new Input(input);
            var bias = new Bias();
            var outputNode = new Perceptron(sigmoid, "Output");
            Connection.Create(inpToOutWeight, inputNode, outputNode);
            Connection.Create(biasToOutWeight, bias, outputNode);

            nw.Nodes = new Node[][] {
                new Node[] { inputNode },
                new Node[] { outputNode }
            };

            var nwOut = nw.Nodes[1][0].Output;

            //Output
            // = Sig(inpToOut.Output + biasToOut.Output)
            // = Sig((inpToOut.Weight * inpToOut.GetInput()) + (biasToOutWeight * 1))
            // = Sig((inpToOut.Weight * inputNode.Output) + (biasToOutWeight))
            // = Sig((this.weight * this.input) + (biasToOutWeight))
            var expOut = sigmoid.Calculate((inpToOutWeight * input) + biasToOutWeight);

            Assert.AreEqual(nwOut, expOut);
        }
Ejemplo n.º 2
0
        /// <param name="checkSuccess">Funcion takes 'expected', 'actual' returns bool wether or not actual can be considerd a success</param>
        public static ValidationResult Validate(Network network, IEnumerable<InputExpectedResult> inputResults, Func<double[], double[], bool> checkSuccess)
        {
            ValidationResult totalResult = null;
            foreach(var cur in inputResults) {
                double[] actual = network.GetInputResult(cur.Input);

                double sse = SumSquaredError(cur.Output, actual);
                bool success = checkSuccess(cur.Output, actual);

                var res = new ValidationResult(sse, success);

                totalResult += res;
            }

            return totalResult;
        }
Ejemplo n.º 3
0
        private static void Main(string[] args) {
            var data = DataReader.ReadFromFile("data/iris.data")/*.OrderBy(i => random.Next())*/;
            foreach(var entry in data) {
                //TODO: it is suboptimal to calculate min and max every time
                entry.PetalLength = Normalize(entry.PetalLength, data.Min(i => i.PetalLength), data.Max(i => i.PetalLength));
                entry.PetalWidth = Normalize(entry.PetalWidth, data.Min(i => i.PetalWidth), data.Max(i => i.PetalWidth));
                entry.SepalLength = Normalize(entry.SepalLength, data.Min(i => i.SepalLength), data.Max(i => i.SepalLength));
                entry.SepalWidth = Normalize(entry.SepalWidth, data.Min(i => i.SepalWidth), data.Max(i => i.SepalWidth));
            }

            var network = new Network(new NeuralNet.TransferFunctions.HyperbolicTangentFunction(), true);
            network.FillNetwork(4, 3, 6);

            var train = data.Take(TrainSetSize).ToArray();
            var inputAndExpectedResuls = train.Select(entry => new InputExpectedResult(entry.AsInput, entry.AsOutput));
            var validation = data.Skip(TrainSetSize).ToArray();

            var bp = new Backpropagate(network, 0.5, 3);

            var trainData = inputAndExpectedResuls.ToArray();

            int trains = 0;
            double score = 0;
            var start = Environment.TickCount;
            while(score < 90) {
                trains++;
                bp.Train(trainData.OrderBy(i => random.Next()).ToArray());

                var stats = NetworkValidation.Validate(network, inputAndExpectedResuls, IrisEntry.IsOutputSuccess);
                score = stats.SuccessPercentage;
                Console.WriteLine($"{trains,-4}" + stats.ToString());
            }
            Console.WriteLine($"\nTime elapsed: {Environment.TickCount - start}Ms");
            Console.WriteLine("Done training");

            var trainStats = NetworkValidation.Validate(network, train.Select(entry => new InputExpectedResult(entry.AsInput, entry.AsOutput)), IrisEntry.IsOutputSuccess);
            var validateStats = NetworkValidation.Validate(network, validation.Select(entry => new InputExpectedResult(entry.AsInput, entry.AsOutput)), IrisEntry.IsOutputSuccess);

            Console.WriteLine($"{trainStats.ToString()} TRAIN");
            Console.WriteLine($"{validateStats.ToString()} VALIDATE");
            Console.WriteLine($"{(trainStats + validateStats).ToString()} TOTAL");

            Console.ReadKey();
        }
 public NeuralNetBot(Checker myColor, Network network, double? lambda=null, LambdaType? lambdaType=null)
     : base(myColor, lambda, lambdaType)
 {
     Network = network;
 }
Ejemplo n.º 5
0
 static void Main(string[] args)
 {
     var nn = new Network(4, 3, 1);
     var result = nn.FeedForward(new double[] { 0.4, 0.6, 0.1, 0.2 });
     return;
 }
Ejemplo n.º 6
0
        public void TestFillNetwork()
        {
            int inputs = 2;
            int layer1 = 3;
            int layer2 = 4;
            int outputs = 1;

            var nw = new Network(new SigmoidFunction(), true);
            nw.FillNetwork(inputs, outputs, layer1, layer2);

            Assert.IsTrue(nw.Nodes[0].Length == inputs);
            Assert.IsTrue(nw.Nodes[1].Length == layer1);
            Assert.IsTrue(nw.Nodes[2].Length == layer2);
            Assert.IsTrue(nw.Nodes[3].Length == outputs);

            Assert.IsTrue(((Perceptron)nw.Nodes[3][0]).GetIncommingConnections().Count == layer2 + 1); // +1 for bias node
            Assert.IsTrue(((Perceptron)nw.Nodes[2][3]).GetIncommingConnections().Count == layer1 + 1); // +1 for bias node
        }
Ejemplo n.º 7
0
        public void TestTraining()
        {
            var sigmoid = new SigmoidFunction();

            var net = new Network(sigmoid, true);
            net.FillNetwork(2, 2, 2);

            net.Nodes[0][0].GetOutgoingConnections()[0].Weight = .15;
            net.Nodes[0][0].GetOutgoingConnections()[1].Weight = .2;
            net.Nodes[0][1].GetOutgoingConnections()[0].Weight = .25;
            net.Nodes[0][1].GetOutgoingConnections()[1].Weight = .3;
            net.Nodes[1][0].GetOutgoingConnections()[0].Weight = .4;
            net.Nodes[1][0].GetOutgoingConnections()[1].Weight = .45;
            net.Nodes[1][1].GetOutgoingConnections()[0].Weight = .5;
            net.Nodes[1][1].GetOutgoingConnections()[1].Weight = .55;

            var expected = new InputExpectedResult(new double[] { .05, .1 }, new double[] { .01, .99 });

            var before = NetworkValidation.Validate(net, new InputExpectedResult[] { expected }, (a, b) => true);

            var bp = new Backpropagate(net, 0.5);
            bp.Train(new InputExpectedResult[] { expected });

            var after = NetworkValidation.Validate(net, new InputExpectedResult[] { expected }, (a, b) => true);

            Assert.IsTrue(before.AvgSSE > after.AvgSSE);
        }
Ejemplo n.º 8
0
        public void TestNetworkOutput()
        {
            var rand = new Random();
            double input = (double)(rand.NextDouble());
            double weight = (double)(rand.NextDouble());

            var sigmoid = new SigmoidFunction();
            var nw = new Network(sigmoid, false);

            var inputNode = new Input(input);
            var outputNode = new Perceptron(sigmoid, "Output");
            Connection.Create(weight, inputNode, outputNode);

            nw.Nodes = new Node[][] {
                new Node[] { inputNode },
                new Node[] { outputNode }
            };

            var nwOut = nw.CurOutput()[0];
            Assert.AreEqual(outputNode.Output, nwOut);

            var expOut = sigmoid.Calculate(input * weight);

            Assert.AreEqual(nwOut, expOut);
        }
Ejemplo n.º 9
0
    void Start()
    {
        //MIND:
#if MODULUS_MIND
        //ASSUMES SINGLE_THREADED. (ELSE PUT A LOCK HERE)

        if (minds == null)
        {
            minds = new NeuralNet.Network[max_mind];
            for (int i = 0; i < max_mind; i++)
            {
                mind = new NeuralNet.Network(brainConfig,
                                             NeuralNet.Misc.sigmoidNF());        //Create a neural-network with input size for the eyes, and 4 outputs.

                //if(mind == null)
                //    Debug.Log("Mind is nulL!");
                minds[i] = mind;
            }
        }

        if (mind == null)
        {
            brainID = cur_mind;
            mind    = minds[cur_mind];
            cur_mind++;
            cur_mind = cur_mind % max_mind;
        }
        //End locked-area.

        //simple_train();
#else
        if (mind == null)
        {
            mind = new NeuralNet.Network(brainConfig,
                                         NeuralNet.Misc.sigmoidNF());            //Create a neural-network with input size for the eyes, and 4 outputs.
            //simple_train();
        }
#endif



        myBodyParts = new List <GameObject>();

        body = GetComponent <Rigidbody>();
        //body.velocity = new Vector3 (1,0,0);
        //body.velocity = (new Vector3(Random.Range(-100, 100), 0, Random.Range(-100, 100))).normalized * 10;

#if ADD_EYE
        eye = Creature_bits.newEye();
        AddBodyPart(eye, (this.transform.forward + new Vector3(0, .4f, 0)));
#endif

#if ADD_MOUTH
        GameObject mouth = Instantiate(mouthPart);
        AddBodyPart(mouth, this.transform.forward);
#endif

        //max_age = 300*12*2; //300 ~= 5secs, *12 make sit about a minute
        max_age    = 300; //300frames.
        max_health = 100;
        max_food   = 1000;

        age    = 0;
        food   = max_food / 2;
        health = max_health;

        trueColor = GetComponent <Renderer>().material.color;
    }
Ejemplo n.º 10
0
 private void InitNetwork(double learnRate, int microBatchsize, int inputHeight, int outputHeight, int[] hiddenHeights)
 {
     Network = new Network(TransferFunc, true);
     Network.FillNetwork(inputHeight, outputHeight, hiddenHeights);
     BackpropTrain = new Backpropagate(Network, learnRate, microBatchsize);
 }
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (Status == TrainStatus.Running)
                {
                    Status = TrainStatus.Paused;
                    Save();
                    return;
                }

                if (Thread != null && Thread.IsAlive)
                   Thread.Join(); // Wait for last thread to finish working before starting again.

                Termination termination;
                if (cbTerminationType.Text == "ByValidationSet")
                    termination = Termination.ByValidationSet(DataParser.ValidationSet, ToInt(tbValidateCycle, x => x > 0, "Validation cycle must be > 0"));
                else if (cbTerminationType.Text == "ByIteration")
                    termination = Termination.ByIteration(ToInt(tbIterations, x => x > 0, "Iterations must be > 0"));
                else
                    throw new Exception("Did not recognize termination type: " + cbTerminationType.Text);

                NetworkParameters parameters = new NetworkParameters()
                {
                    InitialWeightInterval = new Tuple<double, double>(ToDouble(tbInitialWeightMin), ToDouble(tbInitialWeightMax)),
                    LearningRate = ToDouble(tbLearningRate, x => x > 0, "Learning rate must be > 0"),
                    LearningRateDecay = ToDouble(tbLearningRateDecay),
                    Momentum = ToDouble(tbMomentum, x => x >= 0 && x < 1, "Momentum must be in [0,1)"),
                    MomentumDecay = ToDouble(tbMomentumDecay)
                };

                string name = ToString(tbName, s => s.Trim() != string.Empty, "Network name cannot be empty.");
                if (Status == TrainStatus.Create && Directory.Exists(name))
                {
                    if (MessageBox.Show("Neural Net folder '" + name + "' already exists. Delete contents?\r\nIf not, use a unique name.", "Error", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                        (new DirectoryInfo(name)).Delete(true);
                    else
                        return;
                }

                if (Status == TrainStatus.Create)
                {
                    Network = new Network(
                        name,
                        ToInt(tbInputs, x => x > 0, "Number of input nodes must be > 0"),
                        tbHiddens.Text.Split(new char[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => ToInt(x, tbHiddens, xx => xx > 0, "Number of hidden nodes must be > 0 per layer")).ToList<int>(),
                        ToInt(tbOutputs, x => x > 0, "Number of output nodes must be > 0"),
                        termination,
                        parameters);
                }
                else
                {
                    Network.Termination.Type = termination.Type;
                    Network.Termination.ValidationSet = termination.ValidationSet;
                    Network.Termination.ValidateCycle = termination.ValidateCycle;
                    Network.Termination.TotalIterations = termination.TotalIterations;
                    Network.Parameters = parameters;
                    Network.Name = name;
                }

                Status = TrainStatus.Running;
                Thread = new Thread(new ThreadStart(DoTrain)) { IsBackground = true };
                Thread.Start();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error");
            }
        }
        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            Save();

            Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
            open.Filter = "Neural Network|*.net";
            open.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
            open.RestoreDirectory = true;
            if (open.ShowDialog().Value)
            {
                try
                {
                    Network = (Network)Serializer.Deserialize(open.FileName);
                    PopulateControls(Network);
                    Status = TrainStatus.Paused;
                    Title = Network.Name;
                }
                catch { MessageBox.Show("Could not deserialize " + open.FileName.ToString(), "Error"); }
            }
        }
        public void UpdateProgress(Network network)
        {
            lbError.Content = (!network.TrueError.HasValue ? "0" : String.Format("{0:f5}", network.TrueError.Value).ToString());
            lbIteration.Content = network.Termination.CurrentIteration;
            lbTimeElapsed.Content = network.TrainTime.Elapsed.ToString(@"d\.hh\:mm\:ss");

            for (; CurrentErrorHistoryIndex < network.ErrorHistory.Count; ++CurrentErrorHistoryIndex)
            {
                ValidationPlot.AppendAsync(this.Dispatcher, network.ErrorHistory[CurrentErrorHistoryIndex]);
            }
        }
 public void PopulateControls(Network network)
 {
     tbName.Text = network.Name;
     tbInputs.Text = network.Inputs.Count.ToString();
     tbHiddens.Text = network.HiddensLayout.Aggregate(string.Empty, (total, i) => total += i.ToString() + " ").Trim();
     tbOutputs.Text = network.Outputs.Count.ToString();
     tbLearningRate.Text = network.Parameters.LearningRate.ToString();
     tbLearningRateDecay.Text = network.Parameters.LearningRateDecay.ToString();
     tbMomentum.Text = network.Parameters.Momentum.ToString();
     tbMomentumDecay.Text = network.Parameters.MomentumDecay.ToString();
     tbInitialWeightMin.Text = network.Parameters.InitialWeightInterval.Item1.ToString();
     tbInitialWeightMax.Text = network.Parameters.InitialWeightInterval.Item2.ToString();
     cbTerminationType.Text = network.Termination.Type.ToString();
     tbIterations.Text = network.Termination.TotalIterations.ToString();
     tbValidateCycle.Text = network.Termination.ValidateCycle.ToString();
     ValidationPlot.Collection.Clear();
     TrainingPlot.Collection.Clear();
     UpdateProgress(network);
 }
        /// <summary>
        /// Simulate a game until completion.
        /// </summary>
        /// <param name="board">Starting board that the bots will play on.  This need not be empty!</param>
        /// <param name="network">Neural network that provides the AI for gameplay.</param>
        /// <returns>Trace of game sequence, each board state stored as a Neural Net Example</returns>
        public List<Example> Play(Board board, Network network)
        {
            Bot allen = new NeuralNetBot(Checker.Blue, network); // <-- you know he will win :)
            Bot jason = new NeuralNetBot(Checker.Green, network);

            List<Example> trace = new List<Example>();

            Turns = 0;
            Bot current = allen.MyColor == board.NextPlayer ? allen : jason;
            while (!board.IsGameOver)
            {
                int column;
                double score;
                current.SelectMove(board, out column, out score);
                Log(String.Format("{0} picks column {1}   (Score: {2:f2})", (current == allen ? "Allen" : "Jason"), column, score));
                board.AddChecker(current.MyColor, column);

                Example example = Transform.ToNormalizedExample(board, current.MyColor);
                example.Predictions.Add(score);
                trace.Add(example);

                current = (current == allen ? jason : allen);
                ++Turns;
            }

            if (Viewer != null)
                Viewer.BatchAddCheckers(Checker.Blue, board.MoveHistory,completedBoard:board);

            TotalTurns += Turns;

            Checker winner;
            if (board.TryGetWinner(out winner))
            {
            //The game is over, there was a winner.
            //This means the last element of "trace" represents a won
            //board state (i.e. there is a four-in-a-row with color
            //'winner').

                if (trace.Count > 0) trace[trace.Count - 1].Predictions[0] = Transform.ToValue(GameResult.Win);
                if (trace.Count > 1) trace[trace.Count - 2].Predictions[0] = Transform.ToValue(GameResult.Loss);
                if (winner == allen.MyColor)
                {
                    Log("WINNER:  Allen");
                    ++AllenWon;
                }
                else
                {
                    Log("WINNER:  Jason");
                    ++JasonWon;
                }
            }
            else
            {
                if (trace.Count > 0) trace[trace.Count - 1].Predictions[0] = Transform.ToValue(GameResult.Draw);
                if (trace.Count > 1) trace[trace.Count - 2].Predictions[0] = Transform.ToValue(GameResult.Draw);
                Log("TIE");
                ++Ties;
            }

            ++TotalGames;
            Log(string.Format("Turns: {0} ({1:f2})", Turns, (double)TotalTurns / TotalGames));
            Log(string.Format("Allen: {0}({1:f2}) Jason: {2}({3:f2}) Ties {4}({5:f2})   TOTAL: {6}", AllenWon, (double)AllenWon / TotalGames, JasonWon, (double)JasonWon / TotalGames, Ties, (double)Ties / TotalGames, TotalGames));
            Log("");

            List<Example> trace1 = new List<Example>(), trace2 = new List<Example>();
            for (int i = 0; i < trace.Count; ++i)
            {
                if (i % 2 == 0) trace1.Add(trace[i]);
                else trace2.Add(trace[i]);
            }
            double lambda = .7;
            double alpha = .1;
            double gamma = .5;
            UpdateTraceLabels(trace1, lambda, alpha, gamma);
            UpdateTraceLabels(trace2, lambda, alpha, gamma);

            return trace1.Union(trace2).ToList();
        }
Ejemplo n.º 16
0
 public Trainer(NetworkGenerator gui)
 {
     this.gui = gui;
     Network = gui.Network;
 }
Ejemplo n.º 17
0
 public Trainer(Network network)
 {
     Network = network;
 }
Ejemplo n.º 18
0
        public void TestGetInputResult()
        {
            var rand = new Random();
            double input = (double)(rand.NextDouble() * 100 + 1);
            double weight = (double)(rand.NextDouble() * 2);

            var sigmoid = new SigmoidFunction();
            var nw = new Network(sigmoid, false);

            var inputNode = new Input(input);
            var outputNode = new Perceptron(sigmoid, "Output");
            Connection.Create(weight, inputNode, outputNode);

            nw.Nodes = new Node[][] {
                new Node[] { inputNode },
                new Node[] { outputNode }
            };

            var nwOut = nw.GetInputResult(input)[0];

            //Output
            // = Sig(inpToOut.Output)
            // = Sig(inpToOut.Weight * inpToOut.GetInput())
            // = Sig(inpToOut.Weight * inputNode.Output)
            // = Sig(this.weight * this.input)
            var expOut = sigmoid.Calculate(weight * input);

            Assert.AreEqual(nwOut, expOut);
        }