public TicTacToeGenetic(FeedforwardNetwork network, bool reset, int populationSize, double mutationPercent, double percentToMate, Type opponent) { this.setOpponent(opponent); this.MutationPercent = mutationPercent; this.MatingPopulation = percentToMate * 2; this.PopulationSize = populationSize; this.PercentToMate = percentToMate; this.Chromosomes = new TicTacToeChromosome[this.PopulationSize]; for (int i = 0; i < this.Chromosomes.Length; i++) { FeedforwardNetwork chromosomeNetwork = (FeedforwardNetwork)network .Clone(); if (reset) { chromosomeNetwork.Reset(); } TicTacToeChromosome c = new TicTacToeChromosome(this, chromosomeNetwork); c.UpdateGenes(); SetChromosome(i, c); } SortChromosomes(); }
public static double[] NetworkToArray(FeedforwardNetwork network) { int size = 0; foreach (FeedforwardLayer layer in network.Layers) { if (layer.HasMatrix()) { size += layer.MatrixSize; } } double[] result = new double[size]; int index = 0; foreach (FeedforwardLayer layer in network.Layers) { if (layer.NextLayer != null) { double[] matrix = layer.LayerMatrix.ToPackedArray(); for (int i = 0; i < matrix.Length; i++) { result[index++] = matrix[i]; } } } return(result); }
public CardRecognizer() { //Initialize common filter sequence , this sequence generally will be applied commonSeq = new FiltersSequence(); commonSeq.Add(Grayscale.CommonAlgorithms.BT709); commonSeq.Add(new OtsuThreshold()); commonSeq.Add(new DifferenceEdgeDetector()); Stream strm; BinaryFormatter bformat; AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; strm = File.Open("NetworkLetra.net", FileMode.Open); bformat = new BinaryFormatter(); NetworkLetra = (FeedforwardNetwork)bformat.Deserialize(strm); strm = File.Open("NetworkSuits.net", FileMode.Open); bformat = new BinaryFormatter(); NetworkSuits = (FeedforwardNetwork)bformat.Deserialize(strm); strm = File.Open("NetworkNumero.net", FileMode.Open); bformat = new BinaryFormatter(); NetworkNumero = (FeedforwardNetwork)bformat.Deserialize(strm); AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve; }
public YearBornBot() { this.network = (FeedforwardNetwork)SerializeObject .Load(Config.FILENAME_WHENBORN_NET); this.histogram = (WordHistogram)SerializeObject .Load(Config.FILENAME_HISTOGRAM); }
/// <summary> /// Convert to an array. This is used with some training algorithms that /// require that the "memory" of the neuron(the weight and threshold values) /// be expressed as a linear array. /// </summary> /// <param name="network">A neural network.</param> /// <returns>The memory of the neural network as an array.</returns> public static double[] NetworkToArray(FeedforwardNetwork network) { int size = 0; // first determine size foreach (FeedforwardLayer layer in network.Layers) { // count the size of the weight matrix if (layer.HasMatrix()) { size += layer.MatrixSize; } } // allocate an array to hold Double[] result = new Double[size]; // copy data to array int index = 0; foreach (FeedforwardLayer layer in network.Layers) { // now the weight matrix(if it exists) if (layer.Next != null) { Double[] matrix = layer.LayerMatrix.ToPackedArray(); for (int i = 0; i < matrix.Length; i++) { result[index++] = matrix[i]; } } } return(result); }
/// <summary> /// Construct a genetic algorithm for a neural network that uses training sets. /// </summary> /// <param name="network">The neural network.</param> /// <param name="reset">Should each neural network be reset to random values.</param> /// <param name="input">The input training set.</param> /// <param name="ideal">The ideal values for the input training set.</param> /// <param name="populationSize">The initial population size.</param> /// <param name="mutationPercent">The mutation percentage.</param> /// <param name="percentToMate">The percentage of the population allowed to mate.</param> public TrainingSetNeuralGeneticAlgorithm(FeedforwardNetwork network, bool reset, double[][] input, double[][] ideal, int populationSize, double mutationPercent, double percentToMate) { this.MutationPercent = mutationPercent; this.MatingPopulation = percentToMate * 2; this.PopulationSize = populationSize; this.PercentToMate = percentToMate; this.input = input; this.ideal = ideal; this.Chromosomes = new TrainingSetNeuralChromosome[this.PopulationSize]; for (int i = 0; i < this.Chromosomes.Length; i++) { FeedforwardNetwork chromosomeNetwork = (FeedforwardNetwork)network .Clone(); if (reset) { chromosomeNetwork.Reset(); } TrainingSetNeuralChromosome c = new TrainingSetNeuralChromosome( this, chromosomeNetwork); c.UpdateGenes(); SetChromosome(i, c); } SortChromosomes(); }
/// <summary> /// Create, train and use a neural network for XOR. /// </summary> /// <param name="args">Not used</param> static void Main(string[] args) { FeedforwardNetwork network = new FeedforwardNetwork(); network.AddLayer(new FeedforwardLayer(2)); network.AddLayer(new FeedforwardLayer(3)); network.AddLayer(new FeedforwardLayer(1)); network.Reset(); // train the neural network Train train = new HeatonResearchNeural.Feedforward.Train.Backpropagation.Backpropagation(network, XOR_INPUT, XOR_IDEAL, 0.7, 0.9); int epoch = 1; do { train.Iteration(); Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error); epoch++; } while ((epoch < 5000) && (train.Error > 0.001)); // test the neural network Console.WriteLine("Neural Network Results:"); for (int i = 0; i < XOR_IDEAL.Length; i++) { double[] actual = network.ComputeOutputs(XOR_INPUT[i]); Console.WriteLine(XOR_INPUT[i][0] + "," + XOR_INPUT[i][1] + ", actual=" + actual[0] + ",ideal=" + XOR_IDEAL[i][0]); } }
public void geneticNeural() { FeedforwardNetwork network = createNetwork(); // train the neural network Console.WriteLine("Determining initial scores"); TicTacToeGenetic train = new TicTacToeGenetic(network, true, NeuralTicTacToe.POPULATION_SIZE, NeuralTicTacToe.MUTATION_PERCENT, NeuralTicTacToe.MATE_PERCENT, this.player2.GetType()); train.UseThreadPool = true; ThreadPool.SetMaxThreads(NeuralTicTacToe.THREAD_POOL_SIZE, NeuralTicTacToe.THREAD_POOL_SIZE); int epoch = 1; DateTime started = DateTime.Now; int minutes = 0; do { train.Iteration(); TimeSpan span = (DateTime.Now - started); minutes = span.Minutes; Console.WriteLine("Epoch #" + epoch + " Error:" + train.getScore() + ",minutes left=" + (NeuralTicTacToe.TRAIN_MINUTES - minutes)); epoch++; } while (minutes < NeuralTicTacToe.TRAIN_MINUTES); SerializeObject.Save("tictactoe.net", train.Network); }
public TrainingSetNeuralGeneticAlgorithm(FeedforwardNetwork network, bool reset, double[][] input, double[][] ideal, int populationSize, double mutationPercent, double allowedPercentageToMate) { MutationPercent = mutationPercent; MatingPopulation = allowedPercentageToMate * 2; PopulationSize = populationSize; PercentToMate = allowedPercentageToMate; Input = input; Ideal = ideal; Chromosomes = new TrainingSetNeuralChromosome[PopulationSize]; for (var i = 0; i < Chromosomes.Length; i++) { var chromosomeNetwork = (FeedforwardNetwork)network.Clone(); if (reset) { chromosomeNetwork.Reset(); } var c = new TrainingSetNeuralChromosome(this, chromosomeNetwork); c.UpdateGenes(); SetChromosome(i, c); } SortChromosomes(); }
public static FeedforwardNetwork loadNetwork() { FeedforwardNetwork result = (FeedforwardNetwork)SerializeObject .Load("tictactoe.net"); return(result); }
static void Main(string[] args) { FeedforwardNetwork network = new FeedforwardNetwork(); network.AddLayer(new FeedforwardLayer(2)); network.AddLayer(new FeedforwardLayer(3)); network.AddLayer(new FeedforwardLayer(1)); network.Reset(); // train the neural network TrainingSetNeuralGeneticAlgorithm train = new TrainingSetNeuralGeneticAlgorithm( network, true, XOR_INPUT, XOR_IDEAL, 5000, 0.1, 0.25); int epoch = 1; do { train.Iteration(); Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error); epoch++; } while ((epoch < 5000) && (train.Error > 0.001)); network = train.Network; // test the neural network Console.WriteLine("Neural Network Results:"); for (int i = 0; i < XOR_IDEAL.Length; i++) { double [] actual = network.ComputeOutputs(XOR_INPUT[i]); Console.WriteLine(XOR_INPUT[i][0] + "," + XOR_INPUT[i][1] + ", actual=" + actual[0] + ",ideal=" + XOR_IDEAL[i][0]); } }
public void ThreadProc() { int update = 0; Prune prune = new Prune(0.7, 0.5, PruneIncrementalForm.XOR_INPUT, this.obtainIdeal(), 0.05); prune.StartIncremental(); while (!prune.Done) { prune.PruneIncramental(); update++; if (update == 10) { this.SetText("Cycles:" + prune.Cycles + ",Hidden Neurons:" + prune.HiddenNeuronCount + ", Current Error=" + prune.Error); update = 0; } } this.SetText("Best network found:" + prune.HiddenNeuronCount + ",error = " + prune.Error); this.network = prune.CurrentNetwork; //this.btnRun.Enabled = true; }
private void btnPrune_Click(object sender, EventArgs e) { Prune prune = new Prune(this.network, PruneSelectiveForm.XOR_INPUT, this.obtainIdeal(), 0.05); int count = prune.PruneSelective(); this.network = prune.CurrentNetwork; this.SetText("Prune removed " + count + " neurons."); }
/// <summary> /// Constructor that is designed to setup for a selective prune. /// </summary> /// <param name="network">The neural network that we wish to prune.</param> /// <param name="train">The training set input data.</param> /// <param name="ideal">The ideal outputs for the training set input data.</param> /// <param name="maxError">The maximum allowed error rate.</param> public Prune(FeedforwardNetwork network, double[][] train, double[][] ideal, double maxError) { this.currentNetwork = network; this.train = train; this.ideal = ideal; this.maxError = maxError; }
public TrainingSetNeuralChromosome(TrainingSetNeuralGeneticAlgorithm genetic, FeedforwardNetwork network) { GeneticAlgorithm = genetic; Network = network; InitGenes(network.MatrixSize); UpdateGenes(); }
/// <summary> /// Internal method used to clip the hidden neurons. /// </summary> /// <param name="neuron">The neuron to clip.</param> /// <returns>Returns the new neural network.</returns> protected FeedforwardNetwork ClipHiddenNeuron(int neuron) { FeedforwardNetwork result = (FeedforwardNetwork)this.currentNetwork .Clone(); ICollection <FeedforwardLayer> c = result.HiddenLayers; ((FeedforwardLayer)c.ElementAt(0)).Prune(neuron); return(result); }
/** * The constructor, takes a list of cities to set the initial "genes" to. * * @param cities * The order that this chromosome would visit the cities. These * cities can be thought of as the genes of this chromosome. * @throws NeuralNetworkException */ public TicTacToeChromosome(TicTacToeGenetic genetic, FeedforwardNetwork network) { this.GA = genetic; this.Network = network; InitGenes(network.MatrixSize); UpdateGenes(); }
public PruneSelectiveForm() { InitializeComponent(); this.network = new FeedforwardNetwork(); this.network.AddLayer(new FeedforwardLayer(NUM_INPUT)); this.network.AddLayer(new FeedforwardLayer(NUM_HIDDEN)); this.network.AddLayer(new FeedforwardLayer(NUM_OUTPUT)); this.network.Reset(); }
public void playNeural() { FeedforwardNetwork network = loadNetwork(); Player[] players = new Player[2]; players[0] = new PlayerNeural(network); players[1] = new PlayerMinMax(); ScorePlayer score = new ScorePlayer(players[0], players[1], true); score.score(); }
public static void ArrayToNetwork(double[] array, FeedforwardNetwork network) { int index = 0; foreach (FeedforwardLayer layer in network.Layers) { if (layer.NextLayer != null) { index = layer.LayerMatrix.FromPackedArray(array, index); } } }
/// <summary> /// Construct a simulated annleaing trainer for a feedforward neural network. /// </summary> /// <param name="network">The neural network to be trained.</param> /// <param name="input">The input values for training.</param> /// <param name="ideal">The ideal values for training.</param> /// <param name="startTemp">The starting temperature.</param> /// <param name="stopTemp">The ending temperature.</param> /// <param name="cycles">The number of cycles in a training iteration.</param> public NeuralSimulatedAnnealing(FeedforwardNetwork network, double[][] input, double[][] ideal, double startTemp, double stopTemp, int cycles) { this.network = network; this.input = input; this.ideal = ideal; this.temperature = startTemp; this.StartTemperature = startTemp; this.StopTemperature = stopTemp; this.Cycles = cycles; }
public NeuralSimulatedAnnealing(FeedforwardNetwork network, double[][] input, double[][] ideal, double startTemperature, double stopTemperature, int cyclesPerIteration) { Network = network; this._input = input; this._ideal = ideal; Temperature = startTemperature; StartTemperature = startTemperature; StopTemperature = stopTemperature; CyclesPerIteration = cyclesPerIteration; }
public Backpropagation(FeedforwardNetwork network, double[][] input, double[][] ideal, double learnRate, double momentum) { Network = network; _learnRate = learnRate; _momentum = momentum; _input = input; _ideal = ideal; foreach (var layer in network.Layers) _layerMapping.Add(layer, new BackpropagationLayer(this, layer)); }
/// <summary> /// Internal method that will loop through all hidden neurons and prune them /// if pruning the neuron does not cause too great of an increase in error. /// </summary> /// <returns>True if a prune was made, false otherwise.</returns> protected bool FindNeuron() { for (int i = 0; i < this.HiddenCount; i++) { FeedforwardNetwork trial = this.ClipHiddenNeuron(i); double e2 = DetermineError(trial); if (e2 < this.maxError) { this.currentNetwork = trial; return(true); } } return(false); }
/// <summary> /// Convert from an array. Use an array to populate the memory of the neural network. /// </summary> /// <param name="array">An array that will hold the memory of the neural network.</param> /// <param name="network">A neural network to convert to an array.</param> public static void ArrayToNetwork(Double[] array, FeedforwardNetwork network) { // copy data to array int index = 0; foreach (FeedforwardLayer layer in network.Layers) { // now the weight matrix(if it exists) if (layer.Next != null) { index = layer.LayerMatrix.FromPackedArray(array, index); } } }
override public void Randomize() { var rand = new Random(); var array = Network.ToArray(); for (var i = 0; i < array.Length; i++) { var add = 0.5 - (rand.NextDouble()); add = add / StartTemperature; add = add * Temperature; array[i] = array[i] + add; } FeedforwardNetwork.ArrayToNetwork(array, Network); }
public void Process() { this.network = NetworkUtil.CreateNetwork(); Console.WriteLine("Preparing training sets..."); this.common = new CommonWords(Config.FILENAME_COMMON_WORDS); this.histogramGood = new WordHistogram(this.common); this.histogramBad = new WordHistogram(this.common); // load the good words this.histogramGood.BuildFromFile(Config.FILENAME_GOOD_TRAINING_TEXT); this.histogramGood.BuildComplete(); // load the bad words this.histogramBad.BuildFromFile(Config.FILENAME_BAD_TRAINING_TEXT); this.histogramBad.BuildComplete(); // remove low scoring words this.histogramGood .RemoveBelow((int)this.histogramGood.CalculateMean()); this.histogramBad.RemovePercent(0.99); // remove common words this.histogramGood.RemoveCommon(this.histogramBad); this.histogramGood.Trim(Config.INPUT_SIZE); this.goodAnalysis = new AnalyzeSentences(this.histogramGood, Config.INPUT_SIZE); this.badAnalysis = new AnalyzeSentences(this.histogramGood, Config.INPUT_SIZE); this.goodAnalysis.Process(this.trainingSet, 0.9, Config.FILENAME_GOOD_TRAINING_TEXT); this.badAnalysis.Process(this.trainingSet, 0.1, Config.FILENAME_BAD_TRAINING_TEXT); this.sampleCount = this.trainingSet.Ideal.Count; Console.WriteLine("Processing " + this.sampleCount + " training sets."); AllocateTrainingSets(); CopyTrainingSets(); TrainNetworkBackpropBackprop(); SerializeObject.Save(Config.FILENAME_WHENBORN_NET, this.network); SerializeObject.Save(Config.FILENAME_HISTOGRAM, this.histogramGood); Console.WriteLine("Training complete."); }
public Backpropagation(FeedforwardNetwork network, double[][] input, double[][] ideal, double learnRate, double momentum) { Network = network; _learnRate = learnRate; _momentum = momentum; _input = input; _ideal = ideal; foreach (var layer in network.Layers) { _layerMapping.Add(layer, new BackpropagationLayer(this, layer)); } }
public static FeedforwardNetwork createNetwork() { FeedforwardNetwork network = new FeedforwardNetwork(); network.AddLayer(new FeedforwardLayer(9)); network .AddLayer(new FeedforwardLayer(NeuralTicTacToe.NEURONS_HIDDEN_1)); if (NeuralTicTacToe.NEURONS_HIDDEN_2 > 0) { network.AddLayer(new FeedforwardLayer( NeuralTicTacToe.NEURONS_HIDDEN_2)); } network.AddLayer(new FeedforwardLayer(1)); network.Reset(); return(network); }
public void createNetwork() { ActivationFunction threshold = new ActivationTANH(); this.network = new FeedforwardNetwork(); this.network.AddLayer(new FeedforwardLayer(threshold, INPUT_SIZE)); this.network.AddLayer(new FeedforwardLayer(threshold, SineWave.NEURONS_HIDDEN_1)); if (SineWave.NEURONS_HIDDEN_2 > 0) { this.network.AddLayer(new FeedforwardLayer(threshold, SineWave.NEURONS_HIDDEN_2)); } this.network.AddLayer(new FeedforwardLayer(threshold, OUTPUT_SIZE)); this.network.Reset(); }
public void geneticNeural() { Stopwatch sw = new Stopwatch(); FeedforwardNetwork network = createNetwork(); // train the neural network Console.WriteLine("Determining initial scores"); TicTacToeGenetic train = new TicTacToeGenetic(network, true, NeuralTicTacToe.POPULATION_SIZE, NeuralTicTacToe.MUTATION_PERCENT, NeuralTicTacToe.MATE_PERCENT, this.player2.GetType()); train.UseThreadPool = true; sw.Stop(); string duration = String.Format("Training_time: {0}", sw.Elapsed.Minutes); Console.WriteLine(duration); ThreadPool.SetMaxThreads(NeuralTicTacToe.THREAD_POOL_SIZE, NeuralTicTacToe.THREAD_POOL_SIZE); int epoch = 1; DateTime started = DateTime.Now; int minutes = 0; double error = train.getScore(); do { sw.Start(); error = train.getScore(); if (error > 0) { train.Iteration(); } sw.Stop(); minutes = sw.Elapsed.Minutes; string observation = String.Format("Epoch: {0}, Error:{1}, minutes_left : {2}", epoch, error, (NeuralTicTacToe.TRAIN_MINUTES - minutes)); Console.WriteLine(observation); epoch++; } while (minutes < NeuralTicTacToe.TRAIN_MINUTES && error > 0.00001d); SerializeObject.Save("tictactoe.net", train.Network); }
/// <summary> /// The rate at which the weight matrix will be adjusted based on /// learning. /// </summary> /// <param name="network">The neural network to be trained.</param> /// <param name="input">The input values to the neural network.</param> /// <param name="ideal">The ideal values expected from the neural network.</param> /// <param name="learnRate">The learning rate, how fast to modify neural network values.</param> /// <param name="momentum">The momentum, how much to use the previous training iteration for the current.</param> public Backpropagation(FeedforwardNetwork network, double[][] input, double[][] ideal, double learnRate, double momentum) { this.network = network; this.learnRate = learnRate; this.momentum = momentum; this.input = input; this.ideal = ideal; foreach (FeedforwardLayer layer in network.Layers) { BackpropagationLayer bpl = new BackpropagationLayer(this, layer); this.layerMap.Add(layer, bpl); } }
public TrainingSetNeuralGeneticAlgorithm(FeedforwardNetwork network, bool reset, double[][] input, double[][] ideal, int populationSize, double mutationPercent, double allowedPercentageToMate) { MutationPercent = mutationPercent; MatingPopulation = allowedPercentageToMate * 2; PopulationSize = populationSize; PercentToMate = allowedPercentageToMate; Input = input; Ideal = ideal; Chromosomes = new TrainingSetNeuralChromosome[PopulationSize]; for (var i = 0; i < Chromosomes.Length; i++) { var chromosomeNetwork = (FeedforwardNetwork)network.Clone(); if (reset) chromosomeNetwork.Reset(); var c = new TrainingSetNeuralChromosome(this, chromosomeNetwork); c.UpdateGenes(); SetChromosome(i, c); } SortChromosomes(); }