public Convolution2D(Variable <T> data, int kernelH, int kernelW, int numFilter) { Util.EnsureTrue(data.Shape.Rank == 4); Util.EnsureTrue(data.Shape[1] > 0); Util.EnsureTrue(data.Shape[2] > 0); Util.EnsureTrue(data.Shape[3] > 0); var numInputFilter = data.Shape[1]; var numOutputFilter = numFilter; var height = data.Shape[2]; var width = data.Shape[3]; // fixed padding and stride now ConvolutionDesc = new ConvolutionDescriptor(); ConvolutionDesc.Set2D(0, 0, 1, 1, 1, 1, ConvolutionMode.CROSS_CORRELATION); using (var dataDesc = new TensorDescriptor()) using (var weightDesc = new FilterDescriptor()) { var dataType = Dnn.DataTypeOf <T>(); var tempN = 100; // for temp mini batch size dataDesc.Set4D(dataType, TensorFormat.CUDNN_TENSOR_NCHW, tempN, (int)numInputFilter, (int)height, (int)width); weightDesc.Set4D(dataType, TensorFormat.CUDNN_TENSOR_NCHW, numOutputFilter, (int)numInputFilter, kernelH, kernelW); // get output dimension int n, c, h, w; ConvolutionDesc.Get2DForwardOutputDim(dataDesc, weightDesc, out n, out c, out h, out w); //Console.WriteLine($"{c},{h},{w}"); // Create variables var scale = Sqrt(3.0.AsScalar <T>() / ((double)(numInputFilter * kernelH * kernelW)).AsScalar <T>()); Data = data; Weight = Parameter(scale * (2.0.AsScalar <T>() * RandomUniform <T>(Shape.Create(numOutputFilter, numInputFilter, kernelH, kernelW), 0UL, 0UL) - 1.0.AsScalar <T>())); Bias = Parameter(Fill(Shape.Create(c), ScalarOps.Conv <T>(0.1))); Output = Variable <T>(PartialShape.Create(-1, c, h, w)); Workspace1 = AuxVariable <byte>(); Workspace2 = AuxVariable <byte>(); AddInput(Data); AddInput(Weight); AddInput(Bias); AddOutput(Output); AddAuxVar(Workspace1); AddAuxVar(Workspace2); } }
// cuDNN fields setup private void SetupCuDnnInfo() { ConvolutionDescription.Set2D(OperationInfo.VerticalPadding, OperationInfo.HorizontalPadding, OperationInfo.VerticalStride, OperationInfo.HorizontalStride, 1, 1, (Alea.cuDNN.ConvolutionMode)OperationInfo.Mode); FilterDescription.Set4D(DataType.FLOAT, TensorFormat.CUDNN_TENSOR_NCHW, OutputInfo.Channels, KernelInfo.Channels, KernelInfo.Height, KernelInfo.Width); BiasDescription.Set4D(DataType.FLOAT, TensorFormat.CUDNN_TENSOR_NCHW, 1, OutputInfo.Channels, 1, 1); }