Ejemplo n.º 1
0
 void Awake()
 {
     Target = (NeuralNetwork)target;
 }
Ejemplo n.º 2
0
        static void Main()
        {
            List <string> Accuracy = new List <string>();

            var EngWord = File.ReadAllLines("EN.filtered");
            var ITWord  = File.ReadAllLines("IT.filtered");

            List <CharSpace> Wordspace = new List <CharSpace>();

            for (int i = 0; i < 8; i++)
            {
                Wordspace.Add(new CharSpace(i));
            }

            var allinputs = new List <ConstantArgumentValue>();

            foreach (var i in Wordspace)
            {
                allinputs.AddRange(i.letters);
            }

            OutputData English, Italian;

            English = new OutputData();
            Italian = new OutputData();

            var outputs = new List <OutputData>();

            outputs.Add(English);
            outputs.Add(Italian);

            NeuralNetwork LanguageNeuralNet = new NeuralNetwork(allinputs, outputs, new int[] { 20 });


            int counterEN, counterIT;

            counterEN = 0;
            counterIT = 0;
            var random = new Random();
            int Right, Wrong;

            Right = 0;
            Wrong = 0;

            {
                var accur = CalculateAccurary(Wordspace, LanguageNeuralNet, English, Italian, EngWord, ITWord);
                Accuracy.Add((counterEN + counterIT).ToString() + ";" + accur.ToString());
            }

            while (counterEN + counterIT < EngWord.Length + ITWord.Length)
            {
                string word;
                if (random.Next() % 2 == 0)
                {
                    if (counterEN != EngWord.Length)
                    {
                        word = EngWord[counterEN];
                        counterEN++;
                        English.MustBeHigh = true;
                        Italian.MustBeHigh = false;
                    }
                    else
                    {
                        word = ITWord[counterIT];
                        counterIT++;
                        English.MustBeHigh = false;
                        Italian.MustBeHigh = true;
                    }
                }
                else
                {
                    if (counterIT != ITWord.Length)
                    {
                        word = ITWord[counterIT];
                        counterIT++;
                        English.MustBeHigh = false;
                        Italian.MustBeHigh = true;
                    }
                    else
                    {
                        word = EngWord[counterEN];
                        counterEN++;
                        English.MustBeHigh = true;
                        Italian.MustBeHigh = false;
                    }
                }

                for (int characternum = 0; characternum < 8; characternum++)
                {
                    Wordspace[characternum].SetLetter(word[characternum]);
                }
                LanguageNeuralNet.Learn();
                LanguageNeuralNet.CalculateResults();
                if (English.Value > Italian.Value)
                {
                    if (English.MustBeHigh)
                    {
                        Right++;
                    }
                    else
                    {
                        Wrong++;
                    }
                }
                else
                {
                    if (Italian.MustBeHigh)
                    {
                        Right++;
                    }
                    else
                    {
                        Wrong++;
                    }
                }
                if ((counterEN + counterIT) % 50 == 0)
                {
                    var accur = CalculateAccurary(Wordspace, LanguageNeuralNet, English, Italian, EngWord, ITWord);
                    Accuracy.Add((counterEN + counterIT).ToString() + ";" + accur.ToString());
                }


                //if ((counterEN + counterIT) == 7000)
                //{
                //    double percentageright = (double)Right / (double)(Right + Wrong) * 100f;
                //    int lol = 9;
                //}
            }

            File.WriteAllLines("Results20NodesAccurary.txt", Accuracy.ToArray());
        }
Ejemplo n.º 3
0
        static void Main()
        {
            var EngWord = File.ReadAllLines("EN.filtered");
            var ITWord  = File.ReadAllLines("IT.filtered");

            List <CharSpace> Wordspace = new List <CharSpace>();

            for (int i = 0; i < 8; i++)
            {
                Wordspace.Add(new CharSpace(i));
            }

            var allinputs = new List <ArgumentValue>();

            foreach (var i in Wordspace)
            {
                allinputs.AddRange(i.letters);
            }

            OutputData English, Italian;

            English = new OutputData();
            Italian = new OutputData();

            var outputs = new List <OutputData>();

            outputs.Add(English);
            outputs.Add(Italian);
            Italian.MustBeHigh = true;

            bool makeNew = true;

            NeuralNetwork LanguageNeuralNet;

            if (!makeNew)
            {
                //LanguageNeuralNet = NeuralNetwork.Load(allinputs, outputs);
                LanguageNeuralNet = NeuralNetwork.LoadMatrix(allinputs, outputs);
            }
            else
            {
                System.Diagnostics.Stopwatch builder = new System.Diagnostics.Stopwatch();
                builder.Start();
                LanguageNeuralNet = new NeuralNetwork(allinputs, outputs, new int[] { 10 });
                builder.Stop();

                System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
                timer.Start();

                int counterEN, counterIT;
                counterEN = 0;
                counterIT = 0;
                var random = new Random();
                int Right, Wrong;
                Right = 0;
                Wrong = 0;

                while (counterEN + counterIT < EngWord.Length + ITWord.Length)
                {
                    string word;
                    if (random.Next() % 2 == 0)
                    {
                        if (counterEN != EngWord.Length)
                        {
                            word = EngWord[counterEN];
                            counterEN++;
                            English.MustBeHigh = true;
                            Italian.MustBeHigh = false;
                        }
                        else
                        {
                            word = ITWord[counterIT];
                            counterIT++;
                            English.MustBeHigh = false;
                            Italian.MustBeHigh = true;
                        }
                    }
                    else
                    {
                        if (counterIT != ITWord.Length)
                        {
                            word = ITWord[counterIT];
                            counterIT++;
                            English.MustBeHigh = false;
                            Italian.MustBeHigh = true;
                        }
                        else
                        {
                            word = EngWord[counterEN];
                            counterEN++;
                            English.MustBeHigh = true;
                            Italian.MustBeHigh = false;
                        }
                    }

                    for (int characternum = 0; characternum < 8; characternum++)
                    {
                        Wordspace[characternum].SetLetter(word[characternum]);
                    }
                    LanguageNeuralNet.Learn();
                    LanguageNeuralNet.CalculateResults();
                    if (English.Value > Italian.Value)
                    {
                        if (English.MustBeHigh)
                        {
                            Right++;
                        }
                        else
                        {
                            Wrong++;
                        }
                    }
                    else
                    {
                        if (Italian.MustBeHigh)
                        {
                            Right++;
                        }
                        else
                        {
                            Wrong++;
                        }
                    }
                }
                timer.Stop();
                LanguageNeuralNet.SaveMatrix();
            }

            int definitiveRight = 0;
            int definitiveWrong = 0;

            foreach (var i in EngWord)
            {
                for (int characternum = 0; characternum < 8; characternum++)
                {
                    Wordspace[characternum].SetLetter(i[characternum]);
                }
                LanguageNeuralNet.CalculateResults();
                if (English.Value > Italian.Value)
                {
                    definitiveRight++;
                }
                else
                {
                    definitiveWrong++;
                }
            }
            foreach (var i in ITWord)
            {
                for (int characternum = 0; characternum < 8; characternum++)
                {
                    Wordspace[characternum].SetLetter(i[characternum]);
                }
                LanguageNeuralNet.CalculateResults();
                if (Italian.Value > English.Value)
                {
                    definitiveRight++;
                }
                else
                {
                    definitiveWrong++;
                }
            }
            double endPercentage = (double)definitiveRight / (double)(definitiveRight + definitiveWrong) * 100.0;
        }
        public static NeuralNetwork LoadMatrix(List <ArgumentValue> Inputs, List <OutputData> Outputs)
        {
            NeuralNetwork newNeuralNet = new NeuralNetwork();

            System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog();
            open.Filter = "Comma Seperated Values (*.csv)|*.csv";
            //open.FileName = "matrixtest.csv";
            open.ShowDialog();
            StreamReader reader     = new StreamReader(open.FileName);
            string       wholeThing = reader.ReadToEnd();

            reader.Close();

            var lines = wholeThing.Split('\n');

            var splitElements = from line in lines select line.Split(';');

            int lastSize = 1;

            List <List <List <string> > > stringMatrices = new List <List <List <string> > >();

            foreach (var line in splitElements)
            {
                if (line.Length <= 1)
                {
                    lastSize = line.Length;
                }
                else
                {
                    if (line.Length != lastSize)
                    {
                        stringMatrices.Add(new List <List <string> >());
                    }
                    lastSize = line.Length;
                    stringMatrices.Last().Add(line.ToList());
                }
            }

            List <double[, ]> matrices = new List <double[, ]>();

            foreach (var i in stringMatrices)
            {
                var matrix = new double[i[0].Count, i.Count];
                for (int x = 0; x < matrix.GetLength(0); x++)
                {
                    for (int y = 0; y < matrix.GetLength(1); y++)
                    {
                        matrix[x, y] = double.Parse(i[y][x], CultureInfo.InvariantCulture);
                    }
                }
                matrices.Add(matrix);
            }

            if (Inputs.Count != matrices[0].GetLength(0))
            {
                throw new Exception("Inputs do not match with loaded matrix");
            }

            if (Outputs.Count != matrices.Last().GetLength(1))
            {
                throw new Exception("Outputs do not match with loaded matrix");
            }


            foreach (var i in Inputs)//Fill the network with nodes
            {
                newNeuralNet.Input.Add(new InputNeuron(i));
            }
            foreach (var i in Outputs)//Fill the network with nodes
            {
                newNeuralNet.Output.Add(new OutputNeuron(i));
            }

            //Adds inbetweenlayers for all the inbetween layers
            for (int i = 0; i < matrices.Count - 1; i++)
            {
                newNeuralNet.InbetweenLayers.Add(new List <Neuron>());
                for (int y = 0; y < matrices[i].GetLength(1); y++)
                {
                    newNeuralNet.InbetweenLayers[i].Add(new Neuron());
                }
            }

            for (int x = 0; x < matrices[0].GetLength(0); x++)
            {
                for (int y = 0; y < matrices[0].GetLength(1); y++)
                {
                    newNeuralNet.Input[x].LinkTo(newNeuralNet.InbetweenLayers[0][y], new ArgumentValue(matrices[0][x, y]));
                }
            }

            for (int i = 0; i < newNeuralNet.InbetweenLayers.Count - 1; i++)
            {
                for (int y = 0; y < matrices[i + 1].GetLength(1); y++)
                {
                    for (int x = 0; x < matrices[i + 1].GetLength(0); x++)
                    {
                        newNeuralNet.InbetweenLayers[i][x].LinkTo(newNeuralNet.InbetweenLayers[i + 1][y], new ArgumentValue(matrices[i][x, y]));
                    }
                }
            }

            for (int x = 0; x < matrices.Last().GetLength(0); x++)
            {
                for (int y = 0; y < matrices.Last().GetLength(1); y++)
                {
                    newNeuralNet.InbetweenLayers.Last()[x].LinkTo(newNeuralNet.Output[y], new ArgumentValue(matrices.Last()[x, y]));
                }
            }

            newNeuralNet.BuildEquations();

            return(newNeuralNet);
        }
Ejemplo n.º 5
0
 public Trainer(NeuralNetwork neuralNetwork, TrainData trainData)
 {
     NeuralNetwork = neuralNetwork;
     TrainData     = trainData;
 }