public virtual void PrepareNetwork() { if (Source != null) { Source.PrepareNetwork(); } if (Verbose) { OperationsCount.Reset(); DateTime start = DateTime.Now; Prepare(); layerPrepared = true; var end = DateTime.Now; Console.WriteLine("Prepare {0} computed in {1} seconds ({2} -- {3})", this.GetType().Name, (end - start).TotalSeconds, start.ToString("hh:mm:ss.fff"), end.ToString("hh:mm:ss.fff")); OperationsCount.Print(); } else { Prepare(); layerPrepared = true; } }
public void CryptoNetsPoolLayer() { OperationsCount.Reset(); int batchSize = 8192; var Factory = new EncryptedSealBfvFactory(new ulong[] { 549764251649 /*, 549764284417*/ }, (ulong)batchSize); int weightscale = 32; var DenseLayer3 = new PoolLayer() { Source = new DummyLayer(), InputShape = new int[] { 5 * 13 * 13 }, KernelShape = new int[] { 5 * 13 * 13 }, Stride = new int[] { 1000 }, MapCount = new int[] { 100 }, Weights = CryptoNets.CryptoNets.Transpose(Weights.Weights_1, 5 * 13 * 13, 100), Bias = Weights.Biases_2, WeightsScale = weightscale * weightscale, Factory = Factory }; DenseLayer3.Prepare(); var input = Matrix <double> .Build.Dense(8192, 5 * 13 * 13); var m = Factory.GetEncryptedMatrix(input, EMatrixFormat.ColumnMajor, 1); var start = DateTime.Now; var z = DenseLayer3.Apply(m); var stop = DateTime.Now; var time = (stop - start).TotalMilliseconds; Console.WriteLine("time {0}", time); OperationsCount.Print(); z.Dispose(); m.Dispose(); DenseLayer3.Dispose(); }
public virtual IMatrix GetNext() { if (!layerPrepared) { Prepare(); } var m = Source.GetNext(); #if DEBUG Trace = Environment.StackTrace; #endif if (Verbose) { OperationsCount.Reset(); DateTime start = DateTime.Now; var res = Apply(m); var end = DateTime.Now; Console.WriteLine("Layer {0} computed in {1} seconds ({2} -- {3}) layer width ({4},{5})", this.GetType().Name, (end - start).TotalSeconds, start.ToString("hh:mm:ss.fff"), end.ToString("hh:mm:ss.fff"), m.RowCount, m.ColumnCount); CryptoTracker.TestBudget(res.GetColumn(0), Factory); OperationsCount.Print(); if (res != m) { m.Dispose(); } return(res); } else { var res = Apply(m); if (res != m) { m.Dispose(); } return(res); } }
static void Main(string[] args) { string fileName = "MNIST-28x28-test.txt"; int batchSize = 8192; int numberOfRecords = 10000; var Factory = new EncryptedSealBfvFactory(new ulong[] { 2148728833, 2148794369, 2149810177 }, 16384, DecompositionBitCount: 60, GaloisDecompositionBitCount: 60, SmallModulusCount: 7); int weightscale = 32; var ReaderLayer = new BatchReader { FileName = fileName, SparseFormat = true, MaxSlots = batchSize, NormalizationFactor = 1.0 / 256.0, Scale = 16.0 }; var EncryptedLayer = new EncryptLayer() { Source = ReaderLayer, Factory = Factory }; var StartTimingLayer = new TimingLayer() { Source = EncryptedLayer, StartCounters = new string[] { "Batch-Time" } }; var ConvLayer1 = new PoolLayer() { Source = StartTimingLayer, 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 ActivationLayer2 = new SquareActivation() { Source = ConvLayer1 }; var ActivationLayer2 = new AppxReLUActivation() { Source = ConvLayer1 }; var DenseLayer3 = new PoolLayer() { Source = ActivationLayer2, InputShape = new int[] { 5 * 13 * 13 }, KernelShape = new int[] { 5 * 13 * 13 }, Stride = new int[] { 1000 }, MapCount = new int[] { 100 }, Weights = Transpose(Weights.Weights_1, 5 * 13 * 13, 100), Bias = Weights.Biases_2, WeightsScale = weightscale * weightscale }; //var ActivationLayer4 = new SquareActivation() { Source = DenseLayer3 }; var ActivationLayer4 = new AppxReLUActivation() { Source = DenseLayer3 }; var DenseLayer5 = new PoolLayer() { Source = ActivationLayer4, InputShape = new int[] { 100 }, KernelShape = new int[] { 100 }, Stride = new int[] { 1000 }, MapCount = new int[] { 10 }, Weights = Weights.Weights_3, Bias = Weights.Biases_3, WeightsScale = weightscale }; var StopTimingLayer = new TimingLayer() { Source = DenseLayer5, StopCounters = new string[] { "Batch-Time" } }; var network = StopTimingLayer; OperationsCount.Reset(); Console.WriteLine("Preparing"); network.PrepareNetwork(); OperationsCount.Print(); OperationsCount.Reset(); for (var p = (INetwork)network; p != null; p = p.Source) { if (p is BaseLayer b) { b.Verbose = true; } } int count = 0; int errs = 0; while (count < numberOfRecords) { using (var m = network.GetNext()) Utils.ProcessInEnv(env => { var decrypted = m.Decrypt(env); for (int i = 0; i < decrypted.RowCount; i++) { int pred = 0; for (int j = 1; j < decrypted.ColumnCount; j++) { if (decrypted[i, j] > decrypted[i, pred]) { pred = j; } } if (pred != ReaderLayer.Labels[i]) { errs++; } count++; if (count % 100 == 0) { Console.WriteLine("errs {0}/{1} accuracy {2:0.000}% prediction {3} label {4}", errs, count, 100 - (100.0 * errs / (count)), pred, ReaderLayer.Labels[i]); } } Console.WriteLine("Batch size {0} {1}", batchSize, TimingLayer.GetStats()); }, Factory); } Console.WriteLine("errs {0}/{1} accuracy {2:0.000}%", errs, count, 100 - (100.0 * errs / (count))); network.DisposeNetwork(); }