public unsafe void SoftmaxBackwardOutput()
        {
            float[,]
            x = WeightsProvider.NewFullyConnectedWeights(TensorInfo.Linear(400), 250, WeightsInitializationMode.GlorotNormal).AsSpan().AsMatrix(400, 250),
            y = new float[400, 127];
            for (int i = 0; i < 400; i++)
            {
                y[i, ThreadSafeRandom.NextInt(max: 127)] = 1;
            }
            OutputLayerBase
                cpu = new SoftmaxLayer(TensorInfo.Linear(250), 127, WeightsInitializationMode.GlorotNormal, BiasInitializationMode.Gaussian),
                gpu = new CuDnnSoftmaxLayer(cpu.InputInfo, cpu.OutputInfo.Size, cpu.Weights, cpu.Biases);

            fixed(float *px = x, py = y)
            {
                Tensor.Reshape(px, x.GetLength(0), x.GetLength(1), out Tensor xt);
                cpu.Forward(xt, out Tensor z, out Tensor a);
                a.Duplicate(out Tensor a2);
                Tensor.Reshape(py, y.GetLength(0), y.GetLength(1), out Tensor yt);
                cpu.Backpropagate(a, yt, z);
                gpu.Backpropagate(a2, yt, z);
                Assert.IsTrue(a.ContentEquals(a2));
                a.Free();
                a2.Free();
                z.Free();
            }
        }
Пример #2
0
        public Chromosome(int length)
        {
            Fitness = 0;
            _length = length;
            Genome  = new int[_length];

            for (var i = 0; i < _length; i++)
            {
                Genome[i] = ThreadSafeRandom.NextInt(int.MinValue, int.MaxValue);
            }
        }
        public void SoftmaxBackwardOutput()
        {
            float[,] y = new float[400, 127];
            for (int i = 0; i < 400; i++)
            {
                y[i, ThreadSafeRandom.NextInt(max: 127)] = 1;
            }
            OutputLayerBase
                cpu = new SoftmaxLayer(TensorInfo.Linear(250), 127, WeightsInitializationMode.GlorotNormal, BiasInitializationMode.Gaussian),
                gpu = new CuDnnSoftmaxLayer(cpu.InputInfo, cpu.OutputInfo.Size, cpu.Weights, cpu.Biases);

            TestBackward(cpu, gpu, y);
        }
Пример #4
0
        protected override void CrossChromosomes(Chromosome parent1, Chromosome parent2, int index)
        {
            var start = ThreadSafeRandom.NextInt(parent1.GenomeLength());
            var end   = ThreadSafeRandom.NextInt(parent1.GenomeLength());

            var child1 = parent1.Clone();
            var child2 = parent2.Clone();

            NewPopulation[index] = child1;

            if (index + 1 < NewPopulation.Count)
            {
                NewPopulation[index + 1] = child2;
            }

            if (ThreadSafeRandom.NextDouble() > CrossoverProbability)
            {
                return;
            }

            if (start > end)
            {
                var tmp = start;
                start = end;
                end   = tmp;
            }

            for (var i = start; i <= end; i++)
            {
                var gen = i / 32;
                var bit = i - gen * 32;


                if (bit == 0 && end / 32 != gen)
                {
                    var genome = child1.Genome[gen];
                    child1.Genome[gen] = child2.Genome[gen];
                    child2.Genome[gen] = genome;

                    i += 31;
                }
                else if (((child1.Genome[gen] >> bit & 1) ^ (child2.Genome[gen] >> bit & 1)) == 1)
                {
                    child1.Genome[gen] ^= (int)(1 << bit);
                    child2.Genome[gen] ^= (int)(1 << bit);
                }
            }
        }
Пример #5
0
        public override void SelectPopulation()
        {
            Parallel.For(0, _populationSize, i =>
            {
                var tournament = new Chromosome[_tournamentSize];

                for (var j = 0; j < _tournamentSize; j++)
                {
                    tournament[j] = Population[ThreadSafeRandom.NextInt(Population.Count)];
                }

                NewPopulation[i] = tournament.MinBy(ch => ch.Fitness).First();
            });

            Parallel.For(0, _populationSize, i => Population[i] = NewPopulation[i].Clone());
        }
        // Tries to process a random batch through the network (this method should just not crash)
        private static void ValidateGraph([NotNull] INeuralNetwork network)
        {
            float[,]
            x = new float[200, network.InputInfo.Size],
            y = new float[200, network.OutputInfo.Size];
            for (int i = 0; i < 200; i++)
            {
                for (int j = 0; j < x.GetLength(1); j++)
                {
                    x[i, j] = ThreadSafeRandom.NextFloat();
                }
                y[i, ThreadSafeRandom.NextInt(max: y.GetLength(1))] = 1;
            }
            _ = network.Forward(x);
            SamplesBatch            batch = new SamplesBatch(x, y);
            ComputationGraphNetwork graph = network.To <INeuralNetwork, ComputationGraphNetwork>();

            graph.Backpropagate(batch, 0.5f, WeightsUpdaters.AdaDelta(TrainingAlgorithms.AdaDelta(), graph));
            _ = network.ExtractDeepFeatures(x);
        }
Пример #7
0
        public void CrossPopulation()
        {
            for (var i = 0; i < _populationSize; i += 2)
            {
                var parent1 = Population[ThreadSafeRandom.NextInt(_populationSize)];

                Chromosome parent2;

                do
                {
                    parent2 = Population[ThreadSafeRandom.NextInt(_populationSize)];
                } while (parent1.Equals(parent2));

                CrossChromosomes(parent1, parent2, i);
            }

            for (var i = 0; i < _populationSize; i++)
            {
                Population[i] = NewPopulation[i];
            }
        }
Пример #8
0
        protected override void Mutate(Chromosome chromosome)
        {
            var index = ThreadSafeRandom.NextInt(chromosome.GenomeLength());

            chromosome.Genome[index / 32] ^= (int)(1 << (index % 32));
        }