Esempio n. 1
0
        public static TeachingSample[] generateTeachingSamplesFromFile(string file)
        {
            List <TeachingSample> samples = new List <TeachingSample>();

            try
            {
                StreamReader reader = File.OpenText(file);

                string current_str;

                while (!reader.EndOfStream)
                {
                    current_str = reader.ReadLine();
                    string[] values = current_str.Split(':');

                    string input_str  = values[0].Trim();
                    string output_str = values[1].Trim();

                    string[] input_strs  = input_str.Split(',');
                    string[] output_strs = output_str.Split(',');

                    TeachingSample sample = new TeachingSample(input_strs.Length, output_strs.Length);

                    foreach (var input in input_strs)
                    {
                        sample.input_values[Array.IndexOf(input_strs, input)] = Convert.ToDouble(input);
                    }
                    foreach (var output in output_strs)
                    {
                        sample.desired_outputs[Array.IndexOf(output_strs, output)] = Convert.ToDouble(output);
                    }

                    samples.Add(sample);
                }
            }
            catch (Exception)
            {
                return(null);
            }
            return(samples.ToArray());
        }
Esempio n. 2
0
            private static void back_propagation(List <Neuron[]> ann_layers, double precision, TeachingSample teaching_sample, Teacher teacher)
            {
                Neuron[] input_layer  = ann_layers.Find(layer => layer is InputNeuron[]);
                Neuron[] hidden_layer = ann_layers.Find(layer => !(layer is InputNeuron[]) && !(layer is OutputNeuron[]));
                Neuron[] output_layer = ann_layers.Find(layer => layer is OutputNeuron[]);

                double[] deltas_output = new double[output_layer.Length];
                double[] deltas_hidden = new double[hidden_layer.Length];

                foreach (var neuron in output_layer)
                {
                    double output = neuron.getAxonForTeacher(teacher).value;
                    double target = teaching_sample.desired_outputs[Array.IndexOf(output_layer, neuron)];
                    deltas_output[Array.IndexOf(output_layer, neuron)] = output * (1 - output) * (target - output);
                }

                double[] new_weights_output = new double[output_layer.Length * hidden_layer.Length];
                int      ind = 0;

                foreach (var neuron in output_layer)
                {
                    foreach (var synapse in neuron.getSynapsesForTeacher(teacher))
                    {
                        double last_weight = synapse.getWeightAsTeacher(teacher);
                        double new_weight  = last_weight + (precision * deltas_output[Array.IndexOf(output_layer, neuron)] * synapse.axon.value);
                        new_weights_output[ind] = new_weight;
                        ind++;
                    }
                }

                foreach (var neuron in hidden_layer)
                {
                    double output = neuron.getAxonForTeacher(teacher).value;
                    double sum    = 0;

                    foreach (var out_neuron in output_layer)
                    {
                        double last_error = out_neuron.getSynapsesForTeacher(teacher).Find(syn => syn.axon == neuron.getAxonForTeacher(teacher)).getWeightAsTeacher(teacher);
                        sum += deltas_output[Array.IndexOf(output_layer, out_neuron)] * last_error;
                    }

                    deltas_hidden[Array.IndexOf(hidden_layer, neuron)] = output * (1 - output) * sum;
                }

                foreach (var neuron in hidden_layer)
                {
                    foreach (var synapse in neuron.getSynapsesForTeacher(teacher))
                    {
                        double last_weight = synapse.getWeightAsTeacher(teacher);
                        double new_weight  = last_weight + (precision * deltas_hidden[Array.IndexOf(hidden_layer, neuron)] * synapse.axon.value);
                        synapse.recalculateWeightAsTeacher(teacher, new_weight);
                    }
                }

                ind = 0;

                foreach (var neuron in output_layer)
                {
                    foreach (var synapse in neuron.getSynapsesForTeacher(teacher))
                    {
                        synapse.recalculateWeightAsTeacher(teacher, new_weights_output[ind]);
                        ind++;
                    }
                }
            }
Esempio n. 3
0
        private void bGo_Click(object sender, EventArgs e)
        {
            try
            {
                TeachingSample[] samples = null;

                if (rbText.Checked)
                {
                    MessageBox.Show("Please, choose text file to generate teaching samples");

                    OpenFileDialog dlg = new OpenFileDialog();
                    dlg.ShowDialog();

                    if (dlg.FileName != null && dlg.FileName != "")
                    {
                        samples = TeachingSample.generateTeachingSamplesFromFile(dlg.FileName);

                        if (samples == null)
                        {
                            throw new Exception();
                        }
                    }
                }
                else if (rbImage.Checked)
                {
                    MessageBox.Show("Please, choose images to generate teaching samples");

                    OpenFileDialog dlg = new OpenFileDialog();
                    dlg.Multiselect = true;
                    dlg.ShowDialog();
                    if (dlg.FileNames.Length > 0)
                    {
                        samples = new TeachingSample[dlg.FileNames.Length];
                        for (int i = 0; i < samples.Length; i++)
                        {
                            List <string[]> anns_info = ann_manager.getTableInfo();

                            int input_length  = 0;
                            int output_length = 0;
                            foreach (var info in anns_info)
                            {
                                if (info[0] == ann_manager.getSelectedANNName())
                                {
                                    input_length  = Convert.ToInt32(info[1]);
                                    output_length = Convert.ToInt32(info[3]);
                                    break;
                                }
                            }

                            samples[i] = new TeachingSample(input_length, output_length);
                            if (!samples[i].generateTeachingSampleFromImage(dlg.FileNames[i]))
                            {
                                MessageBox.Show("one of the files selected is bad");
                                return;
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("No files for teaching sample selected!");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("An odd situation occured. This is strange as f**k (also as div by 0), and program cant proceed...");
                    throw new Exception();
                }

                if (samples != null)
                {
                    double precision  = Convert.ToDouble(textBox2.Text);
                    int    iterations = Convert.ToInt32(textBox1.Text);

                    Close();

                    string teachingResults = ann_manager.teachANN(precision, samples, iterations);

                    MessageBox.Show("Teaching results: " + teachingResults);
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception) { MessageBox.Show("An error occured. Probably, you entered wrong values, or choose wrong file(s)"); };
        }