예제 #1
0
        public static Tuple<INetwork, IFactory> LoLa(string FileName, bool Encrypt)
        {
            Console.WriteLine("LoLa mode");
            Console.Write("Generating keys in ");
            var start = DateTime.Now;
            var Factory = Encrypt ? (IFactory)new EncryptedSealBfvFactory(new ulong[] { 557057, 638977, 737281, 786433 }, 8192) : new RawFactory(8192);
            var end = DateTime.Now;
            Console.WriteLine("{0} seconds", (end - start).TotalSeconds);

            int weightscale = 32;
            
            var readerLayer = new LLConvReader()
            {
                FileName = FileName,
                SparseFormat = true,
                NormalizationFactor = 1.0 / 256.0,
                Scale = 16.0,
                InputShape = new int[] { 28, 28 },
                KernelShape = new int[] { 5, 5 },
                Upperpadding = new int[] { 1, 1 },
                Stride = new int[] { 2, 2 },

            };
            var encryptLayer = new EncryptLayer() { Source = readerLayer, Factory = Factory };
            var ConvLayer1 = new LLPoolLayer()
            {
                Source = encryptLayer,
                InputShape = new int[] { 28, 28 },
                KernelShape = new int[] { 5, 5 },
                Upperpadding = new int[] { 1, 1 },
                Stride = new int[] { 2, 2 },
                MapCount = new int[] { 5, 1 },
                WeightsScale = weightscale,
                Weights = Weights.Weights_0
            };
            var VectorizeLayer2 = new LLVectorizeLayer() { Source = ConvLayer1 };

            //var ActivationLayer3 = new SquareActivation() { Source = VectorizeLayer2 };
            var ActivationLayer3 = new AppxReLUActivation() { Source = VectorizeLayer2 };
            //var ActivationLayer3 = new LeakyReLUActivation() { Source = VectorizeLayer2 };
            //var ActivationLayer3 = new ReLUActivation() { Source = VectorizeLayer2 };

            var DuplicateLayer4 = new LLDuplicateLayer() { Source = ActivationLayer3, Count = 8 };


            var DenseLayer5 = new LLPackedDenseLayer()
            {
                Source = DuplicateLayer4,
                Weights = Transpose(Weights.Weights_1, 5 * 13 * 13, 100),
                Bias = Weights.Biases_2,
                WeightsScale = weightscale * weightscale,
                PackingCount = DuplicateLayer4.Count,
                PackingShift = 1024
            };




            var InterleaveLayer6 = new LLInterleaveLayer()
            {
                Source = DenseLayer5,
                Shift = -1,
                SelectedIndices = Enumerable.Range(0, (int)DuplicateLayer4.Count).Select(i => 1023 + i * 1024).ToList()
            };
            //var ActivationLayer7 = new SquareActivation() { Source = InterleaveLayer6 };
            var ActivationLayer7 = new AppxReLUActivation() { Source = InterleaveLayer6 };
            //var ActivationLayer7 = new LeakyReLUActivation() { Source = InterleaveLayer6 };
            //var ActivationLayer7 = new ReLUActivation() { Source = InterleaveLayer6 };

            var DenseLayer8 = new LLInterleavedDenseLayer()
            {
                Source = ActivationLayer7,
                Weights = Weights.Weights_3,
                Bias = Weights.Biases_3,
                WeightsScale = weightscale,
                Shift = -1,
                SelectedIndices = Enumerable.Range(0, (int)DuplicateLayer4.Count).Select(i => 1023 + i * 1024).ToList()

            };

            return new Tuple<INetwork, IFactory>(DenseLayer8, Factory);
        }
예제 #2
0
        public static Tuple<INetwork, IFactory> LoLaDense(string FileName, bool Encrypt)
        {
            Console.WriteLine("LoLa-Dense mode");
            Console.Write("Generating keys in ");
            var start = DateTime.Now;
            var Factory = (Encrypt) ? (IFactory)new EncryptedSealBfvFactory(new ulong[] { 34359771137, 34360754177 }, 16384, DecompositionBitCount: 60, GaloisDecompositionBitCount: 60, SmallModulusCount: 7)
                : (IFactory)new RawFactory(16384);
            var end = DateTime.Now;
            Console.WriteLine("{0} seconds", (end - start).TotalSeconds);

            int weightscale = 32;

            var readerLayer = new LLSingleLineReader()
            {
                FileName = FileName,
                SparseFormat = true,
                NormalizationFactor = 1.0 / 256.0,
                Scale = 16.0,
            };
            var encryptLayer = new EncryptLayer() { Source = readerLayer, Factory = Factory };
            var preConvLayer1 = new LLPreConvLayer()
            {
                Source = encryptLayer,
                InputShape = new int[] { 28, 28 },
                KernelShape = new int[] { 5, 5 },
                Upperpadding = new int[] { 1, 1 },
                Stride = new int[] { 2, 2 },
                UseAxisForBlocks = new bool[] { true, true }
            };
            var ConvLayer2 = new LLPoolLayer()
            {
                Source = preConvLayer1,
                InputShape = new int[] { 28, 28 },
                KernelShape = new int[] { 5, 5 },
                Upperpadding = new int[] { 1, 1 },
                Stride = new int[] { 2, 2 },
                MapCount = new int[] { 5, 1 },
                WeightsScale = weightscale,
                Weights = Weights.Weights_0,
                HotIndices = preConvLayer1.HotIndices
            };

            var VectorizeLayer3 = new LLVectorizeLayer() { Source = ConvLayer2 };

            //var ActivationLayer4 = new SquareActivation() { Source = VectorizeLayer3 };
            var ActivationLayer4 = new AppxReLUActivation() { Source = VectorizeLayer3 };
            //var ActivationLayer4 = new LeakyReLUActivation() { Source = VectorizeLayer3 };
            //var ActivationLayer4 = new ReLUActivation() { Source = VectorizeLayer3 };

            var DuplicateLayer5 = new LLDuplicateLayer() { Source = ActivationLayer4, Count = 16};


            var DenseLayer6 = new LLPackedDenseLayer()
            {
                Source = DuplicateLayer5,
                Weights = preConvLayer1.RearrangeWeights(Transpose(Weights.Weights_1, 5 * 13 * 13, 100)),
                Bias = Weights.Biases_2,
                WeightsScale = weightscale * weightscale,
                PackingCount = DuplicateLayer5.Count,
                PackingShift = 1024,
            };

            //var ActivationLayer7 = new SquareActivation() { Source = DenseLayer6 };
            var ActivationLayer7 = new AppxReLUActivation() { Source = DenseLayer6 };
            //var ActivationLayer7 = new LeakyReLUActivation() { Source = DenseLayer6 };
            //var ActivationLayer7 = new ReLUActivation() { Source = DenseLayer6 };

            var InterleaveLayer8 = new LLInterleaveLayer()
            {
                Source = ActivationLayer7,
                Shift = -1,
                SelectedIndices = Enumerable.Range(0, (int)DuplicateLayer5.Count).Select(i => 1023 + i * 1024).ToList()
            };


            var DenseLayer8 = new LLInterleavedDenseLayer()
            {
                Source = InterleaveLayer8,
                Weights = Weights.Weights_3,
                Bias = Weights.Biases_3,
                WeightsScale = weightscale,
                Shift = -1,
                SelectedIndices = Enumerable.Range(0, (int)DuplicateLayer5.Count).Select(i => 1023 + i * 1024).ToList()

            };

            var network = DenseLayer8;

            return new Tuple<INetwork, IFactory>(network, Factory);
        }