Exemple #1
0
        // Constructs a convolutional network with two convolutional layers,
        // two fully-connected layers, ReLU units and a softmax on the output.
        public static void BuildCnn(IAllocator allocator, SeedSource seedSource, int batchSize, bool useCudnn, out Sequential model, out ICriterion criterion, out bool outputIsClassIndices)
        {
            var inputWidth  = MnistParser.ImageSize;
            var inputHeight = MnistParser.ImageSize;

            var elementType = DType.Float32;

            var inputDims = new long[] { batchSize, 1, inputHeight, inputWidth };

            model = new Sequential();
            model.Add(new ViewLayer(inputDims));

            var outSize = AddCnnLayer(allocator, seedSource, elementType, model, inputDims, 20, useCudnn);

            outSize = AddCnnLayer(allocator, seedSource, elementType, model, outSize, 40, useCudnn);

            var convOutSize = outSize[1] * outSize[2] * outSize[3];

            model.Add(new ViewLayer(batchSize, convOutSize));

            var hiddenSize = 1000;
            var outputSize = 10;

            model.Add(new DropoutLayer(allocator, seedSource, elementType, 0.5f, batchSize, convOutSize));
            model.Add(new LinearLayer(allocator, seedSource, elementType, (int)convOutSize, hiddenSize, batchSize));
            model.Add(new ReLULayer(allocator, elementType, batchSize, hiddenSize));

            model.Add(new DropoutLayer(allocator, seedSource, elementType, 0.5f, batchSize, hiddenSize));
            model.Add(new LinearLayer(allocator, seedSource, elementType, hiddenSize, outputSize, batchSize));
            model.Add(LayerBuilder.BuildLogSoftMax(allocator, elementType, batchSize, outputSize, useCudnn));

            criterion            = new ClassNLLCriterion(allocator, batchSize, outputSize);
            outputIsClassIndices = true; // output of criterion is class indices
        }
Exemple #2
0
        // Constructs a network with two fully-connected layers; one sigmoid, one softmax
        public static void BuildMLPSoftmax(IAllocator allocator, SeedSource seedSource, int batchSize, bool useCudnn, out Sequential model, out ICriterion criterion, out bool outputIsClassIndices)
        {
            int inputSize  = MnistParser.ImageSize * MnistParser.ImageSize;
            int hiddenSize = 100;
            int outputSize = MnistParser.LabelCount;

            var elementType = DType.Float32;

            model = new Sequential();
            model.Add(new ViewLayer(batchSize, inputSize));

            model.Add(new LinearLayer(allocator, seedSource, elementType, inputSize, hiddenSize, batchSize));
            model.Add(new SigmoidLayer(allocator, elementType, batchSize, hiddenSize));

            model.Add(new LinearLayer(allocator, seedSource, elementType, hiddenSize, outputSize, batchSize));
            model.Add(LayerBuilder.BuildLogSoftMax(allocator, elementType, batchSize, outputSize, useCudnn));

            criterion            = new ClassNLLCriterion(allocator, batchSize, outputSize);
            outputIsClassIndices = true; // output of criterion is class indices
        }