//handle network simulation public void SimulateNetwork() { //forward prop network float[] inputs = new float[] { RaycastForCarScript.sensor1Distance, RaycastForCarScript.sensor2Distance, RaycastForCarScript.sensor3Distance }; float[] outputs = brain.ForwardProp(inputs); //decide reward float reward = brain.DecideReward(inputs, outputs); //backprop data if (reward > 0) { while (reward > 0) { brain.BackProp(new float[] { Random.Range(-30F, 30F), Random.Range(-30F, 30F), Random.Range(-30F, 30F) }, outputs); reward--; } } else if (reward < 0) { while (reward < 0) { brain.BackProp(inputs, new float[] { Random.Range(-15F, 15F), Random.Range(-15F, 15F) }); reward++; } } //add output to memory bank brain.MemoryBank.AddMemory(inputs, outputs); //move car float engine = outputs[0]; float turn = outputs[1]; if (engine > MaxSpeed) { engine = MaxSpeed; } if (engine < -MaxSpeed) { engine = -MaxSpeed; } if (turn > MaxTurn) { turn = MaxTurn; } if (turn < -MaxTurn) { turn = -MaxTurn; } MoveCar(engine, turn); }
private void Learning(RichTextBox rtb, ProgressBar pb, Button btn_start) { this.Dispatcher.Invoke(() => rtb.AppendText(String.Format("Training...(0/{0})\n\n", this.reps))); for (int j = 0; j < this.reps; j++) { for (int i = 0; i < inputs.Count; i++) { try { lock (nn) { nn.FeedForward(inputs[i]); nn.BackProp(outputs[i]); } } catch (Exception) { this.Dispatcher.Invoke(() => rtb.AppendText("Failed")); } } this.Dispatcher.Invoke(() => rtb.AppendText(String.Format("Training...({0}/{1})\n", j + 1, this.reps))); this.Dispatcher.Invoke(() => rtb_log.ScrollToEnd()); this.Dispatcher.Invoke(() => pb.Value = j + 1); } this.Dispatcher.Invoke(() => btn_start.IsEnabled = true); this.Dispatcher.Invoke(() => pb.Value = 0); this.Dispatcher.Invoke(() => rtb_log.AppendText("Completed!\n")); t1 = new Thread(() => Learning(this.rtb_log, this.pb_training, this.btn_start)); }
public void Calcular() { float[][] entrada; entrada = new float[TamañoBatch][]; float[][] esperado; esperado = new float[TamañoBatch][]; for (int i = 0; i < entrada.Length; i++) { entrada[i] = new float[28 * 28]; esperado[i] = new float[10]; } for (int i = 0; i < 60000 / TamañoBatch; i++) // 60 000 -> 59 900 { for (int j = 0; j < TamañoBatch; j++) // pasa la informacion de entrada { entrada[j] = mnist.valoresDeEntrenamoento[(i * TamañoBatch) + j]; float[] lab = new float[10]; lab[(int)mnist.lablesEntrenamiento[(i * TamañoBatch) + j]] = 1; esperado[j] = lab; net.FeedForward(entrada[j]); net.BackProp(esperado[j]); net.UpdateNetwork(); } //net.UpdateNetwork(); } Probar(); }
// Use this for initialization void Start() { // 0 0 0 => 0 // 0 0 1 => 1 // 0 1 0 => 1 // 0 1 1 => 0 // 1 0 0 => 1 // 1 0 1 => 0 // 1 1 0 => 0 // 1 1 1 => 1 NeuralNetwork net = new NeuralNetwork(new int[] { 3, 25, 25, 1 }); //intiilize network //Itterate 5000 times and train each possible output //5000*8 = 40000 traning operations for (int i = 0; i < 5000; i++) { net.FeedForward(new float[] { 0, 0, 0 }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { 0, 0, 1 }); net.BackProp(new float[] { 1 }); net.FeedForward(new float[] { 0, 1, 0 }); net.BackProp(new float[] { 1 }); net.FeedForward(new float[] { 0, 1, 1 }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { 1, 0, 0 }); net.BackProp(new float[] { 1 }); net.FeedForward(new float[] { 1, 0, 1 }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { 1, 1, 0 }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { 1, 1, 1 }); net.BackProp(new float[] { 1 }); } //output to see if the network has learnt //WHICH IT HAS!!!!! UnityEngine.Debug.Log(net.FeedForward(new float[] { 0, 0, 0 })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { 0, 0, 1 })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { 0, 1, 0 })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { 0, 1, 1 })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { 1, 0, 0 })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { 1, 0, 1 })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { 1, 1, 0 })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { 1, 1, 1 })[0]); }
static void Main(string[] args) { Console.WriteLine("Welcome!\nour Neural Network is now to Study the 3-Input XOR Pattern!"); Console.WriteLine("Welcome!\nwe first start by training the Neural Network for you!"); NeuralNetwork nn = new NeuralNetwork(new int[] { 3, 25, 25, 1 }); //Neural Network Training! for (int i = 0; i < epochs; i++) { nn.FeedForward(new float[] { 0, 0, 0 }); //INPUT LAYER of 3 Neurons nn.BackProp(new float[] { 0 }); //HAS THIS Expected Output Layer of 1 Neuron nn.FeedForward(new float[] { 0, 0, 1 }); nn.BackProp(new float[] { 1 }); nn.FeedForward(new float[] { 0, 1, 0 }); nn.BackProp(new float[] { 1 }); nn.FeedForward(new float[] { 0, 1, 1 }); nn.BackProp(new float[] { 0 }); nn.FeedForward(new float[] { 1, 0, 0 }); nn.BackProp(new float[] { 1 }); nn.FeedForward(new float[] { 1, 0, 1 }); nn.BackProp(new float[] { 0 }); nn.FeedForward(new float[] { 1, 1, 0 }); nn.BackProp(new float[] { 0 }); nn.FeedForward(new float[] { 1, 1, 1 }); nn.BackProp(new float[] { 1 }); } //Finished Training Console.WriteLine("Finished Training!!!"); Console.WriteLine("Testing Area:\n"); var o = nn.FeedForward(new float[] { 0, 0, 0 }); Console.WriteLine("the Output for Sequence 0 0 0 is: " + o[0]); o = nn.FeedForward(new float[] { 0, 0, 1 }); Console.WriteLine("the Output for Sequence 0 0 1 is: " + o[0]); o = nn.FeedForward(new float[] { 1, 0, 1 }); Console.WriteLine("the Output for Sequence 1 0 1 is: " + o[0]); o = nn.FeedForward(new float[] { 1, 1, 1 }); Console.WriteLine("the Output for Sequence 1 1 1 is: " + o[0]); Console.WriteLine("Done! Press Any Key to Close..."); Console.ReadKey(); }
void Start() { // 0 0 0 => 0 // 0 0 1 => 1 // 0 1 0 => 1 // 0 1 1 => 0 // 1 0 0 => 1 // 1 0 1 => 0 // 1 1 0 => 0 // 1 1 1 => 1 NeuralNetwork net = new NeuralNetwork(new int[] { 3, 25, 25, 1 }); float n = 10f; for (int i = 0; i < 5000; i++) { net.FeedForward(new float[] { 0, 0, 0 }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { 0, 0, n }); net.BackProp(new float[] { 1 }); net.FeedForward(new float[] { 0, n, 0 }); net.BackProp(new float[] { 1 }); net.FeedForward(new float[] { 0, n, n }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { n, 0, 0 }); net.BackProp(new float[] { 1 }); net.FeedForward(new float[] { n, 0, n }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { n, n, 0 }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { n, n, n }); net.BackProp(new float[] { 1 }); } UnityEngine.Debug.Log(net.FeedForward(new float[] { 0, 0, 0 })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { 0, 0, n })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { 0, n, 0 })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { 0, n, n })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { n, 0, 0 })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { n, 0, n })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { n, n, 0 })[0]); UnityEngine.Debug.Log(net.FeedForward(new float[] { n, n, n })[0]); }
public static void Learning(NeuralNetwork net, string fileName) { try { LoadInputOutput(fileName); } catch { throw new Exception("Nem lehetett beolvasni a fájlból"); } for (int i = 0; i < inputs.Count; i++) { net.FeedForward(inputs[i]); net.BackProp(outputs[i]); } }
private void OnCollisionEnter(Collision collision) { corrector = Vector3.Reflect(corrector, collision.contacts[0].normal); network.BackProp(rb.velocity, corrector); transform.position = pos; }
public void TrainNetwork() //When user presses train button, this function will be run and network will be trained and saved in file. { // Read in training values: string path = "Assets/Resources/TrainingValues.txt"; // TestValues: float[][] input = new float[256][]; float[][] output = new float[256][]; //Read the text from directly from the test.txt file StreamReader reader = new StreamReader(path); string line; int inputNr = 0; int outputNr = 0; int lineNr = 0; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("#")) // Don't read comments in the txt file: { continue; } if (lineNr % 2 == 0) { string[] bits = line.Split(' '); float[] array = new float[bits.Length]; for (int i = 0; i < bits.Length; i++) { array[i] = float.Parse(bits[i]); } input[inputNr] = array; inputNr++; } else { string[] bits = line.Split(' '); float[] array = new float[bits.Length]; for (int i = 0; i < bits.Length; i++) { array[i] = float.Parse(bits[i]); } output[outputNr] = array; outputNr++; } lineNr++; } NeuralNetwork net = new NeuralNetwork(new int[] { 10, 25, 25, 4 }); // initialize network for (int i = 0; i < trainGenerations; i++) { for (int j = 0; j < (lineNr / 2); j++) { net.FeedForward(input[j]); net.BackProp(output[j]); } } // Used to see the accuracy of the network: float accuracy = 0; for (int i = 0; i < trainGenerations; i++) { for (int j = 0; j < (lineNr / 2); j++) { float[] outputTest = net.FeedForward(input[j]); for (int k = 0; k < 4; k++) { if (output[j][k] == 1) // Supposed to be 1. { float newValue = outputTest[k] * 2 - 1; // Making value from 0.5-1 to 0-1. accuracy = (accuracy + newValue) / 2; } else // Supposed to be 0. { float newValue = 1 - (outputTest[k] * 2); // Making value from 0.5-0 to 0-1. accuracy = (accuracy + newValue) / 2; } } } } Debug.Log("Accuracy: " + accuracy); // Save the training values: net.SaveBrain(); net.LoadBrain(); }
static void Main(string[] args) { // 0 0 0 => 0 // 0 0 1 => 1 // 0 1 0 => 1 // 0 1 1 => 0 // 1 0 0 => 1 // 1 0 1 => 0 // 1 1 0 => 0 // 1 1 1 => 1 NeuralNetwork net = new NeuralNetwork(new int[] { 3, 25, 25, 1 }); //Itterate 5000 times and train each possible output //5000*8 = 40000 traning operations for (int i = 0; i < 5000; i++) { net.FeedForward(new float[] { 0, 0, 0 }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { 0, 0, 1 }); net.BackProp(new float[] { 1 }); net.FeedForward(new float[] { 0, 1, 0 }); net.BackProp(new float[] { 1 }); net.FeedForward(new float[] { 0, 1, 1 }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { 1, 0, 0 }); net.BackProp(new float[] { 1 }); net.FeedForward(new float[] { 1, 0, 1 }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { 1, 1, 0 }); net.BackProp(new float[] { 0 }); net.FeedForward(new float[] { 1, 1, 1 }); net.BackProp(new float[] { 1 }); } Console.WriteLine(net.FeedForward(new float[] { 0, 0, 0 })[0]); Console.WriteLine(net.FeedForward(new float[] { 0, 0, 1 })[0]); Console.WriteLine(net.FeedForward(new float[] { 0, 1, 0 })[0]); Console.WriteLine(net.FeedForward(new float[] { 0, 1, 1 })[0]); Console.WriteLine(net.FeedForward(new float[] { 1, 0, 0 })[0]); Console.WriteLine(net.FeedForward(new float[] { 1, 0, 1 })[0]); Console.WriteLine(net.FeedForward(new float[] { 1, 1, 0 })[0]); Console.WriteLine(net.FeedForward(new float[] { 1, 1, 1 })[0]); //Console.WriteLine(Math.Round(net.FeedForward(new float[] { 0, 0, 0 })[0])); //Console.WriteLine(Math.Round(net.FeedForward(new float[] { 0, 0, 1 })[0])); //Console.WriteLine(Math.Round(net.FeedForward(new float[] { 0, 1, 0 })[0])); //Console.WriteLine(Math.Round(net.FeedForward(new float[] { 0, 1, 1 })[0])); //Console.WriteLine(Math.Round(net.FeedForward(new float[] { 1, 0, 0 })[0])); //Console.WriteLine(Math.Round(net.FeedForward(new float[] { 1, 0, 1 })[0])); //Console.WriteLine(Math.Round(net.FeedForward(new float[] { 1, 1, 0 })[0])); //Console.WriteLine(Math.Round(net.FeedForward(new float[] { 1, 1, 1 })[0])); Console.WriteLine("\nPress Enter to quit."); Console.ReadLine(); }