Esempio n. 1
0
        private void WriteLearnSamplesToDgv()
        {
            dgvLearningData.Rows.Clear();
            dgvLearningData.Columns.Clear();

            // columns
            dgvLearningData.Columns.Add("lineNo", "Line no.");

            dgvLearningData.Columns[0].ReadOnly = true;
            dgvLearningData.Columns[0].DefaultCellStyle.BackColor = System.Drawing.SystemColors.Control;

            int inputCountWithoutBiases = InputCount;

            for (int col = 0; col < inputCountWithoutBiases; col++)
            {
                dgvLearningData.Columns.Add("in" + col, "input " + (col + 1));
            }

            for (int col = 0; col < OutputCount; col++)
            {
                dgvLearningData.Columns.Add("out" + col, "output " + (col + 1));
            }

            for (int sampleNo = 0; sampleNo < learnSamples.Count; sampleNo++)
            {
                LearnSample sample = learnSamples.ElementAt(sampleNo);

                String[] tab = new String[inputCountWithoutBiases + OutputCount + 1];

                int tabIdx = 0;
                tab[tabIdx++] = (sampleNo + 1).ToString();

                for (int inputNo = 0; inputNo < inputCountWithoutBiases; inputNo++)
                {
                    tab[tabIdx++] = sample.InputData.ElementAt(inputNo).ToString();
                }

                for (int outputNo = 0; outputNo < OutputCount; outputNo++)
                {
                    tab[tabIdx++] = sample.OutputData.ElementAt(outputNo).ToString();
                }

                dgvLearningData.Rows.Add(tab);
            }

            foreach (DataGridViewColumn col in dgvLearningData.Columns)
            {
                col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            }

            dgvLearningData.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            SetRowsForLearningSamples();
        }
Esempio n. 2
0
        private static bool LoadTeachingElements(int inputCount, int outputCount, out List <LearnSample> LearningData,
                                                 String filename)
        {
            LearningData = new List <LearnSample>();
            bool ret = true;

            try
            {
                StreamReader sr = new StreamReader(filename);

                String line   = sr.ReadLine();
                int    lineNo = 1;
                while (line != null)
                {
                    if (line.Length > 0)
                    {
                        line = line.Replace('.', ',');

                        String[] tabString = line.Split(';'); // yes, I force delimiter as ';'

                        if (tabString.Length != inputCount + outputCount)
                        {
                            throw new Exception("Difference between input/output count and data length in line " + lineNo +
                                                ". Elements in line: " + tabString.Length + ", inputs + outputs: " + (inputCount + outputCount) + ".");
                        }

                        double[] tabDouble = new double[tabString.Length];

                        try
                        {
                            for (int i = 0; i < tabDouble.Length; i++)
                            {
                                tabDouble[i] = double.Parse(tabString[i]);
                            }
                        }
                        catch (Exception)
                        {
                            throw new Exception("Error during parse value to double in line " + lineNo);
                        }

                        double[] inputs  = SubArray(tabDouble, 0, inputCount);
                        double[] outputs = SubArray(tabDouble, inputCount, tabDouble.Length - inputCount);

                        // TODO: refactory needed - I should be able to send inputs and outputs in constructor
                        LearnSample teachingElement = new LearnSample(outputs.Length);
                        teachingElement.InputData  = inputs.ToList();
                        teachingElement.OutputData = outputs;

                        LearningData.Add(teachingElement);
                    }

                    line = sr.ReadLine();
                    lineNo++;
                }

                sr.Close();
                ret = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error on reading file " + filename + ": " + ex.Message);
                ret = false;
            }

            return(ret);
        }
Esempio n. 3
0
        private static bool LearnNetwork(NeuralNetwork neuralNetwork, bool networkUseBias, List <LearnSample> learningData, double learningRatio, double momentum,
                                         double minErrorPercentageToStop, long maxEpochNo, bool randomOrderOfLearningData, long writeOnConsoleEveryEpoch, bool keyEscapeInterrupt)
        {
            List <LearnSample> learningDataToUse = learningData;

            Console.WriteLine();

            if (randomOrderOfLearningData)
            {
                Console.WriteLine("Randomize order of learning data...");
                learningDataToUse = ShuffleList(learningData);
            }

            Console.WriteLine("Starting to learn network (press F2 to stop)...");
            bool doLearningProcess = true;

            Console.WriteLine();
            Console.WriteLine("   Epoch no.     Network error   Network error change");

            try
            {
                List <double> errorList        = new List <double>();
                double        lastNetworkError = 1;
                long          epochNo          = 0;
                String        stopComment      = null;
                bool          stopedByKey      = false;

                while (doLearningProcess)
                {
                    for (int i = 0; i < learningData.Count; i++)
                    {
                        LearnSample learnSample = learningDataToUse.ElementAt(i);
                        //neuralNetwork.Learn()
                        String outStringErrorMessage = null;
                        neuralNetwork.Learn(learnSample, learningRatio, momentum, out outStringErrorMessage, epochNo, networkUseBias);

                        if (outStringErrorMessage != null)
                        {
                            throw new Exception(outStringErrorMessage);
                        }

                        epochNo++;

                        // TODO: maybe using additional bool variable "isMaxEpochNoSet" will increase performance by comparing bool not doubles?
                        if (maxEpochNo > 0)
                        {
                            if (epochNo >= maxEpochNo)
                            {
                                stopComment = String.Format(
                                    "Learning stopped because of epoch number reached (epochno = {0}).",
                                    epochNo
                                    );
                                doLearningProcess = false;
                            }
                        }

                        // TODO: maybe using additional bool variable "isMinErrorSet" will increase performance by comparing bool not doubles?
                        if (minErrorPercentageToStop > 0)
                        {
                            if (neuralNetwork.NetworkError < minErrorPercentageToStop / 100)
                            {
                                stopComment = String.Format(
                                    "Learning stopped because of minimum percentage of network error reached (network error = {0, 4:0.000}%).",
                                    neuralNetwork.NetworkError * 100
                                    );
                                doLearningProcess = false;
                            }
                        }

                        if (keyEscapeInterrupt)
                        {
                            if (Console.KeyAvailable)
                            {
                                ConsoleKeyInfo cki = Console.ReadKey();
                                if (cki.Key == ConsoleKey.F2)
                                {
                                    doLearningProcess = false;
                                    stopComment       = "Learning stopped by pressing F2 key.";
                                    stopedByKey       = true;
                                }
                            }
                        }

                        if (epochNo % writeOnConsoleEveryEpoch == 0 || !doLearningProcess)
                        {
                            Console.WriteLine(
                                String.Format(
                                    (stopedByKey ? " {0, 10}" : " {0,11}") + " {1,16:0.000}% {2,21:0.000} per mille ",
                                    epochNo,
                                    neuralNetwork.NetworkError * 100,
                                    (neuralNetwork.NetworkError - lastNetworkError) * 1000));

                            errorList.Add(neuralNetwork.NetworkError);
                            lastNetworkError = neuralNetwork.NetworkError;

                            if (!doLearningProcess)
                            {
                                if (stopComment != null)
                                {
                                    Console.WriteLine();
                                    Console.WriteLine(stopComment);
                                }

                                break;
                            }
                        }
                    }
                }
            }
            catch (NotImplementedException /*FormatException*/ e)
            {
                Console.WriteLine("Error during learning: " + e.Message + ". Learning stopped.");

                return(false);
            }

            return(true);
        }
Esempio n. 4
0
        public List <LearnSample> GetLearnSamples()
        {
            //PaintRowsNormalDgvLearning();
            bool allOk = true;

            if (rbLearnAllData.Checked)
            {
                return(learnSamples); // TODO: Now getting only readed from file, not from dgv
            }
            else
            {
                #region Validating and getting choosen rows indexes
                List <int> choosenRowIndexes = new List <int>();

                foreach (DataGridViewCell cell in dgvLearningData.SelectedCells)
                {
                    if (!choosenRowIndexes.Contains(cell.RowIndex))
                    {
                        choosenRowIndexes.Add(cell.RowIndex);
                    }
                }

                if (choosenRowIndexes.Count == 0)
                {
                    return(null);
                }

                for (int choosenRowIdx = 0; choosenRowIdx < choosenRowIndexes.Count; choosenRowIdx++)
                {
                    int row = choosenRowIndexes.ElementAt(choosenRowIdx);

                    bool rowOk = true;

                    for (int cell = 1; cell < dgvLearningData.ColumnCount; cell++)
                    {
                        if (dgvLearningData.Rows[row].Cells[cell].Value == null && IsNetworkUsingBiases && dgvLearningData.Columns[cell].Visible)
                        {
                            rowOk = false;
                            break;
                        }

                        String input = dgvLearningData.Columns[cell].Visible ? dgvLearningData.Rows[row].Cells[cell].Value.ToString() : "1";
                        try
                        {
                            double doubleValue = Double.Parse(input);
                        }
                        catch (Exception)
                        {
                            rowOk = false;
                            break;
                        }
                    }

                    if (!rowOk)
                    {
                        allOk = false;

                        PaintRowRedDgvLearning(row);
                    }
                }

                if (!allOk)
                {
                    MessageBox.Show(this, "Inside red - colored rows are errors - correct it before learning!",
                                    "Wrong value(s)", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return(null);
                }
                #endregion

                #region Create list of learn samples using choosen rows indexes
                List <LearnSample> returnLearnSamples = new List <LearnSample>();

                for (int i = 0; i < choosenRowIndexes.Count; i++)
                {
                    int         row         = choosenRowIndexes.ElementAt(i);
                    LearnSample learnSample = new LearnSample(OutputCount);

                    for (int inputNo = 0; inputNo < InputCount; inputNo++)
                    {
                        String input = dgvLearningData.Rows[row].Cells[inputNo + 1].Value.ToString();
                        try
                        {
                            double doubleValue = Double.Parse(input);
                            learnSample.InputData.Add(doubleValue);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(this, input + " is not a double value! Learning doesn't start.", "Wrong value", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return(null);
                        }
                    }

                    for (int outputNo = 0; outputNo < OutputCount; outputNo++)
                    {
                        String output = dgvLearningData.Rows[row].Cells[1 + InputCount + outputNo].Value.ToString();
                        try
                        {
                            double doubleValue = Double.Parse(output);
                            learnSample.OutputData[outputNo] = doubleValue;
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(this, output + " is not a double value! Learning doesn't start.", "Wrong value", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return(null);
                        }
                    }

                    returnLearnSamples.Add(learnSample);
                }
                #endregion

                return(returnLearnSamples);
            }
        }
Esempio n. 5
0
        private void ShowVisualization(int dataLearnItemIdx)
        {
            if (dld == null || dataLearnItemIdx == -1 || dataLearnItemIdx > learnSamples.Count - 1)
            {
                dgvInputData.Rows.Clear();
                dgvInputData.Columns.Clear();

                dgvOutputData.Rows.Clear();
                dgvOutputData.Columns.Clear();
            }
            else
            {
                LearnSample learnSample = learnSamples.ElementAt(dataLearnItemIdx);

                #region Input and ouptup dgv creation

                Size inputDimension  = dld.InputPossibleDimensions[dld.InputDimensionIndex];
                Size outputDimension = dld.OutputPossibleDimensions[dld.OutputDimensionIndex];

                if (dgvInputData.ColumnCount == 0)
                {
                    for (int columnNo = 0; columnNo < inputDimension.Width; columnNo++)
                    {
                        dgvInputData.Columns.Add("c" + columnNo, "c" + columnNo);
                        dgvInputData.Columns[dgvInputData.ColumnCount - 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    }

                    //String[] tab = new string[inputDimension.Width];

                    for (int rowNo = 0; rowNo < inputDimension.Height; rowNo++)
                    {
                        dgvInputData.Rows.Add();
                    }
                }

                if (dgvOutputData.ColumnCount == 0)
                {
                    for (int columnNo = 0; columnNo < outputDimension.Width; columnNo++)
                    {
                        dgvOutputData.Columns.Add("c" + columnNo, "c" + columnNo);
                        dgvOutputData.Columns[dgvOutputData.ColumnCount - 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    }

                    //String[] tab = new string[inputDimension.Width];

                    for (int rowNo = 0; rowNo < outputDimension.Height; rowNo++)
                    {
                        dgvOutputData.Rows.Add();
                    }
                }
                #endregion

                int elementNo = 0;
                for (int columnNo = 0; columnNo < inputDimension.Width; columnNo++)
                {
                    for (int rowNo = 0; rowNo < inputDimension.Height; rowNo++)
                    {
                        dgvInputData[columnNo, rowNo].Value = UseMapping(learnSample.InputData[elementNo++].ToString(), dld.MappedInputs);
                    }
                }

                elementNo = 0;
                for (int columnNo = 0; columnNo < outputDimension.Width; columnNo++)
                {
                    for (int rowNo = 0; rowNo < outputDimension.Height; rowNo++)
                    {
                        dgvOutputData[columnNo, rowNo].Value = UseMapping(learnSample.OutputData[elementNo++].ToString(), dld.MappedOutputs);
                    }
                }
            }

            if (dgvInputData.SelectedCells.Count > 0)
            {
                dgvInputData.SelectedCells[0].Selected = false;
            }
            if (dgvOutputData.SelectedCells.Count > 0)
            {
                dgvOutputData.SelectedCells[0].Selected = false;
            }
        }
Esempio n. 6
0
        private void BReadingDataFile_Click(object sender, EventArgs e)
        {
            if (ofdOpen.ShowDialog() == DialogResult.OK)
            {
                List <string> fileContent = new List <string>();

                tbFileName.Text = "Loading learning data, please wait...";
                tbFileName.Refresh();

                FileInfo     fiLearnFile = new FileInfo(ofdOpen.FileName);
                StreamReader sr          = new StreamReader(fiLearnFile.FullName);

                String line = sr.ReadLine();
                while (line != null)
                {
                    if (line.Length > 0)
                    {
                        fileContent.Add(line);
                    }

                    line = sr.ReadLine();
                }
                sr.Close();

                // validate content
                if (fileContent.Count == 0)
                {
                    MessageBox.Show(this, "File is empty!", "Wrong file content", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                String[] tab = fileContent.ElementAt(0).Split(new char[] { ';' });
                int      inputCountWithoutBiases = InputCount;

                if (tab.Length != inputCountWithoutBiases + OutputCount)
                {
                    MessageBox.Show(this, "Quantity of data in row not equal sum of neurons input and output layer!", "Wrong file content", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                learnSamples.Clear();
                for (int contentLineNo = 0; contentLineNo < fileContent.Count; contentLineNo++)
                {
                    // validation line
                    tab = fileContent.ElementAt(contentLineNo).Split(new char[] { ';' });

                    if (tab.Length != inputCountWithoutBiases + OutputCount)
                    {
                        MessageBox.Show(this, "Quantity of data in row not equal sum of neurons input and output in line number " + contentLineNo + "!",
                                        "Wrong file content", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        learnSamples.Clear();
                        return;
                    }
                    else
                    {
                        try
                        {
                            for (int i = 0; i < tab.Length; i++)
                            {
                                Double.Parse(tab[i]);
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(this, "Some of data are not double values in line number " + contentLineNo + "!",
                                            "Wrong file content", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            learnSamples.Clear();
                            return;
                        }
                    }

                    // read data
                    int dataNo = 0;

                    LearnSample learnSample = new LearnSample(OutputCount);

                    for (; dataNo < inputCountWithoutBiases; dataNo++)
                    {
                        learnSample.InputData.Add(Double.Parse(tab[dataNo]));
                    }

                    int outputNo = 0;
                    for (; dataNo < tab.Length; dataNo++)
                    {
                        learnSample.OutputData[outputNo++] = Double.Parse(tab[dataNo]);
                    }

                    learnSamples.Add(learnSample);
                }

                FileInfo dld = new FileInfo(fiLearnFile.FullName.Replace(fiLearnFile.Extension, ".dld"));
                if (dld.Exists)
                {
                    ReadDldFile(dld);
                }

                if (cbShowData.Checked)
                {
                    WriteLearnSamplesToDgv();
                }

                tbFileName.Text = fiLearnFile.FullName;
            }
        }
Esempio n. 7
0
        private void BwLearning_DoWork(object sender, DoWorkEventArgs e)
        {
            epochNo = 0;
            int    sampleNo     = 0;
            bool   errorGreater = true;
            Random random       = new Random();
            double averageSum   = 0;
            ulong  averageCnt   = 0;

            averageError = -1;

            bwError = null;

            if (!Network.PreviousInputsSet)
            {
                Network.PrepareToLearn();
            }

            while (((bLearnXTimes && epochNo < epochMax) ||
                    (bLearnUntilMinError && errorGreater)) &&
                   bwRunning)
            {
                LearnSample learnSample = null;

                if (bUsingDataRandom)
                {
                    sampleNo    = random.Next(0, MainWindow.ucDialogLearningData.GetLearnSamples().Count);
                    learnSample = MainWindow.ucDialogLearningData.GetLearnSamples().ElementAt(sampleNo);
                }
                else if (rbUsingDataInSequence.Checked)
                {
                    learnSample = MainWindow.ucDialogLearningData.GetLearnSamples().ElementAt(sampleNo);
                }

                Network.Learn(learnSample, learningRatio, momentum, out string errorMessage, epochNo);

                if (errorMessage != null)
                {
                    bwError = "Learning stopped in epoch no. " + (epochNo + 1) + " becouse of error: " + errorMessage;
                    break;
                }

                errorGreater = (averageError > minimumError || averageError == -1);

                epochNo++;

                if (bUsingDataInSequence)
                {
                    sampleNo++;
                    if (sampleNo == MainWindow.ucDialogLearningData.GetLearnSamples().Count)
                    {
                        sampleNo = 0;
                    }
                }

                if (bLearnXTimes && epochNo % 100 == 0)
                {
                    bwLearning.ReportProgress((epochNo * 100) / epochMax);
                }

                if (averageCnt < MAX_AVERAGE_ERROR_RANGE)
                {
                    averageSum += Network.NetworkError;
                    averageCnt++;
                }
                else
                {
                    averageError = averageSum / averageCnt;
                    averageSum   = 0;
                    averageCnt   = 0;
                }
            }
        }