Esempio n. 1
0
    // Start is called before the first frame update
    void Start()
    {
        StreamReader streamReader = new StreamReader(path);

        while (!streamReader.EndOfStream)
        {
            double[] datosEntrada = new double[10];
            for (int i = 0; i < datosEntrada.Length; i++)
            {
                string linea = streamReader.ReadLine();
                datosEntrada[i] = System.Double.Parse(linea);
            }
            double[] datosSalida = new double[14];
            for (int i = 0; i < datosSalida.Length; i++)
            {
                string linea = streamReader.ReadLine();
                datosSalida[i] = double.Parse(linea);
            }
            NeuralNetwork.DataSet dataSet = new NeuralNetwork.DataSet(datosEntrada, datosSalida);
            dataSets.Add(dataSet);
        }
        net.Train(dataSets, 0.1);

        nombresAlumnos[0]  = "Orlando";
        nombresAlumnos[1]  = "DOC";
        nombresAlumnos[2]  = "Alex";
        nombresAlumnos[3]  = "Fernando";
        nombresAlumnos[4]  = "Raul";
        nombresAlumnos[5]  = "Luis";
        nombresAlumnos[6]  = "Edgardo";
        nombresAlumnos[7]  = "Mauricio";
        nombresAlumnos[8]  = "Mauro";
        nombresAlumnos[9]  = "Juan";
        nombresAlumnos[10] = "Alda";
        nombresAlumnos[11] = "Martin";
        nombresAlumnos[12] = "Arturo";
        nombresAlumnos[13] = "Chema";

        preguntas[0] = "¿Usa lentes?";
        preguntas[1] = "¿Es foraneo?";
        preguntas[2] = "¿Es de cabello chino?";
        preguntas[3] = "¿Es gordo?";
        preguntas[4] = "¿Es programador?";
        preguntas[5] = "¿Es artista?";
        preguntas[6] = "¿Fuma?";
        preguntas[7] = "¿Se encuentra realizando sus prácticas profesionales?";
        preguntas[8] = "¿Es alto?";
        preguntas[9] = "¿Tiene barba?";

        pregunta.text = preguntas[0];
    }
Esempio n. 2
0
    private void Awake()
    {
        instance = this;
        thisNet  = new NeuralNetwork.NeuralNet(4, 3, 1);

        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 1.0, 1.0, 0.5 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 0.0, 1.0, 0.5 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 1.0, 0.0, 0.5 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 0.0, 0.0, 0.5 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 1.0, 1.0, 0.5 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 0.0, 1.0, 0.5 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 1.0, 0.0, 0.5 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 0.0, 0.0, 0.5 }, new double[] { 0.5 }));

        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 1.0, 1.0, 0.1 }, new double[] { 0.1 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 1.0, 0.0, 0.1 }, new double[] { 0.1 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 0.5, 0.0, 0.1 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 0.5, 1.0, 0.1 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 1.0, 1.0, 0.1 }, new double[] { 0.1 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 1.0, 0.0, 0.1 }, new double[] { 0.1 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 0.5, 0.0, 0.1 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 0.5, 1.0, 0.1 }, new double[] { 0.5 }));

        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 1.0, 1.0, 0.9 }, new double[] { 0.9 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 0.0, 1.0, 0.9 }, new double[] { 0.9 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 1.0, 0.5, 0.9 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 1.0, 0.0, 0.5, 0.9 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 1.0, 1.0, 0.9 }, new double[] { 0.9 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 0.0, 1.0, 0.9 }, new double[] { 0.9 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 1.0, 0.5, 0.9 }, new double[] { 0.5 }));
        DataSets.Add(new NeuralNetwork.DataSet(new double[] { 0.0, 0.0, 0.5, 0.9 }, new double[] { 0.5 }));

        thisNet.Train(DataSets, 10);
    }
    private void OnEnable()
    {
        net      = new NeuralNet(3, 1, 1);
        dataSets = new List <DataSet>();
        Debug.Log("inicializando");

        double[] inputs        = new double[3];
        double[] desireOutputs = new double[1];
        inputs[0] = 0.1f;
        inputs[1] = 0.1f;
        inputs[2] = 0.1f;

        desireOutputs[0] = 0.1f;
        dataSets.Add(new DataSet(inputs, desireOutputs));

        net.Train(dataSets, 0.001f);

        double[] vals = new double[3];
        vals[0] = 0.1f;
        vals[1] = 0.1f;
        vals[2] = 0.9f;
        double[] respuesta = net.Compute(vals);
        Debug.Log(respuesta[0]);

        if (respuesta[0] < 0.09f)
        {
            col = 0;
        }
        if (respuesta[0] > 0.09f && respuesta[0] < 0.1)
        {
            col = 1;
        }
        if (respuesta[0] > 0.1f)
        {
            col = 2;
        }

        sonidoNotificacion.SetActive(true);
        indice++;
        botones[0].SetActive(true);
        botones[1].SetActive(true);
        botones[2].SetActive(true);
        cajasMensaje[indice - 1].SetActive(true);
        textoMensaje[indice - 1].text = maquina.mensajesMaquina[indice - 1, col];
        textoRespuesta[0].text        = maquina.mensajesUsuario[indice - 1, 0];
        textoRespuesta[1].text        = maquina.mensajesUsuario[indice - 1, 1];
        textoRespuesta[2].text        = maquina.mensajesUsuario[indice - 1, 2];
        if (indice == 6)
        {
            maquina.ActivarEstado(maquina.EstadoCaptura);
        }
    }
Esempio n. 4
0
        private static void TrainXORTest(NeuralNet NN)
        {
            double[][] TrainData =
            {
                new double[] { 0, 1 },
                new double[] { 1, 1 },
                new double[] { 1, 0 },
                new double[] { 0, 0 },
            };

            var Labels = new double[][]
            {
                new double[] { 0 },
                new double[] { 1 },
                new double[] { 0 },
                new double[] { 0 },
            };

            for (int i = 1; i < 10000; i++)
            {
                TrainData.Shuffle();
                NN.Train(TrainData, Labels, 0.01);
            }
        }
Esempio n. 5
0
        public TrainAndTestPage(NeuralNetRunType runtype, NeuralNet neuralnet, MNISTDataManager mnistdata)
        {
            // Define GUI

            // Page properties
            Title           = string.Format("{0} Net", runtype.ToString().ToUpperFirstOnly());
            BackgroundColor = Color.SteelBlue;
            Padding         = new Thickness(Application.Current.MainPage.Width * 0.05, Application.Current.MainPage.Height * 0.05);

            // Label for description
            Label description = new Label
            {
                Text   = string.Format("Please define the number of data sets from the MNIST {0} data base to be used for {0}ing. Each data set represents one digit. The {0} data base holds about {1:N0} data sets in total.", runtype.ToString(), mnistdata.CountData),
                Margin = new Thickness(0, 0, 0, Application.Current.MainPage.Height * 0.075)
            };

            // Label for number of training / testing sets used
            Label label_useddatasets = new Label
            {
                Text   = String.Format("{0:N0} data set{1} selected", mnistdata.UsedDataSets, mnistdata.UsedDataSets == 0 ? "" : "s"),
                Margin = new Thickness(0, 0, 0, Application.Current.MainPage.Height * 0.075)
            };

            // Slider for number of data sets used for training / testing
            Slider slider_useddatasets = new Slider(1, mnistdata.CountData, mnistdata.UsedDataSets);

            slider_useddatasets.ValueChanged += (s, e) =>
            {
                mnistdata.UsedDataSets  = (int)e.NewValue;
                label_useddatasets.Text = String.Format("{0:N0} data set{1} selected", mnistdata.UsedDataSets, mnistdata.UsedDataSets == 0 ? "" : "s");
            };

            // Progress bar
            ProgressBar progressbar = new ProgressBar {
                Progress = 0
            };

            // Label for showing progress
            Label label_progress = new Label
            {
                Text = "Currently no calculation running",
            };

            // Button to start training / testing
            Button button = new Button
            {
                Text          = string.Format("Start {0}ing", runtype.ToString().ToUpperFirstOnly()),
                WidthRequest  = Application.Current.MainPage.Width * 0.5,
                HeightRequest = Application.Current.MainPage.Height * 0.10
            };

            // Define page
            Content = new ScrollView
            {
                Content = new StackLayout
                {
                    Children =
                    {
                        description,
                        label_useddatasets,
                        slider_useddatasets,
                        button,
                        progressbar,
                        label_progress
                    }
                }
            };

            // Button event
            // Here training and test sessions are executed
            button.Clicked += async(s, e) =>
            {
                // Check if another calculation is currently running
                if (activerun == true)
                {
                    var answer = await DisplayAlert("Warning", "Do you want to stop current calculation?", "Yes", "No");

                    if (answer)
                    {
                        endrun = true;
                    }
                    return;
                }

                // Check if selected data set is big
                if (mnistdata.UsedDataSets > mnistdata.UsedDataSetsWarning)
                {
                    bool answer = await DisplayAlert("Warning", "Please be aware that big data sets effect run time. Continue?", "Yes", "No");

                    if (!answer)
                    {
                        return;
                    }
                }

                // Set activerun and endrun flags
                activerun = true;
                endrun    = false;

                // Disable Back button
                NavigationPage.SetHasBackButton(this, false);

                // Rename Button to Stop
                button.Text = string.Format("Stop {0}ing", runtype.ToString().ToUpperFirstOnly());

                // Define parameters for progress bar calculation (increase bar 100 times or each 20 runs)
                int progresssteps = Math.Max(100, mnistdata.UsedDataSets / 20);
                int progressspan  = (int)Math.Ceiling((double)mnistdata.UsedDataSets / progresssteps);

                // Counter for training / testing runs
                int i = 0;

                // Initialize progress bar
                label_progress.Text = i.ToString() + " / " + mnistdata.UsedDataSets.ToString();
                await progressbar.ProgressTo((double)i / mnistdata.UsedDataSets, 1, Easing.Linear);

                // Train neural net
                if (runtype == NeuralNetRunType.train)
                {
                    while (i < mnistdata.UsedDataSets && endrun == false)
                    {
                        // Counter for runs in progress step
                        int j = 0;

                        // Training loop for one progress step done as task
                        await Task.Run(() =>
                        {
                            while (j < progressspan && i < mnistdata.UsedDataSets)
                            {
                                // Train net with data set i
                                neuralnet.Train(mnistdata.Input(i), mnistdata.Output(i));

                                j++;
                                i++;
                            }

                            // Remember number of training data sets
                            neuralnet.TrainingDataCounter += j;
                        });

                        // Update progress bar
                        label_progress.Text = i.ToString() + " / " + mnistdata.UsedDataSets.ToString();
                        await progressbar.ProgressTo((double)i / mnistdata.UsedDataSets, 1, Easing.Linear);
                    }

                    // Show mesage
                    await DisplayAlert("Result", string.Format("Neural net trained with {0:N0} data sets", i), "OK");
                }

                if (runtype == NeuralNetRunType.test)
                {
                    // Setup scorecard for capturing test results
                    // digittotal counts all trained figures, digitcorrect the ones that are identified correctly
                    Vector <double> digittotal = Vector <double> .Build.Dense(10);

                    Vector <double> digitcorrect = Vector <double> .Build.Dense(10);

                    // Testing loop
                    while (i < mnistdata.UsedDataSets && endrun == false)
                    {
                        // Counter for runs in progress step
                        int j = 0;

                        // Testing loop for one progress step done as task
                        await Task.Run(() =>
                        {
                            while (j < progressspan && i < mnistdata.UsedDataSets)
                            {
                                // Increase counter for testet digit (0..9)
                                int number = mnistdata.Number(i);
                                digittotal[number]++;

                                // Ask net
                                Vector <double> answer = neuralnet.Query(mnistdata.Input(i));

                                // Check if it's correct
                                if (answer.AbsoluteMaximumIndex() == number)
                                {
                                    digitcorrect[number]++;
                                }

                                j++;
                                i++;
                            }

                            // Remember number of training data sets
                            neuralnet.TrainingDataCounter += j;
                        });

                        // Update progress bar
                        label_progress.Text = i.ToString() + " / " + mnistdata.UsedDataSets.ToString();
                        await progressbar.ProgressTo((double)i / mnistdata.UsedDataSets, 1, Easing.Linear);
                    }

                    // Remeber performance result
                    neuralnet.Performance.Add(digitcorrect.Sum() / digittotal.Sum());

                    // Show test results
                    await Navigation.PushAsync(new ResultsMNISTPage(neuralnet, mnistdata, digittotal, digitcorrect));
                }

                // Reset progress bar
                label_progress.Text = "Currently no calculation running";
                await progressbar.ProgressTo(0.0, 250, Easing.Linear);

                // Rename Button to Start
                button.Text = string.Format("Start {0}ing", runtype.ToString().ToUpperFirstOnly());

                // Enable Back button
                NavigationPage.SetHasBackButton(this, true);

                // Cancel activerun flag
                activerun = false;
            };
        }
Esempio n. 6
0
        public ResultsCamPage(NeuralNet neuralnet, Vector <double> input, Vector <double> result)
        {
            // Define GUI

            // Page properties
            Title           = "Results";
            BackgroundColor = Color.SteelBlue;
            Padding         = new Thickness(Application.Current.MainPage.Width * 0.05, Application.Current.MainPage.Height * 0.05);


            // Define styles for labels in grid
            var labelgridStyle = new Style(typeof(Label))
            {
                Setters =
                {
                    new Setter {
                        Property = Label.TextColorProperty, Value = Color.GhostWhite
                    },
                    new Setter {
                        Property = Label.BackgroundColorProperty, Value = Color.SteelBlue
                    },
                    new Setter {
                        Property = Label.HorizontalOptionsProperty, Value = LayoutOptions.FillAndExpand
                    },
                    new Setter {
                        Property = Label.HorizontalTextAlignmentProperty, Value = TextAlignment.Center
                    },
                    new Setter {
                        Property = Label.VerticalOptionsProperty, Value = LayoutOptions.FillAndExpand
                    },
                    new Setter {
                        Property = Label.VerticalTextAlignmentProperty, Value = TextAlignment.Center
                    }
                }
            };

            var labelgridhighlightStyle = new Style(typeof(Label))
            {
                BasedOn = labelgridStyle,
                Setters =
                {
                    new Setter {
                        Property = Label.TextColorProperty, Value = Color.GhostWhite
                    },
                    new Setter {
                        Property = Label.BackgroundColorProperty, Value = Color.DarkOrange
                    },
                    new Setter {
                        Property = Label.FontAttributesProperty, Value = FontAttributes.Bold
                    }
                }
            };

            var labelgridheadlineStyle = new Style(typeof(Label))
            {
                BasedOn = labelgridStyle,
                Setters =
                {
                    new Setter {
                        Property = Label.TextColorProperty, Value = Color.SteelBlue
                    },
                    new Setter {
                        Property = Label.BackgroundColorProperty, Value = Color.GhostWhite
                    },
                    new Setter {
                        Property = Label.FontAttributesProperty, Value = FontAttributes.Bold
                    }
                }
            };

            // Define 11x3 grid for representing results
            Grid grid = new Grid();

            grid.BackgroundColor   = Color.GhostWhite;
            grid.ColumnSpacing     = 1;
            grid.RowSpacing        = 1;
            grid.Padding           = new Thickness(1);
            grid.HorizontalOptions = LayoutOptions.FillAndExpand;
            grid.VerticalOptions   = LayoutOptions.FillAndExpand;

            for (int i = 0; i < 3; i++)
            {
                grid.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = new GridLength(1, GridUnitType.Star)
                });
            }
            for (int i = 0; i < 11; i++)
            {
                grid.RowDefinitions.Add(new RowDefinition {
                    Height = new GridLength(1, GridUnitType.Star)
                });
            }

            // Add headlines to grid
            grid.Children.Add(new Label {
                Text = "Digit", Style = labelgridheadlineStyle
            }, 0, 0);
            grid.Children.Add(new Label {
                Text = "Ouput", Style = labelgridheadlineStyle
            }, 1, 0);
            grid.Children.Add(new Label {
                Text = "Share", Style = labelgridheadlineStyle
            }, 2, 0);

            // Add content to grid
            for (int i = 0; i < 10; i++)
            {
                if (result.AbsoluteMaximumIndex() == i)
                {
                    grid.Children.Add(new Label {
                        Text = i.ToString(), Style = labelgridhighlightStyle
                    }, 0, i + 1);
                    grid.Children.Add(new Label {
                        Text = String.Format("{0:N3}", result[i]), Style = labelgridhighlightStyle
                    }, 1, i + 1);
                    grid.Children.Add(new Label {
                        Text = String.Format("{0:P1}", result[i] / result.Sum()), Style = labelgridhighlightStyle
                    }, 2, i + 1);
                }
                else
                {
                    grid.Children.Add(new Label {
                        Text = i.ToString(), Style = labelgridStyle
                    }, 0, i + 1);
                    grid.Children.Add(new Label {
                        Text = String.Format("{0:N3}", result[i]), Style = labelgridStyle
                    }, 1, i + 1);
                    grid.Children.Add(new Label {
                        Text = String.Format("{0:P1}", result[i] / result.Sum()), Style = labelgridStyle
                    }, 2, i + 1);
                }
            }

            // Define 2x3 grid for training net with own handwriting
            Grid grid2 = new Grid();

            grid2.BackgroundColor   = Color.Transparent;
            grid2.ColumnSpacing     = 0;
            grid2.RowSpacing        = 0;
            grid2.HorizontalOptions = LayoutOptions.Center;

            grid2.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(8, GridUnitType.Star)
            });
            grid2.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(2, GridUnitType.Star)
            });
            grid2.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Star)
            });
            grid2.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(2, GridUnitType.Star)
            });
            grid2.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Star)
            });
            grid2.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(2, GridUnitType.Star)
            });

            grid2.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(1, GridUnitType.Star)
            });
            grid2.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(1, GridUnitType.Star)
            });

            // Place content
            grid2.Children.Add(new Label {
                Text = "Net's answer:", Style = labelgridStyle, HorizontalTextAlignment = TextAlignment.Start
            }, 0, 0);
            grid2.Children.Add(new Label {
                Text = "Correct answer:", Style = labelgridStyle, HorizontalTextAlignment = TextAlignment.Start
            }, 0, 1);
            grid2.Children.Add(new Label {
                Text = result.AbsoluteMaximumIndex().ToString(), Style = labelgridStyle
            }, 1, 0);

            // labelresult keeps correct answer in Text property
            Label labelresult = new Label {
                Text = result.AbsoluteMaximumIndex().ToString(), Style = labelgridStyle
            };

            grid2.Children.Add(labelresult, 1, 1);

            // Buttons for changing value of correct answer
            Button buttondown = new Button()
            {
                Text = "-"
            };

            grid2.Children.Add(buttondown, 3, 4, 0, 2);
            buttondown.Clicked += (s, e) =>
            {
                if (Convert.ToInt32(labelresult.Text) > 0)
                {
                    labelresult.Text = (Convert.ToInt32(labelresult.Text) - 1).ToString();
                }
                else
                {
                    labelresult.Text = "9";
                }
            };

            Button buttonup = new Button()
            {
                Text = "+"
            };

            grid2.Children.Add(buttonup, 5, 6, 0, 2);
            buttonup.Clicked += (s, e) =>
            {
                if (Convert.ToInt32(labelresult.Text) < 9)
                {
                    labelresult.Text = (Convert.ToInt32(labelresult.Text) + 1).ToString();
                }
                else
                {
                    labelresult.Text = "0";
                }
            };

            // Define button to train net
            Button button = new Button
            {
                Text            = "Train this Digit",
                WidthRequest    = Application.Current.MainPage.Width * 0.8,
                HeightRequest   = Application.Current.MainPage.Height * 0.10,
                VerticalOptions = LayoutOptions.Center,
            };

            // Train net
            button.Clicked += (s, e) =>
            {
                // Determine output vector from correct answer
                Vector <double> output = Vector <double> .Build.Dense(10, 0.01);

                output[Convert.ToInt32(labelresult.Text)] = 0.99;

                // Train net with this data set
                neuralnet.Train(input, output);

                // Increase number of training data sets used so far
                neuralnet.TrainingDataCounter++;

                DisplayAlert("Training completed", string.Format("Net trained with {0} digit{1} so far.", neuralnet.TrainingDataCounter, (neuralnet.TrainingDataCounter == 1 ? "" : "s")), "OK");
            };

            // Define page
            Content = new ScrollView
            {
                Content = new StackLayout
                {
                    Children = { grid, grid2, button },
                    Spacing  = Application.Current.MainPage.Height * 0.05
                }
            };
        }
Esempio n. 7
0
 public static void Train()
 {
     net.Train(dataSets, 0.05);
 }