Пример #1
0
        internal void train(UIElementCollection children)
        {
            bool[] input = new bool[16 * 16];

            foreach (Rectangle rect in children)
            {
                int top  = Convert.ToInt32(rect.GetValue(Canvas.TopProperty));
                int left = Convert.ToInt32(rect.GetValue(Canvas.LeftProperty));

                input[top * 16 + left] = true;
            }

            bool[]       output       = (from e in oneHotEncoding where e.Value == trainChar select e.Key).First();
            TrainingData trainingData = new TrainingData()
            {
                input = input, output = output
            };

            training.Add(trainingData);
            singleLayerPerceptron.train(input, output);
        }
Пример #2
0
        public MainWindowViewModel()
        {
            foreach (char c in alphabet)
            {
                bool[] code = new bool[alphabet.Length];
                code[alphabet.IndexOf(c)] = true;
                oneHotEncoding.Add(code, c);
            }

            try
            {
                using (FileStream fs = File.Open(slpFilePath, FileMode.Open))
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    singleLayerPerceptron = (SingleLayerPerceptron)binaryFormatter.Deserialize(fs);
                }
            }
            catch (Exception e)
            {
                singleLayerPerceptron = new SingleLayerPerceptron(16 * 16, alphabet.Length);
                try
                {
                    #region Load Trainingdata and Train Single Layer Perceptron
                    List <string> lines = new List <string>();
                    using (StreamReader streamReader = new StreamReader(File.Open(trainingFilePath, FileMode.OpenOrCreate)))
                    {
                        while (!streamReader.EndOfStream)
                        {
                            lines.Add(streamReader.ReadLine());
                        }
                    }

                    foreach (string line in lines)
                    {
                        TrainingData trainingData = new TrainingData();
                        string[]     splittedLine = line.Split(' ');
                        string       inputString  = splittedLine[0];
                        string       outputString = splittedLine[1];

                        bool[] input = new bool[inputString.Length];
                        for (int i = 0; i < inputString.Length - 1; i++)
                        {
                            if (inputString[i] == '0')
                            {
                                input[i] = false;
                            }
                            else if (inputString[i] == '1')
                            {
                                input[i] = true;
                            }
                        }
                        trainingData.input = input;

                        //Doublecoding :(
                        bool[] output = new bool[splittedLine[1].Length];
                        for (int i = 0; i < outputString.Length - 1; i++)
                        {
                            if (outputString[i] == '0')
                            {
                                output[i] = false;
                            }
                            else if (outputString[i] == '1')
                            {
                                output[i] = true;
                            }
                        }
                        trainingData.output = output;

                        training.Add(trainingData);
                    }

                    foreach (var item in training)
                    {
                        singleLayerPerceptron.train(item.input, item.output);
                    }
                    #endregion
                }
                catch (Exception)
                {
                    //Nothing special happens here since we initialized training as new list already.
                }
            }
        }