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); }
public NeuralNet Crossover(NeuralNet partner) { OutputLayer.Crossover(partner.OutputLayer); foreach (var layer in HiddenLayers.Zip(partner.HiddenLayers)) { layer.Key.Crossover(layer.Value); } return(this); }
static void Main(string[] args) { var TrainingData = MNIST.ReadMNIST(false); //Set Random Seed for better debugging Accord.Math.Random.Generator.Seed = 5; //Input, Hidden, Output NeuralNet NN = new NeuralNet(2, 2, 1); TrainXORTest(NN); }
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); } }
//Crossover public void Crossover(NeuralNet other, Random rng) { for (int i = 0; i < Layers.Count; i++) { for (int j = 0; j < Layers[i].Neurons.Length; j++) { if (rng.Next(2) == 0) { Layers[i].Neurons[j].BiasWeight = other.Layers[i].Neurons[j].BiasWeight; other.Layers[i].Neurons[j].Weights.CopyTo(Layers[i].Neurons[j].Weights, 0); } } } }
//Initialize Network and Dataset void Awake() { int numInputs, numHiddenLayers, numOutputs; if (File.Exists("data.txt")) { net = ReadFromXmlFile <NeuralNet>("data.txt"); Debug.Log("THIS WORKS"); } else { net = new NeuralNet(37, 2, 10); Debug.Log("reached here"); } dataSets = new List <DataSet>(); }
static void Main(string[] args) { NeuralNet net = new NeuralNet(new int[] { 2, 3, 1 }); double[][][] trainData = net.GetTrainData("trainSet.txt"); net.OpenNet("1.xml"); //net.Training(trainData); //net.SaveNet("1.xml"); Console.WriteLine("in: 0 0\nout: {0}\n\nin: 1 1\nout: {1}\n\nin: 0 1\nout: {2}\n\nin: 1 0\nout: {3}\n\n", net.Ask(new double[] { 0, 0 })[0], net.Ask(new double[] { 1, 1 })[0], net.Ask(new double[] { 0, 1 })[0], net.Ask(new double[] { 1, 0 })[0]); Console.ReadKey(); }
static void Main(string[] args) { //First argument will be the path to the images //Second argument will be the path to the labels int width = 28; int height = 28; List <Digit> digits = GetLabeledImages(args[0], args[1], width, height).Shuffle().Cast <Digit>().ToList(); NeuralNet net = NeuralNet.Input(width * height).Layer <ReLU>(20).Layer <LeakyReLU1>(20).Output <Sigmoid>(10); IEnumerable <IDataSet> training = digits.Take(digits.Count() - 100); IEnumerable <IDataSet> test = digits.Skip(digits.Count() - 100); net.TrainByExample(training, epochs: 2, fuzz: 0.2d); Console.WriteLine($"\n{test.Where(sample => net.Evaluate(sample.Values).Select((o, i) => (o, i)).MaxBy(o => o.o).i == sample.Targets.MaxIndex()).Count()}/{test.Count()} correct from test set."); File.WriteAllText(@"C:\NNTrimmed.json", net.ToJson()); }
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); } }
/// <summary> /// Creates the visual representation of what an input is doing to the neural network /// </summary> /// <param name="input"></param> /// <returns></returns> public List <double> NodeUpdate(List <double> input, NeuralNet nNetwork) { List <double> outputs = new List <double>(); int weight = 0; if (input.Count != numInputs) { return(outputs); } //Set Input Nodes for (int i = 0; i < input.Count; i++) { if (input[i] > 0) { inputObj[i].color = Color.green * (float)(input[i]); inputObj[i].transform.localScale = Vector3.one * ((float)input[i]); } else { inputObj[i].color = Color.red * (float)(input[i] * -1); inputObj[i].transform.localScale = Vector3.one * ((float)input[i] * -1); } } for (int i = 0; i < numHiddenLayers + 1; i++) { if (i > 0) { input = outputs; } outputs = new List <double>(); weight = 0; for (int j = 0; j < nNetwork.layers[i].numNeurons; j++) { double netInput = 0; int nNumInputs = nNetwork.layers[i].neurons[j].numInputs; //For each Weight for (int k = 0; k < nNumInputs; k++) { //Sum the weights and inputs netInput += nNetwork.layers[i].neurons[j].weights[k] * input[weight]; //Set Lines float line = (float)(nNetwork.layers[i].neurons[j].weights[k] * input[weight++]); if (line > 0) { weightObj[i][j][k].SetColors(Color.green * line, Color.green * line); weightObj[i][j][k].SetWidth(line * .25f, line * .25f); } else { weightObj[i][j][k].SetColors(Color.red * -line, Color.red * -line); weightObj[i][j][k].SetWidth(-line * .25f, -line * .25f); } } //Add bias netInput += nNetwork.layers[i].neurons[j].weights[nNumInputs] * -1; double sig = NeuralNet.Sigmoid(netInput, 10); //Set Node Color layersObj[i][j].color = (Color.green * (float)sig); layersObj[i][j].transform.localScale = (Vector3.one * (float)sig); outputs.Add(sig); weight = 0; } } return(outputs); }
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; }; }
public void NeuralNetConstructorAssignsCorrectNumberOfWeightsToEachLayer() { Layer input = new Layer(LayerType.Input, null, 0, 3); Layer hidden = new Layer(LayerType.Hidden, new LogisticSigmoidActivationFunction(), 1, 4); Layer output = new Layer(LayerType.Output, new SoftmaxActivationFunction(), 2, 2); input.Nodes[0].Input = 1; input.Nodes[1].Input = 2; input.Nodes[2].Input = 3; input.Nodes[0].ActivatedSum = 1; input.Nodes[1].ActivatedSum = 2; input.Nodes[2].ActivatedSum = 3; List<Layer> layers = new List<Layer>(); layers.Add(input); layers.Add(hidden); layers.Add(output); //24 weights List<double> weights = new List<double>() { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; //6 biases List<double> biases = new List<double>() { 1, 1, 1, 1, 1, 1 }; NeuralNet nn = new NeuralNet(layers, weights, biases); Assert.AreEqual(nn.Layers[0].Nodes.Count * nn.Layers[1].Nodes.Count, nn.Layers[0].Weights.Count); Assert.AreEqual(nn.Layers[1].Nodes.Count * nn.Layers[2].Nodes.Count, nn.Layers[1].Weights.Count); }
public void NeuralNetConstructorOrdersLayersAscendingByLayerOrder() { Layer input = new Layer(LayerType.Input, null, 0, 3); Layer output = new Layer(LayerType.Output, new SoftmaxActivationFunction(), 2, 2); Layer hidden = new Layer(LayerType.Hidden, new LogisticSigmoidActivationFunction(), 1, 4); input.Nodes[0].Input = 1; input.Nodes[1].Input = 2; input.Nodes[2].Input = 3; input.Nodes[0].ActivatedSum = 1; input.Nodes[1].ActivatedSum = 2; input.Nodes[2].ActivatedSum = 3; List<Layer> layers = new List<Layer>(); layers.Add(input); layers.Add(output); layers.Add(hidden); //24 weights List<double> weights = new List<double>() { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; //6 biases List<double> biases = new List<double>() { 1, 1, 1, 1, 1, 1 }; NeuralNet nn = new NeuralNet(layers, weights, biases); //nn.Run(); Assert.AreEqual(0, nn.Layers[0].LayerOrder); Assert.AreEqual(1, nn.Layers[1].LayerOrder); Assert.AreEqual(2, nn.Layers[2].LayerOrder); }
public ResultsMNISTPage(NeuralNet neuralnet, MNISTDataManager mnistdata, Vector <double> digittotal, Vector <double> digitcorrect) { // 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 < 4; i++) { grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); } for (int i = 0; i < 12; 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 = "Total", Style = labelgridheadlineStyle }, 1, 0); grid.Children.Add(new Label { Text = "Correct", Style = labelgridheadlineStyle }, 2, 0); grid.Children.Add(new Label { Text = "in %", Style = labelgridheadlineStyle }, 3, 0); // Add results for each digit to grid for (int i = 0; i < 10; i++) { grid.Children.Add(new Label { Text = i.ToString(), Style = labelgridStyle }, 0, i + 1); grid.Children.Add(new Label { Text = String.Format("{0:N0}", digittotal[i]), Style = labelgridStyle }, 1, i + 1); grid.Children.Add(new Label { Text = String.Format("{0:N0}", digitcorrect[i]), Style = labelgridStyle }, 2, i + 1); grid.Children.Add(new Label { Text = String.Format("{0:P0}", digittotal[i] > 0 ? digitcorrect[i] / digittotal[i] : 0), Style = labelgridStyle }, 3, i + 1); } // Add sum to grid grid.Children.Add(new Label { Text = "Sum", Style = labelgridhighlightStyle }, 0, 11); grid.Children.Add(new Label { Text = String.Format("{0:N0}", digittotal.Sum()), Style = labelgridhighlightStyle }, 1, 11); grid.Children.Add(new Label { Text = String.Format("{0:N0}", digitcorrect.Sum()), Style = labelgridhighlightStyle }, 2, 11); grid.Children.Add(new Label { Text = String.Format("{0:P0}", digitcorrect.Sum() / digittotal.Sum()), Style = labelgridhighlightStyle }, 3, 11); // Define button for details Button button = new Button { Text = "Details", WidthRequest = Application.Current.MainPage.Width * 0.8, HeightRequest = Application.Current.MainPage.Height * 0.10, VerticalOptions = LayoutOptions.Center, }; // Show page with detailed results for each digit button.Clicked += (s, e) => { if (messageflag == false) { DisplayAlert("Information", "Look at the details by swiping left and right.", "OK"); messageflag = true; } Navigation.PushAsync(new ResultsDetailsPage(neuralnet, mnistdata)); }; // Define page Content = new ScrollView { Content = new StackLayout { Children = { grid, button }, Spacing = Application.Current.MainPage.Height * 0.05 } }; }
/// <summary> /// Copies the contents of the source neural network into this neural network. /// </summary> /// <param name="src">the source net, to be copied</param> /// public void CopyNeuralNet(NeuralNet src) { mLayers.Clear(); mActivations.Clear(); mUnitInputs.Clear(); mActiveUnits.Clear(); mActiveSlope.Clear(); mActiveAmplify.Clear(); mNumInputs = src.mNumInputs; mNumOutputs = src.mNumOutputs; mNumLayers = src.mNumLayers; mOutUnitType = src.mOutUnitType; mOutUnitSlope = src.mOutUnitSlope; mOutUnitAmplify = src.mOutUnitAmplify; // the weighted connections linking the network layers for (int i = 0; i < src.mLayers.Count; i++) { NNetWeightedConnect wCnct = new NNetWeightedConnect(src.mLayers[i]); mLayers.Add(wCnct); } // the activation values for each of the network layers int rowLen = src.mActivations.Count; for (int row = 0; row < rowLen; row++) { List <double> vec = new List <double>(); int colLen = src.mActivations[row].Count; for (int col = 0; col < colLen; col++) { vec.Add(src.mActivations[row][col]); } mActivations.Add(vec); } // the input values for the layer activation functions rowLen = src.mUnitInputs.Count; for (int row = 0; row < rowLen; row++) { List <double> vec = new List <double>(); int colLen = src.mUnitInputs[row].Count; for (int col = 0; col < colLen; col++) { vec.Add(src.mUnitInputs[row][col]); } mUnitInputs.Add(vec); } // the hidden layer unit activation function types for (int i = 0; i < src.mActiveUnits.Count; i++) { mActiveUnits.Add(src.mActiveUnits[i]); } // the hidden layer unit activation function slope values for (int i = 0; i < src.mActiveSlope.Count; i++) { mActiveSlope.Add(src.mActiveSlope[i]); } // the hidden layer unit activation function amplify values for (int i = 0; i < src.mActiveAmplify.Count; i++) { mActiveAmplify.Add(src.mActiveAmplify[i]); } }
public Neuron() { InputSynapses = new List <Synapse>(); //create an object from the input list OutputSynapses = new List <Synapse>(); //create an object from the output list Bias = NeuralNet.GetRandom(); //This value is randomized for every neuorn }
public static Tensor4[] GetRandBatch(Tensor3[] x, int[] y, int batchSize, NeuralNet net) { Tensor4 batchX = new Tensor4(x[0].width, x[0].height, x[0].deep, batchSize); Tensor4 batchY = new Tensor4(net.output.width, net.output.height, net.output.deep, batchSize); int[] indexes = new int[batchSize]; int index = 0; while (true) { indexes[index] = (int)(rand.NextDouble() * x.Length); for (int i = 0; i < index; i++) { if (indexes[i] == indexes[index]) { break; } if (i == index - 1) { index++; break; } } if (index == 0) { index++; } if (index == indexes.Length) { break; } } for (int i = 0; i < batchSize; i++) { for (int zInd = 0; zInd < x[indexes[i]].deep; zInd++) { for (int yInd = 0; yInd < x[indexes[i]].height; yInd++) { for (int xInd = 0; xInd < x[indexes[i]].width; xInd++) { batchX[i, zInd, yInd, xInd] = x[indexes[i]][zInd, yInd, xInd]; } } } Tensor3 Y = new Tensor3(net.output.width, net.output.height, net.output.deep); if (net.output.width != 1) { Y[0, 0, y[indexes[i]]] = 1.0; } else { Y[y[indexes[i]], 0, 0] = 1.0; } for (int zInd = 0; zInd < Y.deep; zInd++) { for (int yInd = 0; yInd < Y.height; yInd++) { for (int xInd = 0; xInd < Y.width; xInd++) { batchY[i, zInd, yInd, xInd] = Y[zInd, yInd, xInd]; } } } } return(new Tensor4[] { batchX, batchY }); }
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 } }; }
public CameraTestPage(NeuralNet neuralnet) { // Save neural net this.neuralnet = neuralnet; // Define GUI // Page properties Title = "Handwriting"; BackgroundColor = Color.SteelBlue; Padding = new Thickness(Application.Current.MainPage.Width * 0.05, Application.Current.MainPage.Height * 0.05); // Define label Label description = new Label { Text = "Take a picture of a single digit (0..9) with your phone camera or draw it directly with your finger on the screen" }; // Define button to take pictures of digits Button buttoncam = new Button { Text = "Take a Picture", WidthRequest = Application.Current.MainPage.Width * 0.8, HeightRequest = Application.Current.MainPage.Height * 0.10, VerticalOptions = LayoutOptions.Center }; buttoncam.Clicked += TakeAndShowPicture; // Define button to draw a digit Button buttondraw = new Button { Text = "Draw with Finger", WidthRequest = Application.Current.MainPage.Width * 0.8, HeightRequest = Application.Current.MainPage.Height * 0.10, VerticalOptions = LayoutOptions.Center }; buttondraw.Clicked += async(s, e) => { // Open page to draw digit // Note: DrawDigitPage writes result to "bitmap" await Navigation.PushAsync(new DrawDigitPage()); }; // Define button to ask net to guess the digit Button buttonasknet = new Button { Text = "Ask Neural Net", WidthRequest = Application.Current.MainPage.Width * 0.8, HeightRequest = Application.Current.MainPage.Height * 0.10, VerticalOptions = LayoutOptions.Center, }; buttonasknet.Clicked += AskNeuralNet; // Define canvas for showing the picture of the digit canvasview = new SKCanvasView { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand }; canvasview.PaintSurface += OnCanvasViewPaintSurface; Content = new ScrollView { Content = new StackLayout { Children = { description, buttoncam, buttondraw, canvasview, buttonasknet }, Spacing = Application.Current.MainPage.Height * 0.05 } }; // Convert bitmap and update canvas when page appears after drawing page Appearing += (s, e) => { if (bitmap == null) { return; } // Check if bitmap is not squared and not 28x28 px if ((bitmap.Width != bitmap.Height) || bitmap.Width != 28) { // Convert bitmap to 28x28 pixel grayscale bitmap = ConvertBitmap(bitmap); // Update canvas canvasview.InvalidateSurface(); } }; }
public Synapse(Neuron inputNeuron, Neuron outputNeuron) { InputNeuron = inputNeuron; OutputNeuron = outputNeuron; Weight = NeuralNet.GetRandom(); }
public Neuron() { InputSynapses = new List <Synapse>(); OutputSynapses = new List <Synapse>(); Bias = NeuralNet.GetRandom(); }
public ResultsDetailsPage(NeuralNet neuralnet, MNISTDataManager mnistdata) { // Save parameters this.neuralnet = neuralnet; this.mnistdata = mnistdata; // Define GUI // Page properties Title = "Details"; BackgroundColor = Color.SteelBlue; Padding = new Thickness(Application.Current.MainPage.Width * 0.05, Application.Current.MainPage.Height * 0.05); // Step 1: Generate initial collection of pictures from MNIST database (buffersize elements) pictures = new ObservableCollection <CarouselItem>(); AddPictures(0); // Step 2: Generate data template (just a Image with data binding to CarouselItem class) DataTemplate template = new DataTemplate(() => { Image image = new Image(); image.SetBinding(Image.SourceProperty, "Picture"); return(image); }); // Step 3: Generate carousel view var myCarousel = new CarouselViewControl(); myCarousel.Position = 0; //default myCarousel.ItemsSource = pictures; myCarousel.ItemTemplate = template; myCarousel.InterPageSpacing = 2; myCarousel.Orientation = CarouselViewOrientation.Horizontal; myCarousel.ShowIndicators = false; myCarousel.HorizontalOptions = LayoutOptions.FillAndExpand; myCarousel.VerticalOptions = LayoutOptions.FillAndExpand; // Define labels labelheadline = new Label(); labeldigitcorrect = new Label(); labeldigitanswernet = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, HeightRequest = Application.Current.MainPage.Height * 0.1, VerticalTextAlignment = TextAlignment.Center }; SetLabels(0); // Step 4: Build page Content = new ScrollView { Content = new StackLayout { Children = { labelheadline, labeldigitcorrect, myCarousel, labeldigitanswernet }, Spacing = Application.Current.MainPage.Height * 0.05 } }; // Update page when image is swiped myCarousel.PositionSelected += (s, e) => { // Add new pictures if user has reached end of carousel // KNOWN ERROR: When end is reached the wrong picture is displayed (first picture of uploaded data) // Warning: stepping through all MINST data may yield to memory problems if (myCarousel.Position == pictures.Count - 1) { AddPictures(myCarousel.Position + 1); } // Update label SetLabels(myCarousel.Position); }; }
/// <summary> /// Copy constructor. /// </summary> /// <param name="original">the NeuralNet object to be copied</param> /// public NeuralNet(NeuralNet original) { CopyNeuralNet(original); }