Ejemplo n.º 1
0
 // Create Value object from sparse input as sequence data, for N-dimensional tensor. Only CreateSequence() for now.
 public static Value CreateSequence <T>(NDShape sampleShape, int sequenceLength,
                                        int[] colStarts, int[] rowIndices, T[] nonZeroValues,
                                        DeviceDescriptor device,
                                        bool readOnly = false)
 {
     return(Value.CreateSequence <T>(sampleShape, sequenceLength, colStarts, rowIndices, nonZeroValues, true, device, readOnly));
 }
Ejemplo n.º 2
0
        // Create Value object from sparse input as sequence data with sequenceStartFlag, for N-dimensional tensor. Only CreateSequence() for now.
        public static Value CreateSequence <T>(NDShape sampleShape, int sequenceLength,
                                               int[] colStarts, int[] rowIndices, T[] nonZeroValues,
                                               bool sequenceStartFlag,
                                               DeviceDescriptor device,
                                               bool readOnly = false)
        {
            if (nonZeroValues.Length != rowIndices.Length)
            {
                throw new System.ArgumentException("The length of rowIndicies must be same as the length of nonZeroValues.");
            }
            if (colStarts.Length != sequenceLength + 1)
            {
                throw new System.ArgumentException("The length of colStarts must be equal to (sequenceLength + 1)");
            }
            uint numNonZeroValues = (uint)nonZeroValues.Length;

            if (typeof(T).Equals(typeof(float)))
            {
                return(Value._CreateSequenceFloat(sampleShape, (uint)sequenceLength, colStarts, rowIndices, nonZeroValues as float[], numNonZeroValues, sequenceStartFlag, device, readOnly));
            }
            else if (typeof(T).Equals(typeof(double)))
            {
                return(Value._CreateSequenceDouble(sampleShape, (uint)sequenceLength, colStarts, rowIndices, nonZeroValues as double[], numNonZeroValues, sequenceStartFlag, device, readOnly));
            }
            else
            {
                throw new System.ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
            }
        }
Ejemplo n.º 3
0
 // Create Value object from NDArrayViews.
 public static Value Create(NDShape sampleShape,
                            System.Collections.Generic.IEnumerable <NDArrayView> sequences,
                            DeviceDescriptor device,
                            bool readOnly = false)
 {
     return(Create(sampleShape, sequences, new System.Collections.Generic.List <bool>(0), device, readOnly));
 }
Ejemplo n.º 4
0
        // Create Value object from OneHotVector input, for N-dimenstional tensor. Only Create() method for now.
        public static Value Create <T>(NDShape sampleShape,
                                       System.Collections.Generic.IEnumerable <System.Collections.Generic.IEnumerable <int> > sequences,
                                       System.Collections.Generic.IEnumerable <bool> sequenceStartFlags,
                                       DeviceDescriptor device,
                                       bool readOnly = false)
        {
            var seqFlags       = Helper.AsBoolVector(sequenceStartFlags);
            var inputSeqVector = new SizeTVectorVector();

            foreach (var seq in sequences)
            {
                var s = Helper.AsSizeTVector(seq);
                inputSeqVector.Add(s);
            }
            if (typeof(T).Equals(typeof(float)))
            {
                return(Value._CreateOneHotFloat(sampleShape, inputSeqVector, seqFlags, device, readOnly));
            }
            else if (typeof(T).Equals(typeof(double)))
            {
                return(Value._CreateOneHotDouble(sampleShape, inputSeqVector, seqFlags, device, readOnly));
            }
            else
            {
                throw new System.ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
            }
        }
Ejemplo n.º 5
0
 public NDMask(NDShape shape) : this(CNTKLibPINVOKE.new_NDMask__SWIG_1(NDShape.getCPtr(shape)), true)
 {
     if (CNTKLibPINVOKE.SWIGPendingException.Pending)
     {
         throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 6
0
        //Copy bias to match the dimensions of the input
        private static float[] make_copies(float[] kernel, NDShape dims, bool swap_axes = false)
        {
            int K = dims[0] * dims[1];

            float[] outarray = new float[dims[0] * dims[1] * dims[2]];
            if (swap_axes == false)
            {
                //Loop over number of maps
                for (int k = 0; k < dims[2]; k++)
                {
                    //Loop over spatial dimensions
                    for (int kk = 0; kk < K; kk++)
                    {
                        outarray[k * K + kk] = kernel[k];
                    }
                }
            }
            else
            {
                //Loop over number of maps
                for (int k = 0; k < K; k++)
                {
                    //Loop over spatial dimensions
                    for (int kk = 0; kk < dims[2]; kk++)
                    {
                        outarray[k * dims[2] + kk] = kernel[kk];
                    }
                }
            }
            return(outarray);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// from HxWxC input, reshape it to (H*W)xC, get mean and covariance matrix for channels
        /// Uses CNTK . This is priamarily used by whitening related
        /// </summary>
        /// <param name="input"></param>
        /// <param name="device"></param>
        /// <returns>tuple of cov, mean and reshaped inputs</returns>
        private static Tuple <Value, Value, Value> CovarianceMatrixFromImage(Value image, DeviceDescriptor device, out Tuple <Variable, Variable, Variable> outputs)
        {
            //create the CNTK model to multiply and reshape to get the covariance matrix, also get the mean
            NDShape  inputShape = image.Shape;
            Variable input      = Variable.InputVariable(inputShape, DataType.Float, "input");
            Variable reshaped   = CNTKLib.Reshape(input, new int[] { inputShape[0] * inputShape[1], inputShape[2] });//change the shape to (W*H)xC
            Variable mean       = CNTKLib.ReduceMean(reshaped, new Axis(0));
            Variable centered   = CNTKLib.Minus(reshaped, mean);
            Variable covMat     = CNTKLib.ElementDivide(CNTKLib.TransposeTimes(centered, centered), Constant.Scalar(DataType.Float, inputShape[0] * inputShape[1] - 1.0));

            var inputDataMap = new Dictionary <Variable, Value>()
            {
                { input, image }
            };
            var outputDataMap = new Dictionary <Variable, Value>()
            {
                { covMat, null }, { mean, null }, { centered, null }
            };

            ((Function)covMat).Evaluate(inputDataMap, outputDataMap, false, device);

            outputs = new Tuple <Variable, Variable, Variable>(covMat, mean, reshaped);
            var result = new Tuple <Value, Value, Value>(outputDataMap[covMat], outputDataMap[mean], outputDataMap[centered]);

            return(result);
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            string filePath = @"D:/Users/Sachit/source/repos/SamplesRepo/IrisData/IrisData/iris-data/saved_model.model";
            var    cpu      = DeviceDescriptor.UseDefaultDevice();

            Console.WriteLine($"Hello from CNTK for {cpu.Type} only!");
            Variable input;


            Function f      = Function.Load(filePath, cpu);
            Variable inp    = f.Arguments.Single();
            NDShape  iShape = inp.Shape;

            var iDataMap = new Dictionary <Variable, Value>();

            float[] a = new float[]  { 1.2f, 2.3f, 3f, 4f, 8f, 2f, 3f, 1f };

            var iVal = Value.CreateBatch(iShape, a, cpu);

            iDataMap.Add(inp, iVal);

            Variable outVar     = f.Output;
            var      outDataMap = new Dictionary <Variable, Value>();

            outDataMap.Add(outVar, null);

            f.Evaluate(iDataMap, outDataMap, cpu);
            var oval  = outDataMap[outVar];
            var odata = oval.GetDenseData <float>(outVar);

            Console.WriteLine("");
        }
Ejemplo n.º 9
0
        public static NDArrayView SafeCreate(NDShape shape, float[] data, DeviceDescriptor device)
        {
            if (device == null)
            {
                device = DeviceDescriptor.UseDefaultDevice();
            }

            if (device == DeviceDescriptor.CPUDevice)
            {
                unsafe
                {
                    fixed(float *f = data)
                    {
                        using (var a = new NDArrayView(shape, data, DeviceDescriptor.CPUDevice, false))
                        {
                            // The NDArrayview constructor with float[] data does not copy nor hold the reference to the source data.
                            // To make the object keep track of its data by itself, make a copy of it by calling DeepClone().
                            return(a.DeepClone(device, false));
                        }
                    }
                }
            }

            // Allocating in GPU memory inevitably causes copying.
            return(new NDArrayView(shape, data, device, false));
        }
Ejemplo n.º 10
0
        private Function Conv(int[] convMapSize, Variable x, DataType dType, DeviceDescriptor device, bool usePadding, bool useBias, string name, uint seed)
        {
            var shape = CNTK.NDShape.CreateNDShape(convMapSize);
            var W     = Weights(shape, dType, device, seed);

            //
            var strides  = new int[] { 1 };
            var sharing  = new bool[] { true };
            var aPadding = new bool[] { usePadding };
            //
            var result = CNTKLib.Convolution(W, x, strides, sharing, aPadding);

            if (useBias)
            {
                var intArray = convMapSize.Length == 4 ?
                               new int[] { 1, 1, NDShape.InferredDimension } :
                new int[] { 1, };

                var bShape = NDShape.CreateNDShape(intArray);

                var b = Bias(bShape, dType, device);

                result = CNTK.CNTKLib.Plus(result, b);
            }

            result = CNTK.CNTKLib.ReLU(result, name);

            return(result);
        }
Ejemplo n.º 11
0
        public List <int> Recognize(string Path)
        {
            var outputDataMap = new Dictionary <Variable, Value>()
            {
                { Classifier.Output, null }
            };
            Variable     inputVar     = Classifier.Arguments.Single();
            NDShape      inputShape   = inputVar.Shape;
            int          imageWidth   = inputShape[0];
            int          imageHeight  = inputShape[1];
            string       sampleImage  = Path;
            Bitmap       bmp          = new Bitmap(Bitmap.FromFile(sampleImage));
            var          resized      = bmp.Resize(imageWidth, imageHeight, true);
            List <float> resizedCHW   = resized.ParallelExtractCHW();
            var          inputDataMap = new Dictionary <Variable, Value>();
            var          inputVal     = Value.CreateBatch(inputShape, resizedCHW, device);

            inputDataMap.Add(inputVar, inputVal);



            Classifier.Evaluate(inputDataMap, outputDataMap, device);
            var outputData = outputDataMap[Classifier.Output].GetDenseData <float>(Classifier.Output);

            return(outputData.Select(l => l.IndexOf(l.Max())).ToList());
        }
Ejemplo n.º 12
0
 // Create Value object from dense input as batch of sequences data.
 public static Value CreateBatchOfSequences <T>(NDShape sampleShape,
                                                System.Collections.Generic.IEnumerable <System.Collections.Generic.IEnumerable <T> > batchOfSequences,
                                                DeviceDescriptor device,
                                                bool readOnly = false)
 {
     return(Create(sampleShape, batchOfSequences, new System.Collections.Generic.List <bool>(0), device, readOnly));
 }
Ejemplo n.º 13
0
 public NDArrayView(NDShape viewShape, int[] colStarts, int[] rowIndices, double[] nonZeroValues, uint numNonZeroValues, DeviceDescriptor device, bool readOnly) : this(CNTKLibPINVOKE.new_NDArrayView__SWIG_20(NDShape.getCPtr(viewShape), colStarts, rowIndices, nonZeroValues, numNonZeroValues, DeviceDescriptor.getCPtr(device), readOnly), true)
 {
     if (CNTKLibPINVOKE.SWIGPendingException.Pending)
     {
         throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 14
0
 public NDArrayView(NDShape viewShape, double[] dataBuffer, uint numBufferElements, DeviceDescriptor device) : this(CNTKLibPINVOKE.new_NDArrayView__SWIG_17(NDShape.getCPtr(viewShape), dataBuffer, numBufferElements, DeviceDescriptor.getCPtr(device)), true)
 {
     if (CNTKLibPINVOKE.SWIGPendingException.Pending)
     {
         throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 15
0
        private CNTK.Function _reshape_dummy_dim(CNTK.Function x, params int[] axis)
        {
            // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/cntk_backend.py#L680

            List <int> shape = In(x.Output.Shape).ToList();


            var _axis = axis.Select(i => i < 0 ? (i + shape.Count) : i).ToArray();

            if (shape.Count(s => s == NDShape.InferredDimension) > 1)
            {
                var result = x;
                foreach (int index in _axis.Sorted().Reverse())
                {
                    result = C.Reshape(result, replacementShape: NDShape.CreateNDShape(new int[] { }),
                                       beginAxis: new Axis(index), endAxis: new Axis(index + 1));
                }
                return(result);
            }
            else
            {
                foreach (int index in _axis.Sorted().Reverse())
                {
                    shape.RemoveAt(index);
                }

                return(C.Reshape(x, NDShape.CreateNDShape(shape)));
            }
        }
Ejemplo n.º 16
0
        // Todo: move it to separate unit tests.
        public void NDArrayViewTest(DeviceDescriptor device)
        {
            Console.WriteLine("Test creating NDArrayView on devices.");

            var data       = new float[10];
            var shape      = new NDShape(1, 10);
            var n1         = new NDArrayView(shape, data, device);
            var n1Clone    = n1.DeepClone(device);
            var n1CloneCPU = n1.DeepClone(DeviceDescriptor.CPUDevice);

            Console.WriteLine("n1: on " + n1.Device.AsString() + ", Storage:" + n1.StorageFormat + ", Shape:" + n1.Shape.AsString());
            Console.WriteLine("n1Clone: on " + n1Clone.Device.AsString() + ", Storage:" + n1Clone.StorageFormat + ", Shape:" + n1Clone.Shape.AsString());
            Console.WriteLine("n1CloneCPU: on " + n1CloneCPU.Device.AsString() + ", Storage:" + n1CloneCPU.StorageFormat + ", Shape:" + n1CloneCPU.Shape.AsString());

            int[] dimensions = { 4, 5 };
            var   shape2     = NDShape.CreateNDShape(dimensions);

            float[] nonZeroValues = { 1, 5, 4, 2, 3, 9, 7, 8, 6 };
            int[]   rowIndices    = { 0, 2, 0, 1, 1, 3, 2, 2, 3 };
            int[]   colStarts     = { 0, 2, 4, 6, 7, 9 };
            var     s1            = new NDArrayView(shape2, colStarts, rowIndices, nonZeroValues, device, true);
            var     s1Clone       = s1.DeepClone(device);
            var     s1DenseCPU    = new NDArrayView(DataType.Float, StorageFormat.Dense, shape2, DeviceDescriptor.CPUDevice);

            s1DenseCPU.CopyFrom(s1);

            Console.WriteLine("s1: on " + s1.Device.AsString() + ", Storage:" + s1.StorageFormat + ", Shape:" + s1.Shape.AsString());
            Console.WriteLine("s1Clone: on " + s1Clone.Device.AsString() + ", Storage:" + s1Clone.StorageFormat + ", Shape:" + s1Clone.Shape.AsString());
            Console.WriteLine("s1DenseCPU: on " + s1DenseCPU.Device.AsString() + ", Storage:" + s1DenseCPU.StorageFormat + ", Shape:" + s1DenseCPU.Shape.AsString());
        }
Ejemplo n.º 17
0
        /// <summary>
        /// create model by w,h,c,outputClassNum
        /// </summary>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <param name="c"></param>
        /// <param name="outputClassNum"></param>
        /// <param name="deviceName"></param>
        public FCN7(int w, int h, int c, int outputClassNum, string deviceName)
        {
            device = NP.CNTKHelper.GetDeviceByName(deviceName);
            //input output variable
            int[] inputDim  = new int[] { w, h, c };
            int[] outputDim = new int[] { outputClassNum };
            inputVariable  = Variable.InputVariable(NDShape.CreateNDShape(inputDim), DataType.Float, "inputVariable");
            outputVariable = Variable.InputVariable(NDShape.CreateNDShape(outputDim), DataType.Float, "labelVariable");
            //build model
            classifierOutput = CreateFullyChannelNetwork(inputVariable, c, outputClassNum);
            Function loss = CNTKLib.SquaredError(classifierOutput, outputVariable);
            Function pred = CNTKLib.ClassificationError(classifierOutput, outputVariable);
            //adam leaner
            ParameterVector parameterVector = new ParameterVector(classifierOutput.Parameters().ToList());
            TrainingParameterScheduleDouble learningRateSchedule = new TrainingParameterScheduleDouble(0.00178125, BatchSize);
            TrainingParameterScheduleDouble momentumRateSchedule = new TrainingParameterScheduleDouble(0.9, BatchSize);
            Learner leaner = CNTKLib.AdamLearner(parameterVector, learningRateSchedule, momentumRateSchedule, true);

            //构造leaner方法
            trainer = Trainer.CreateTrainer(classifierOutput, loss, pred, new List <Learner>()
            {
                leaner
            });
            //TrainingParameterScheduleDouble learningRatePerSample = new TrainingParameterScheduleDouble(0.00178125, BatchSize); //0.00178125
            //TrainingParameterScheduleDouble momentumTimeConstant = CNTKLib.MomentumAsTimeConstantSchedule(256);
            //IList<Learner> parameterLearners = new List<Learner>() { Learner.MomentumSGDLearner(classifierOutput.Parameters(), learningRatePerSample, momentumTimeConstant, true) };
            //trainer = Trainer.CreateTrainer(classifierOutput, loss, pred, parameterLearners);
        }
Ejemplo n.º 18
0
 // Create Value object from dense input as sequence data.
 public static Value CreateSequence <T>(NDShape sampleShape,
                                        System.Collections.Generic.IEnumerable <T> sequence,
                                        DeviceDescriptor device,
                                        bool readOnly = false)
 {
     return(CreateSequence <T>(sampleShape, sequence, true, device, readOnly));
 }
Ejemplo n.º 19
0
        public Tensor categorical_crossentropy(Tensor target, Tensor output, bool from_logits = false)
        {
            // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/cntk_backend.py#L1480

            var _output = In(output);
            var _target = In(target);

            if (from_logits)
            {
                var result = C.CrossEntropyWithSoftmax(_output, _target);
                // cntk's result shape is (batch, 1), while keras expect (batch, )
                CNTK.Function r = C.Reshape(result, NDShape.CreateNDShape(new int[] { }));
                return(Out(r));
            }
            else
            {
                // scale preds so that the class probas of each sample sum to 1
                var o     = C.ElementDivide(_output.function, C.ReduceSum(_output, Axis.EndStaticAxis()));
                var eps   = Constant.Scalar(epsilon(), DeviceDescriptor.CPUDevice);
                var omeps = Constant.Scalar(1.0 - epsilon(), DeviceDescriptor.CPUDevice);
                // avoid numerical instability with _EPSILON clipping
                o = C.Clip(o, eps, omeps);
                CNTK.Function r = C.Negate(C.ReduceSum(C.ElementTimes(_target, C.Log(_output)), Axis.EndStaticAxis()));
                return(Out(r));
            }
        }
Ejemplo n.º 20
0
 public NDArrayView(NDShape viewShape, int[] colStarts, int[] rowIndices, SWIGTYPE_p_int16_t nonZeroValues, uint numNonZeroValues, DeviceDescriptor device) : this(CNTKLibPINVOKE.new_NDArrayView__SWIG_25(NDShape.getCPtr(viewShape), colStarts, rowIndices, SWIGTYPE_p_int16_t.getCPtr(nonZeroValues), numNonZeroValues, DeviceDescriptor.getCPtr(device)), true)
 {
     if (CNTKLibPINVOKE.SWIGPendingException.Pending)
     {
         throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 21
0
 public NDMask(NDShape shape, DeviceDescriptor device) : this(CNTKLibPINVOKE.new_NDMask__SWIG_0(NDShape.getCPtr(shape), DeviceDescriptor.getCPtr(device)), true)
 {
     if (CNTKLibPINVOKE.SWIGPendingException.Pending)
     {
         throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 22
0
        /// <summary>
        /// The example shows
        /// - how to load model from a memory buffer.
        /// </summary>
        /// <param name="device">Specify on which device to run the evaluation.</param>
        public static void LoadModelFromMemory(DeviceDescriptor device)
        {
            try
            {
                Console.WriteLine("\n===== Load model from memory buffer =====");

                // For demo purpose, we first read the the model into memory
                // The model resnet20.dnn is trained by <CNTK>/Examples/Image/Classification/ResNet/Python/TrainResNet_CIFAR10.py
                // Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model.
                string modelFilePath = "resnet20.dnn";
                ThrowIfFileNotExist(modelFilePath, string.Format("Error: The model '{0}' does not exist. Please follow instructions in README.md in <CNTK>/Examples/Image/Classification/ResNet to create the model.", modelFilePath));
                var modelBuffer = File.ReadAllBytes(modelFilePath);

                // Load model from memroy buffer
                Function modelFunc = Function.Load(modelBuffer, device);

                // Get shape data for the input variable
                Variable inputVar    = modelFunc.Arguments.Single();
                NDShape  inputShape  = inputVar.Shape;
                int      imageWidth  = inputShape[0];
                int      imageHeight = inputShape[1];

                // Image preprocessing to match input requirements of the model.
                // This program uses images from the CIFAR-10 dataset for evaluation.
                // Please see README.md in <CNTK>/Examples/Image/DataSets/CIFAR-10 about how to download the CIFAR-10 dataset.
                string sampleImage = "00000.png";
                ThrowIfFileNotExist(sampleImage, string.Format("Error: The sample image '{0}' does not exist. Please see README.md in <CNTK>/Examples/Image/DataSets/CIFAR-10 about how to download the CIFAR-10 dataset.", sampleImage));
                Bitmap       bmp        = new Bitmap(Bitmap.FromFile(sampleImage));
                var          resized    = bmp.Resize(imageWidth, imageHeight, true);
                List <float> resizedCHW = resized.ParallelExtractCHW();

                // Create input data map.
                var inputDataMap = new Dictionary <Variable, Value>();
                var inputVal     = Value.CreateBatch(inputVar.Shape, resizedCHW, device);
                inputDataMap.Add(inputVar, inputVal);

                // Get output variable.
                Variable outputVar = modelFunc.Output;

                // Create output data map. Using null as Value to indicate using system allocated memory.
                // Alternatively, create a Value object and add it to the data map.
                var outputDataMap = new Dictionary <Variable, Value>();
                outputDataMap.Add(outputVar, null);

                // Start evaluation on the device.
                modelFunc.Evaluate(inputDataMap, outputDataMap, device);

                // Get evaluate result as dense output.
                var outputVal  = outputDataMap[outputVar];
                var outputData = outputVal.GetDenseData <float>(outputVar);

                PrintOutput(outputVar.Shape.TotalSize, outputData);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}\nCallStack: {1}\n Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception");
                throw ex;
            }
        }
Ejemplo n.º 23
0
 private void _MarkSequenceBegin(SizeTVector offset, NDShape sectionShape)
 {
     CNTKLibPINVOKE.NDMask__MarkSequenceBegin__SWIG_1(swigCPtr, SizeTVector.getCPtr(offset), NDShape.getCPtr(sectionShape));
     if (CNTKLibPINVOKE.SWIGPendingException.Pending)
     {
         throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 24
0
 private void _InvalidateSection(SizeTVector sectionOffset, NDShape sectionShape)
 {
     CNTKLibPINVOKE.NDMask__InvalidateSection(swigCPtr, SizeTVector.getCPtr(sectionOffset), NDShape.getCPtr(sectionShape));
     if (CNTKLibPINVOKE.SWIGPendingException.Pending)
     {
         throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 25
0
 Parameter Parameter(IEnumerable <int> dims)
 {
     return(new Parameter(NDShape.CreateNDShape(dims), DataType.Double,
                          CNTKLib.GlorotUniformInitializer( // valeur par défaut aléatoire
                              CNTKLib.DefaultParamInitScale,
                              CNTKLib.SentinelValueForInferParamInitRank,
                              CNTKLib.SentinelValueForInferParamInitRank, 1)));
 }
Ejemplo n.º 26
0
 // Create Value object from NDArrayViews with sequenceStartFlags
 public static Value Create(NDShape sampleShape,
                            System.Collections.Generic.IEnumerable <NDArrayView> sequences,
                            System.Collections.Generic.IEnumerable <bool> sequenceStartFlags,
                            DeviceDescriptor device,
                            bool readOnly = false)
 {
     return(Create(sampleShape, sequences, sequenceStartFlags, device, readOnly, /*createNewCopy = */ false));
 }
Ejemplo n.º 27
0
        /// <summary>
        /// Based on Dense from: https://github.com/Microsoft/CNTK/blob/master/bindings/python/cntk/layers/layers.py
        /// </summary>
        public static Function Dense(this Function input,
                                     int units,
                                     CNTKDictionary weightInitializer,
                                     CNTKDictionary biasInitializer,
                                     DeviceDescriptor device,
                                     DataType dataType,
                                     int inputRank = 0,
                                     int mapRank   = 0)
        {
            if (inputRank != 0 && mapRank != 0)
            {
                throw new ArgumentException("Dense: inputRank and mapRank cannot be specified at the same time.");
            }

            var outputShape = NDShape.CreateNDShape(new int[] { units });
            var outputRank  = outputShape.Dimensions.Count;

            var inputRanks = (inputRank != 0) ? inputRank : 1;
            var dimensions = Enumerable.Range(0, inputRanks)
                             .Select(v => NDShape.InferredDimension).ToArray(); // infer all dimensions.
            var inputShape = NDShape.CreateNDShape(dimensions);

            int inferInputRankToMap;

            if (inputRank != 0)
            {
                inferInputRankToMap = -1; // means map_rank is not specified; input_rank rules.
            }
            else if (mapRank == 0)
            {
                inferInputRankToMap = 0;  // neither given: default to 'infer W to use all input dims'.
            }
            else
            {
                inferInputRankToMap = mapRank;  // infer W to use all input dims except the first static 'map_rank' ones.
            }

            var weightsDimensions = outputShape.Dimensions.ToList();

            weightsDimensions.AddRange(inputShape.Dimensions);
            var weightsShape = NDShape.CreateNDShape(weightsDimensions);

            var weights = new Parameter(weightsShape, dataType, weightInitializer, device, "w");

            // Weights and input is in reversed order compared to the original python code.
            // Same goes for the dimensions. This is because the python api reverses the dimensions internally.
            // The python API was made in this way to be similar to other deep learning toolkits.
            // The C# and the C++ share the same column major layout.
            var r = CNTKLib.Times(weights, input, (uint)outputRank, inferInputRankToMap);

            if (biasInitializer != null)
            {
                var biasParameter = new Parameter(outputShape, dataType, biasInitializer, device, "b");
                r = r + biasParameter;
            }

            return(r);
        }
Ejemplo n.º 28
0
        public void cntk_partial_shape_test()
        {
            // Note: Keras/TensorFlow represent unknown dimensions
            // as None, whereas TensorFlowSharp represents as -1:

            /*
             *  import keras
             *
             *  from keras.models import Sequential
             *  from keras.layers import Dense
             *  from keras import backend as K
             *  import numpy as np
             *
             *  a = K.placeholder(shape = (None, 2))
             *  b = K.variable(np.matrix([[1, 2, 3], [4, 5, 6]]))
             *  ab = K.dot(a, b)
             *
             *  shape_a = K.int_shape(a)
             *  shape_b = K.int_shape(b)
             *  shape_ab = K.int_shape(ab)
             *
             *  print(shape_a)
             *  print(shape_b)
             *  print(shape_ab)
             *
             *  >>> Using TensorFlow backend.
             *  (None, 2)
             *  (2, 3)
             *  (None, 3)
             */

            using (var K = new CNTKBackend())
            {
                Tensor a = K.placeholder(shape: new int?[] { null, 2 });
                Tensor b = K.variable(array: new float[, ] {
                    { 1, 2, 3 },
                    { 4, 5, 6 }
                });

                var ab = K.dot(a, b);

                int?[] shape_a  = K.int_shape(a);
                int?[] shape_b  = K.int_shape(b);
                int?[] shape_ab = K.int_shape(ab);

                NDShape tf_shape_a  = K.In(a).CNTK_Shape;
                NDShape tf_shape_b  = K.In(b).CNTK_Shape;
                NDShape tf_shape_ab = K.In(ab).CNTK_Shape;

                AssertEx.AreEqual(new int?[] { null, 2 }, shape_a);
                AssertEx.AreEqual(new int?[] { 2, 3 }, shape_b);
                AssertEx.AreEqual(new int?[] { null, 3 }, shape_ab);

                Assert.AreEqual(NDShape.CreateNDShape(new[] { NDShape.FreeDimension, 2 }), tf_shape_a);
                Assert.AreEqual(NDShape.CreateNDShape(new[] { 2, 3 }), tf_shape_b);
                Assert.AreEqual(NDShape.CreateNDShape(new[] { NDShape.FreeDimension, 3 }), tf_shape_ab);
            }
        }
Ejemplo n.º 29
0
        internal static Function Conv(this Function input,
                                      int[] filterShape,
                                      int filterCount,
                                      int[] filterStride,
                                      Padding padding, // TODO: Consider if padding should be decided pr. dimension.
                                      CNTKDictionary weightInitializer,
                                      CNTKDictionary biasInitializer,
                                      DeviceDescriptor device,
                                      DataType dataType)
        {
            var weights = new Parameter(NDShape.CreateNDShape(filterShape), dataType,
                                        weightInitializer, device);

            var strideShape = NDShape.CreateNDShape(filterStride);

            // Currently, only sharing=true is supported by CNTK. So these are hardcoded.
            // sharing dimensions follows stride dimensions. 1D, 2D, 3D, etc.
            var sharing = CntkUtilities.CreateFilledBoolVector(filterStride.Length, true);

            // Padding dimensions follows stride dimensions. 1D, 2D, 3D, etc.
            var usePadding  = padding.ToBoolean();
            var autoPadding = CntkUtilities.CreateFilledBoolVector(filterStride.Length, usePadding);

            // TODO: Consider if we want to surface the additional options for Convolution:
            // - dilation
            // - reductionRank
            // - groups
            // - maxTempMemSizeInSamples

            // Default for dilation seems to be a shape of size (1) with value 1
            var dilation = NDShape.CreateNDShape(new[] { 1 });

            // Following are defaults extrapolated from CNTK code
            var reductionRank           = 1u;
            var groups                  = 1u;
            var maxTempMemSizeInSamples = 0u;
            var sequential              = false;

            var result = CNTKLib.Convolution(
                weights, input, strideShape, sharing, autoPadding,
                dilation, reductionRank, groups, maxTempMemSizeInSamples, sequential);

            if (biasInitializer != null)
            {
                // Bias dimensions should be defined for filter dimensions.
                // For instance for 2D case: (1, 1, filterChannels).
                var biasShape = filterStride.Select(s => 1).ToList();
                biasShape.Add(filterCount);

                var bias = new Parameter(NDShape.CreateNDShape(biasShape.ToArray()),
                                         dataType, biasInitializer, device);

                result = CNTKLib.Plus(result, bias);
            }

            return(result);
        }
Ejemplo n.º 30
0
        public static Value SafeCreate(NDShape shape, float[] data, DeviceDescriptor device)
        {
            if (device == null)
            {
                device = DeviceDescriptor.UseDefaultDevice();
            }

            return(new Value(new NDArrayView(shape, data, device, false)).DeepClone());
        }