Exemplo n.º 1
0
        public Conv2Layer(IAllocator allocator, SeedSource seedSource, DType elementType, int batchSize, int inputWidth, int inputHeight, int nInputPlane, int nOutputPlane, ConvolutionDesc2d cd)
        {
            this.cd = cd;

            this.weight = new NDArray(allocator, elementType, nOutputPlane, nInputPlane * cd.kW * cd.kH);
            this.bias   = new NDArray(allocator, elementType, nOutputPlane, 1);

            this.gradWeight = new NDArray(allocator, elementType, this.weight.Shape);
            this.gradBias   = new NDArray(allocator, elementType, this.bias.Shape);

            inputSizes     = new long[] { batchSize, nInputPlane, inputHeight, inputWidth };
            this.gradInput = new NDArray(allocator, elementType, inputSizes);

            outputSizes     = SpatialConvolutionMM.OutputSize(inputSizes, weight.Shape, cd);
            this.activation = new NDArray(allocator, elementType, outputSizes);



            this.OutputSizes = outputSizes;

            var stdv = 1.0f / (float)Math.Sqrt(cd.kW * cd.kH * nInputPlane);

            Ops.RandomUniform(weight, seedSource, -stdv, stdv);
            Ops.RandomUniform(bias, seedSource, -stdv, stdv);
        }
Exemplo n.º 2
0
        public Conv2Cpu(IAllocator allocator, SeedSource seedSource, DType elementType, int batchSize, int inputWidth, int inputHeight, int nInputPlane, int nOutputPlane, ConvolutionDesc2d cd)
            : base(allocator, seedSource, elementType, batchSize, inputWidth, inputHeight, nInputPlane, nOutputPlane, cd)
        {
            var finputSizes = SpatialConvolutionMM.FInputSize(inputSizes, outputSizes, cd);

            this.finput     = new NDArray(allocator, elementType, finputSizes);
            this.fgradInput = new NDArray(allocator, elementType, finputSizes);
        }
Exemplo n.º 3
0
 public override NDArray Backward(NDArray input, NDArray gradOutput, ModelMode mode)
 {
     SpatialConvolutionMM.Conv2BackwardInput(input, gradOutput, gradInput, weight, finput, fgradInput, cd);
     SpatialConvolutionMM.Conv2BackwardFilter(input, gradOutput, gradWeight, gradBias, finput, fgradInput, cd);
     return(gradInput);
 }
Exemplo n.º 4
0
 public override NDArray Forward(NDArray input, ModelMode mode)
 {
     SpatialConvolutionMM.Conv2Forward(input, activation, weight, bias, finput, cd);
     return(activation);
 }
Exemplo n.º 5
0
 public override Tensor Backward(Tensor input, Tensor gradOutput, ModelMode mode)
 {
     SpatialConvolutionMM.Conv2BackwardInput(input, gradOutput, gradInput, weight, finput, fgradInput, cd);
     SpatialConvolutionMM.Conv2BackwardFilter(input, gradOutput, gradWeight, gradBias, finput, fgradInput, cd);
     return(gradInput);
 }