Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //Generate the training data
            int            keySize         = 64;
            int            messageSize     = 64;
            int            trainingSetSize = 100;
            List <Triplet> trainingSet     = GenerateDESDataset(trainingSetSize, keySize, messageSize);

            double[][] inputTraining, outputTraining;
            Triplet.Transform2IO(trainingSet, out inputTraining, out outputTraining);

            //Generate the test data
            List <Triplet> testSet = GenerateDESDataset(trainingSetSize, keySize, messageSize);

            double[][] inputTest, outputTest;
            Triplet.Transform2IO(testSet, out inputTest, out outputTest);

            //Find the right sizes, not sure why I have to do that :-/
            int inputSize  = trainingSet.First().original.Count() + trainingSet.First().encrypted.Count();
            int outputSize = trainingSet.First().key.Count();

            //Create a network
            var function = new SigmoidFunction(2.0);
            //ActivationNetwork network = new ActivationNetwork(function, inputSize, 25, outputSize);
            //ParallelResilientBackpropagationLearning teacher = new ParallelResilientBackpropagationLearning(network);

            DeepBeliefNetwork network = new DeepBeliefNetwork(inputSize, 10, outputSize);

            Accord.Neuro.Learning.DeepNeuralNetworkLearning teacher = new DeepNeuralNetworkLearning(network);

            //Train the network
            int    epoch               = 0;
            double stopError           = 0.1;
            int    resets              = 0;
            double minimumErrorReached = double.PositiveInfinity;

            while (minimumErrorReached > stopError && resets < 1)
            {
                network.Randomize();
                //teacher.Reset(0.0125);

                double errorTrain = double.PositiveInfinity;
                for (epoch = 0; epoch < 500000 && errorTrain > stopError; epoch++)
                {
                    errorTrain = teacher.RunEpoch(inputTraining, outputTraining) / (double)trainingSetSize;
                    //Console.WriteLine("Epoch " + epoch + " = \t" + error);
                    if (errorTrain < minimumErrorReached)
                    {
                        minimumErrorReached = errorTrain;
                        network.Save("cryptoDESNetwork.mlp");
                    }
                    Console.Clear();
                    Console.WriteLine("Epoch : " + epoch);
                    Console.WriteLine("Train Set  Error : " + errorTrain.ToString("N2"));
                    double errorTest = teacher.ComputeError(inputTest, outputTest) / (double)inputTest.Count();
                    Console.WriteLine("Test Set  Error : " + errorTest.ToString("N2"));
                }
                //Console.Write("Reset (" + error+")->");
                resets++;
            }
            Console.WriteLine();

            //Compute the reall error
            foreach (Triplet tReal in testSet)
            {
                double[] rIn, rOut, pOut;
                byte[]   brMsg, brEncrypted, brKey;
                tReal.ToBytes(out brMsg, out brEncrypted, out brKey);

                tReal.ToIO(out rIn, out rOut);
                pOut = network.Compute(rIn);

                Triplet tPredicted = new Triplet(rIn, pOut, messageSize);
                byte[]  bpMsg, bpEncrypted, bpKey;
                tPredicted.ToBytes(out bpMsg, out bpEncrypted, out bpKey);

                int wrongBytes = 0;
                for (int i = 0; i < keySize / 8; i++)
                {
                    if (brKey[i] != bpKey[i])
                    {
                        wrongBytes++;
                    }
                }
                Console.WriteLine("Wrong bytes = " + wrongBytes);
                //Console.WriteLine("REAL = \n" + tReal.GetBytesForm());
                //Console.WriteLine("Predicted = \n" + tPredicted.GetBytesForm());
            }

            Console.ReadKey();
        }