public static NeuralNetwork ChooseOne(Generation generation, NeuralNetwork other_network = null) { float r = RandomF.NextFloat(); NeuralNetwork network = null; if (other_network != null) { r = RandomF.NextFloat(0f, 1f - other_network.fitness); } int index = 0; for (int i = 0; i < generation.Population; i++) { if (other_network != null) { if (i == other_network.id) { continue; } } r -= generation[i].fitness; if (r < 0) { index = i; break; } } network = generation[index]; return(network); }
public static NeuralNetwork Diseased(NeuralNetwork neuralNetwork, float rate) { for (int i = 0; i < neuralNetwork.Length; i++) { for (int j = 0; j < neuralNetwork[i].Length; j++) { for (int k = 0; k < neuralNetwork[i][j].Length; k++) { if (RandomF.NextFloat() < rate) { float value = neuralNetwork[i][j][k].x; var offset = RandomF.NextGaussian() * 0.5f; value += offset; neuralNetwork[i][j][k].x = value; } } } if (!neuralNetwork[i].IsBiasNull()) { for (int k = 0; k < neuralNetwork[i].GetBias().Length; k++) { if (RandomF.NextFloat() < rate) { float value = neuralNetwork[i].GetBias()[k].x; var offset = RandomF.NextGaussian() * 0.5f; value += offset; neuralNetwork[i].GetBias()[k].x = value; } } } } return(new NeuralNetwork(neuralNetwork)); }
private void PictureBoxPoster_MouseUp(object sender, MouseEventArgs e) { toggleMove = false; PictureBox pictureBox = (PictureBox)sender; bool train = false; float value = 0f; if (pictureBox.Location.X >= (this.Width - (this.Width * padding))) { value = 1f; train = true; existRight = true; } else if (pictureBox.Location.X <= (this.Width * padding - size.Width)) { value = 0f; train = true; existLeft = true; } else if (trainingData.Length > 0 && (existLeft && existRight)) { pictureBox.Location = new Point(lastPoint.X, pictureBox.Location.Y); pictureBox.BringToFront(); } if (train) { string[] split = linesList[int.Parse(pictureBox.Tag.ToString())].Split(' '); int day = int.Parse(split[1]) - 1; int month = months.IndexOf(split[2]); float year = Map.Float(0f, float.Parse(split[3]), 9999f, 0f, 1f); List <float> inputs = new List <float>(); for (int i = 0; i < 31; i++) { if (i == day) { inputs.Add(1f); } else { inputs.Add(0f); } } for (int i = 0; i < 12; i++) { if (i == month) { inputs.Add(1f); } else { inputs.Add(0f); } } inputs.Add(year); string[] categoriesArr = split[4].Split(','); for (int i = 0; i < categories.Count; i++) { bool exist = false; for (int j = 0; j < categoriesArr.Length; j++) { if (categories[i] == categoriesArr[j]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } string[] directorsArr = split[5].Split(','); for (int i = 0; i < directors.Count; i++) { bool exist = false; for (int j = 0; j < directorsArr.Length; j++) { if (directors[i] == directorsArr[j]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } string[] starsArr = split[6].Split(','); for (int i = 0; i < stars.Count; i++) { bool exist = false; for (int j = 0; j < starsArr.Length; j++) { if (stars[i] == starsArr[j]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } trainingData.Add(inputs.ToArray(), new[] { value }); stopwatch.Start(); neuralNetwork.Training(trainingData); stopwatch.Stop(); Console.WriteLine("Training : " + stopwatch.ElapsedMilliseconds); stopwatch.Reset(); pictureBox.MouseDown -= PictureBoxPoster_MouseDown; pictureBox.MouseUp -= PictureBoxPoster_MouseUp; pictureBox.MouseMove -= PictureBoxPoster_MouseMove; pictureBoxes.Remove(pictureBox); if (existLeft && existRight) { for (int i = 0; i < pictureBoxes.Count; i++) { split = linesList[int.Parse(pictureBoxes[i].Tag.ToString())].Split(' '); day = int.Parse(split[1]) - 1; month = months.IndexOf(split[2]); year = Map.Float(1f, float.Parse(split[3]), 9999f, 0f, 1f); inputs = new List <float>(); for (int j = 0; j < 31; j++) { if (j == day) { inputs.Add(1f); } else { inputs.Add(0f); } } for (int j = 0; j < 12; j++) { if (j == month) { inputs.Add(1f); } else { inputs.Add(0f); } } inputs.Add(year); categoriesArr = split[4].Split(','); for (int j = 0; j < categories.Count; j++) { bool exist = false; for (int z = 0; z < categoriesArr.Length; z++) { if (categories[j] == categoriesArr[z]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } directorsArr = split[5].Split(','); for (int j = 0; j < directors.Count; j++) { bool exist = false; for (int z = 0; z < directorsArr.Length; z++) { if (directors[j] == directorsArr[z]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } starsArr = split[6].Split(','); for (int j = 0; j < stars.Count; j++) { bool exist = false; for (int z = 0; z < starsArr.Length; z++) { if (stars[j] == starsArr[z]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } stopwatch.Start(); Result[] guesses = neuralNetwork.Predict(inputs.ToArray()); stopwatch.Stop(); Console.WriteLine("Predict:" + stopwatch.ElapsedMilliseconds); stopwatch.Reset(); pictureBoxes[i].Location = new Point((int)Map.Float(0f, guesses[0].value, 1f, this.Width * padding, this.Width - (this.Width * padding) - size.Width), (int)RandomF.NextFloat(this.Height - size.Height)); } } } }
private void formUI_Load(object sender, EventArgs e) { ToolTip toolTip = new ToolTip(); months.Add("January"); months.Add("February"); months.Add("March"); months.Add("April"); months.Add("May"); months.Add("June"); months.Add("July"); months.Add("August"); months.Add("September"); months.Add("October"); months.Add("November"); months.Add("December"); string path = @"images\"; string[] lines = File.ReadAllLines(path + "data.txt"); linesList.AddRange(lines); for (int i = 0; i < lines.Length; i++) { string[] split = lines[i].Split(' '); string name = split[0]; PictureBox pictureBoxPoster = new PictureBox() { Name = Path.GetFileName(name), Location = new Point((int)Map.Float(0f, RandomF.NextFloat(this.Width), this.Width, this.Width * padding, this.Width - (this.Width * padding) - size.Width), RandomF.Next(this.Height - size.Height)), Image = Image.FromFile(path + name), Size = size, SizeMode = PictureBoxSizeMode.StretchImage, Tag = i.ToString(), BorderStyle = BorderStyle.FixedSingle }; pictureBoxPoster.MouseDown += PictureBoxPoster_MouseDown; pictureBoxPoster.MouseUp += PictureBoxPoster_MouseUp; pictureBoxPoster.MouseMove += PictureBoxPoster_MouseMove; toolTip.SetToolTip(pictureBoxPoster, split[0]); pictureBoxes.Add(pictureBoxPoster); this.Controls.Add(pictureBoxPoster); // Categories string[] categoriesArr = split[4].Split(','); for (int j = 0; j < categoriesArr.Length; j++) { if (!categories.Contains(categoriesArr[j])) { categories.Add(categoriesArr[j]); } } // Directors string[] directorsArr = split[5].Split(','); for (int j = 0; j < directorsArr.Length; j++) { if (!directors.Contains(directorsArr[j])) { directors.Add(directorsArr[j]); } } // Stars string[] starsArr = split[6].Split(','); for (int j = 0; j < starsArr.Length; j++) { if (!stars.Contains(starsArr[j])) { stars.Add(starsArr[j]); } } } neuralNetwork = new NeuralNetwork(31 + 12 + 1 + categories.Count + directors.Count + stars.Count, 64, 1) { max_circle = 10000, learning_rate = 0.5f, momentum_rate = 0.96f }; }
public Synapse() { this.x = RandomF.NextFloat(-1f, 1f); this.error = 0f; }