Exemple #1
0
        public static void GAN()
        {
            //Generator genFunc = new Generator();
            //Discriminator disc = new Discriminator();
            //NeuralNetwork generator = new NeuralNetwork(new int[] { 100, 128, 28 * 28 }, new IActivationFunction[] { null, new Tanh(), new Sigmoid() }, genFunc);
            NeuralNetwork discriminator = new NeuralNetwork(new int[] { 28 * 28, 128, 1 }, new IActivationFunction[] { null, new Tanh(), new Sigmoid() }, new CrossEntropy(), new SGD());

            Random rng = new Random(0);

            real_in      = new float[50000][];
            expected_vec = new float[50000][];
            for (int i = 0; i < real_in.Length; i++)
            {
                real_in[i]      = new float[28 * 28];
                expected_vec[i] = new float[11];
                readMNIST(i);
            }

            float[] gen_out = new float[11];
            gen_out[10] = 0;

            for (int q = 0; q < 30; q++)
            {
                float[] Gz = null;
                for (int i = 0; i < 50000; i++)
                {
                    //if (expected_vec[i][0] != 1)
                    //    continue;

                    //for (int j = 0; j < generator_input.Length; j++)
                    //    generator_input[j] = (float)rng.NextDouble() * 1;

                    //Gz = generator.Activate(generator_input);
                    //var Dg = discriminator.Activate(Gz);
                    var Dz = discriminator.Activate(real_in[i]);

                    //disc.DiscriminatorValue = Dg[0] - 1;
                    //discriminator.Train(Gz, new float[] { 0 }, 3f / 1000);

                    //disc.DiscriminatorValue = Dz[0] + 0.001f;
                    discriminator.Train(real_in[i]); //, new float[] { expected_vec[i][0] }, 3f / 1000

                    //generator.Train(generator_input, real_in[i], 3f / 5000);


                    //Console.WriteLine($"Discriminator Output: {Dx.}");
                }
                //imsave(Gz);
                Console.WriteLine($"Epoch {q} Finished.");
            }

            for (int i = 0; i < 50; i++)
            {
                var Dx = discriminator.Activate(real_in[i]);

                float max = Dx.Max();
                for (int k = 0; k < Dx.Length; k++)
                {
                    if (expected_vec[i][k] == 1)
                    {
                        Console.WriteLine($"Expected:{k}");
                    }

                    if (Dx[k] == max)
                    {
                        Console.WriteLine($"Discriminator: {k}");
                    }
                }

                Console.WriteLine();
            }
        }
Exemple #2
0
        public static void GAN()
        {
            //Generator genFunc = new Generator();
            //Discriminator disc = new Discriminator();
            NeuralNetwork generator     = new NeuralNetwork(new int[] { 100, 128, 28 * 28 }, new IActivationFunction[] { null, new Tanh(), new Sigmoid() }, new CrossEntropy(), new SGD());
            NeuralNetwork discriminator = new NeuralNetwork(new int[] { 28 * 28, 128, 1 }, new IActivationFunction[] { null, new Tanh(), new Sigmoid() }, new CrossEntropy(), new SGD());

            Random rng = new Random(0);

            real_in      = new float[50000][];
            expected_vec = new float[50000][];
            for (int i = 0; i < real_in.Length; i++)
            {
                real_in[i]      = new float[28 * 28];
                expected_vec[i] = new float[11];
                readMNIST(i);
            }

            float[] gen_out = new float[11];
            gen_out[10] = 0;

            float max_gen_res = 0;

            for (int q = 0; q < 30; q++)
            {
                float[] Gz = null;
                for (int i = 0; i < 50000; i++)
                {
                    if (expected_vec[i][6] != 1)
                    {
                        continue;
                    }

                    for (int j = 0; j < generator_input.Length; j++)
                    {
                        generator_input[j] = (float)rng.NextGaussian(0, System.Math.Pow(28 * 28, 0.5f)) * 1;
                    }

                    Gz = generator.Activate(generator_input);

                    //Train discriminator
                    var(Dz_a, Dz_z)             = discriminator.Train(real_in[i]);
                    var(Dz_nabla_w, Dz_nabla_b) = discriminator.Solver.Solve(Dz_a, Dz_z, new Vector(new float[] { 1 }));
                    (discriminator.Weights, discriminator.Biases) = discriminator.Optimizer.Optimize(discriminator.Weights, discriminator.Biases, Dz_nabla_w, Dz_nabla_b, 3f / 1000);

                    var(DGz_a, DGz_z)             = discriminator.Train(Gz);
                    var(DGz_nabla_w, DGz_nabla_b) = discriminator.Solver.Solve(DGz_a, DGz_z, new Vector(new float[] { 0 }));
                    (discriminator.Weights, discriminator.Biases) = discriminator.Optimizer.Optimize(discriminator.Weights, discriminator.Biases, DGz_nabla_w, DGz_nabla_b, 3f / 1000);

                    //Train Generator
                    //Now compute the error from the output of D, but don't change anything until G
                    NeuralNetwork chained = new NeuralNetwork(generator, discriminator);
                    var(Gz_a_ex, Gz_z_ex)             = chained.Train(generator_input);
                    var(Gz_nabla_w_ex, Gz_nabla_b_ex) = chained.Solver.Solve(Gz_a_ex, Gz_z_ex, new Vector(new float[] { 1 }));

                    Matrix[] Gz_nabla_w = new ArraySegment <Matrix>(Gz_nabla_w_ex, 0, generator.LayerCount).Array;
                    Vector[] Gz_nabla_b = new ArraySegment <Vector>(Gz_nabla_b_ex, 0, generator.LayerCount).Array;
                    (generator.Weights, generator.Biases) = generator.Optimizer.Optimize(generator.Weights, generator.Biases, Gz_nabla_w, Gz_nabla_b, 3f / 1000);

                    if (max_gen_res < DGz_a[2][0])
                    {
                        imsave(Gz);
                        max_gen_res = DGz_a[2][0];
                    }


                    Console.WriteLine($"[{i}]Discriminator Real: {Dz_a[2][0]} Fake: {DGz_a[2][0]}");
                }
                //imsave(Gz);
                Console.WriteLine($"Epoch {q} Finished.");
            }

            for (int i = 0; i < 50; i++)
            {
                var Dx = discriminator.Activate(real_in[i]);

                float max = Dx.Max();
                for (int k = 0; k < Dx.Length; k++)
                {
                    if (expected_vec[i][k] == 1)
                    {
                        Console.WriteLine($"Expected:{k}");
                    }

                    if (Dx[k] == max)
                    {
                        Console.WriteLine($"Discriminator: {k}");
                    }
                }

                Console.WriteLine();
            }
        }