コード例 #1
0
ファイル: ValueShim.cs プロジェクト: zli12/CNTK
        /// <summary>
        /// Create Value object from sparse input as sequence data with sequenceStartFlag, for N-dimensional tensor. Only CreateSequence() for now.
        /// </summary>
        /// <typeparam name="T">data type of the created Value object.Currently, float and double are supported.</typeparam>
        /// <param name="sampleShape">the tensor shape. For sparse input, the tensor shape leading dimensionality must be the same as the total size of the tensor shape.</param>
        /// <param name="sequenceLength">the sequence length.</param>
        /// <param name="colStarts">column start indices</param>
        /// <param name="rowIndices">row indices</param>
        /// <param name="nonZeroValues">sparse values</param>
        /// <param name="sequenceStartFlag">true indicates that it is a new sequence.
        /// false means a continuation of a previous sequence.</param>
        /// <param name="device">device</param>
        /// <param name="readOnly">whether it a readonly value</param>
        /// <returns></returns>
        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 ArgumentException("The length of rowIndicies must be same as the length of nonZeroValues.");
            }
            if (colStarts.Length != sequenceLength + 1)
            {
                throw new 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 ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
            }
        }
コード例 #2
0
ファイル: ValueShim.cs プロジェクト: zli12/CNTK
 /// <summary>
 /// Create Value object from dense input as batch of sequences data.
 /// </summary>
 /// <typeparam name="T">data type</typeparam>
 /// <param name="sampleShape">data shape</param>
 /// <param name="batchOfSequences">the data to be stored in the Value.
 /// The outer vector represents a collection of sequences with variable length,
 /// and the inner vector represents each individual sequence.</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">whether it a readonly value</param>
 /// <returns></returns>
 public static Value CreateBatchOfSequences <T>(NDShape sampleShape,
                                                IEnumerable <IEnumerable <T> > batchOfSequences,
                                                DeviceDescriptor device,
                                                bool readOnly = false)
 {
     return(Create(sampleShape, batchOfSequences, new List <bool>(0), device, readOnly));
 }
コード例 #3
0
ファイル: ValueShim.cs プロジェクト: zli12/CNTK
        /// <summary>
        /// Create Value object from OneHotVector input, for N-dimenstional tensor. Only Create() method for now.
        /// </summary>
        /// <typeparam name="T">data type</typeparam>
        /// <param name="sampleShape">data shape</param>
        /// <param name="sequences">data sequence</param>
        /// <param name="sequenceStartFlag">true indicates that it is a new sequence.
        /// false means a continuation of a previous sequence.</param>
        /// <param name="device">device</param>
        /// <param name="readOnly">whether it a readonly value</param>
        /// <returns></returns>
        public static Value Create <T>(NDShape sampleShape,
                                       IEnumerable <IEnumerable <int> > sequences,
                                       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 ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
            }
        }
コード例 #4
0
ファイル: ValueShim.cs プロジェクト: zli12/CNTK
 /// <summary>
 /// Create Value object from NDArrayViews.
 /// </summary>
 /// <param name="sampleShape">data shape</param>
 /// <param name="sequences">data sequence</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">whether it a readonly value</param>
 /// <returns></returns>
 public static Value Create(NDShape sampleShape,
                            IEnumerable <NDArrayView> sequences,
                            DeviceDescriptor device,
                            bool readOnly = false)
 {
     return(Create(sampleShape, sequences, new List <bool>(0), device, readOnly));
 }
コード例 #5
0
ファイル: ValueShim.cs プロジェクト: zli12/CNTK
 /// <summary>
 /// Create Value object from sparse input as sequence data, for N-dimensional tensor. Only CreateSequence() for now.
 /// </summary>
 /// <typeparam name="T">data type</typeparam>
 /// <param name="sampleShape">the tensor shape. For sparse input, the tensor shape leading dimensionality must be the same as the total size of the tensor shape.</param>
 /// <param name="sequenceLength">the sequence length.</param>
 /// <param name="colStarts">column start indices</param>
 /// <param name="rowIndices">row indices</param>
 /// <param name="nonZeroValues">sparse values</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">whether it a readonly value</param>
 /// <returns></returns>
 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));
 }
コード例 #6
0
ファイル: ValueShim.cs プロジェクト: zli12/CNTK
 /// <summary>
 /// Create Value object from dense input as sequence data.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="sampleShape">the shape fo the value</param>
 /// <param name="sequence">daat sequence</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">whether it is a readonly value</param>
 /// <returns></returns>
 public static Value CreateSequence <T>(NDShape sampleShape,
                                        IEnumerable <T> sequence,
                                        DeviceDescriptor device,
                                        bool readOnly = false)
 {
     return(CreateSequence <T>(sampleShape, sequence, true, device, readOnly));
 }
コード例 #7
0
ファイル: ValueShim.cs プロジェクト: zli12/CNTK
        /// <summary>
        /// Create Value object from dense input as batch of sequences data with sequenceStartFlags.
        /// </summary>
        /// <typeparam name="T">data type</typeparam>
        /// <param name="sampleShape">data shape</param>
        /// <param name="sequences">data sequence</param>
        /// <param name="sequenceStartFlag">true indicates that it is a new sequence.
        /// false means a continuation of a previous sequence.</param>
        /// <param name="device">device</param>
        /// <param name="readOnly">whether it a readonly value</param>
        /// <returns></returns>
        public static Value Create <T>(NDShape sampleShape,
                                       IEnumerable <IEnumerable <T> > sequences,
                                       IEnumerable <bool> sequenceStartFlags,
                                       DeviceDescriptor device,
                                       bool readOnly = false)
        {
            var seqFlags = Helper.AsBoolVector(sequenceStartFlags);

            if (typeof(T).Equals(typeof(float)))
            {
                var inputAsSequencesVector = new FloatVectorVector();
                foreach (var seq in sequences)
                {
                    var seqVector = Helper.AsFloatVector(seq);
                    // The seqVector is copied when adding to inputAsSequencesVector.
                    inputAsSequencesVector.Add(seqVector);
                }
                return(Value._CreateDenseFloat(sampleShape, inputAsSequencesVector, seqFlags, device, readOnly));
            }
            else if (typeof(T).Equals(typeof(double)))
            {
                var inputAsSequencesVector = new DoubleVectorVector();
                foreach (var seq in sequences)
                {
                    var seqVector = Helper.AsDoubleVector(seq);
                    inputAsSequencesVector.Add(seqVector);
                }
                return(Value._CreateDenseDouble(sampleShape, inputAsSequencesVector, seqFlags, device, readOnly));
            }
            else
            {
                throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
            }
        }
コード例 #8
0
ファイル: ValueShim.cs プロジェクト: zli12/CNTK
 /// <summary>
 /// Create Value object from NDArrayViews with sequenceStartFlags
 /// </summary>
 /// <param name="sampleShape">data shape</param>
 /// <param name="sequences">data sequences</param>
 /// <param name="sequenceStartFlag">true indicates that it is a new sequence.
 /// false means a continuation of a previous sequence.</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">whether it a readonly value</param>
 /// <returns></returns>
 public static Value Create(NDShape sampleShape,
                            IEnumerable <NDArrayView> sequences,
                            IEnumerable <bool> sequenceStartFlags,
                            DeviceDescriptor device,
                            bool readOnly = false)
 {
     return(Create(sampleShape, sequences, sequenceStartFlags, device, readOnly, /*createNewCopy = */ false));
 }
        /// <summary>
        /// Create an 'Input' Variable denoting sparse data and specify if gradients are to be computed for this input
        /// </summary>
        /// <param name="shape"></param>
        /// <param name="isSparse"></param>
        /// <param name="dataType"></param>
        /// <param name="needsGradient"></param>
        /// <param name="name"></param>
        /// <param name="dynamicAxes"></param>
        /// <returns></returns>
        public static Variable InputVariable(NDShape shape, DataType dataType, string name = "", IList <Axis> dynamicAxes = null, bool isSparse = false, bool needsGradient = false)
        {
            if (dynamicAxes == null)
            {
                dynamicAxes = Axis.DefaultInputVariableDynamicAxes();
            }
            AxisVector dynamicAxesVector = Helper.AsAxisVector(dynamicAxes);

            return(CNTKLib.InputVariable(shape, isSparse, dataType, needsGradient, name, dynamicAxesVector));
        }
コード例 #10
0
        //
        // Copy the data of the Value object into the buffer provided by 'sequences'.
        // The 'sequences' is a list of sequences with variable length.
        // The number of items contained in the outer list of 'sequences' is the number of sequences in the Value object.
        // Each element of the outer list represents a sequence.
        // Each sequence, represented by List<T>, contains a variable number of samples.
        // Each sample consits of a fixed number of elements with type of 'T'. The number of elements is determined by the sample shape.
        // The number of samples = the count of elements in List<T> / the count of elements of the sample
        // The sampleShape should match the shape of the Value object.
        //
        public static void CopyTo <T>(this Value value, NDShape sampelShape, List <List <T> > sequences)
        {
            if ((value.GetDataType() == DataType.Float) && (!typeof(T).Equals(typeof(float))) ||
                (value.GetDataType() == DataType.Double) && (!typeof(T).Equals(typeof(double))))
            {
                throw new ArgumentException("The value type does not match the list type.");
            }

            throw new Exception("Not implemented yet.");
        }
        /// <summary>
        /// Value Equality.
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool Equals(NDShape p)
        {
            // If parameter is null return false:
            if ((object)p == null)
            {
                return(false);
            }

            // Return true if the fields match:
            return(CNTKLib.AreEqual(this, p));
        }
 /// <summary>
 /// Constructor using double sparse input.
 /// </summary>
 /// <param name="viewShape">shape of the data</param>
 /// <param name="colStarts">starting column</param>
 /// <param name="rowIndices">list of row indices</param>
 /// <param name="nonZeroValues">sparse data</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">whether the data is readonly</param>
 public NDArrayView(NDShape viewShape, int[] colStarts, int[] rowIndices, double[] nonZeroValues, DeviceDescriptor device, bool readOnly = false) : this(viewShape, colStarts, rowIndices, nonZeroValues, (uint)nonZeroValues.Length, device, readOnly)
 {
     if (rowIndices.Length != nonZeroValues.Length)
     {
         throw new ArgumentException("The length of rowIndicies must be same as the length of nonZeroValues.");
     }
     if (viewShape[viewShape.Rank - 1] + 1 != colStarts.Length)
     {
         throw new ArgumentException("The length of colStarts does not match the number of rows, i.e. the dimension size of the last rank of viewShape.");
     }
 }
 /// <summary>
 /// Create batch data as a Value object from a dense source buffer.
 /// Batch data of the specified dataSize are copied from the source buffer
 /// starting at the specified starting position
 /// </summary>
 /// <typeparam name="T">float or double</typeparam>
 /// <param name="sampleShape">shape of the Value</param>
 /// <param name="dataBuffer">source data buffer</param>
 /// <param name="dataStart">index to the source data buffer where batch data start</param>
 /// <param name="dataSize">size of the data</param>
 /// <param name="device">device where data are allocated</param>
 /// <param name="readOnly">whether data are mutable</param>
 /// <returns>the batch data value</returns>
 public static Value CreateBatch <T>(NDShape sampleShape, T[] dataBuffer, int dataStart, int dataSize,
                                     DeviceDescriptor device, bool readOnly = false)
 {
     if (typeof(T).Equals(typeof(float)))
     {
         return(Value._CreateBatchFloat(sampleShape, dataBuffer as float[], dataStart, dataSize, device, readOnly));
     }
     else if (typeof(T).Equals(typeof(double)))
     {
         return(Value._CreateBatchDouble(sampleShape, dataBuffer as double[], dataStart, dataSize, device, readOnly));
     }
     else
     {
         throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
     }
 }
コード例 #14
0
ファイル: ValueShim.cs プロジェクト: zli12/CNTK
        /// <summary>
        /// Create Value object from NDArrayViews with sequenceStartFlags
        /// </summary>
        /// <param name="sampleShape">data shape</param>
        /// <param name="sequences">data sequence</param>
        /// <param name="sequenceStartFlag">true indicates that it is a new sequence.
        /// false means a continuation of a previous sequence.</param>
        /// <param name="device">device</param>
        /// <param name="readOnly">whether it a readonly value</param>
        /// <param name="createNewCopy"></param>
        /// <returns></returns>
        public static Value Create(NDShape sampleShape,
                                   IEnumerable <NDArrayView> sequences,
                                   IEnumerable <bool> sequenceStartFlags,
                                   DeviceDescriptor device,
                                   bool readOnly,
                                   bool createNewCopy)
        {
            var seqVector = new NDArrayViewPtrVector();

            foreach (var element in sequences)
            {
                seqVector.Add(element);
            }
            var startFlags = Helper.AsBoolVector(sequenceStartFlags);

            return(_Create(sampleShape, seqVector, startFlags, device, readOnly, createNewCopy));
        }
コード例 #15
0
ファイル: ValueShim.cs プロジェクト: zli12/CNTK
 /// <summary>
 /// Create Value object from dense input as batch data.
 /// </summary>
 /// <typeparam name="T">float or double</typeparam>
 /// <param name="sampleShape">shape of the Value</param>
 /// <param name="batch">batch of data</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">readonly value</param>
 /// <returns>the value</returns>
 public static Value CreateBatch <T>(NDShape sampleShape, IEnumerable <T> batch, DeviceDescriptor device, bool readOnly = false)
 {
     if (typeof(T).Equals(typeof(float)))
     {
         var inputVector = Helper.AsFloatVector(batch);
         return(Value._CreateBatchFloat(sampleShape, inputVector, device, readOnly));
     }
     else if (typeof(T).Equals(typeof(double)))
     {
         var inputVector = Helper.AsDoubleVector(batch);
         return(Value._CreateBatchDouble(sampleShape, inputVector, device, readOnly));
     }
     else
     {
         throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
     }
 }
 /// <summary>
 /// Generate a uniform distribution random data NDArrayView
 /// </summary>
 /// <typeparam name="T">the data type</typeparam>
 /// <param name="shape">shape of the data</param>
 /// <param name="rangeBegin">low end value</param>
 /// <param name="rangeEnd">high end value</param>
 /// <param name="seed">seed</param>
 /// <param name="device">device</param>
 /// <returns></returns>
 public static NDArrayView RandomUniform <T>(NDShape shape, double rangeBegin, double rangeEnd, uint seed, DeviceDescriptor device)
 {
     if (device == null)
     {
         device = DeviceDescriptor.UseDefaultDevice();
     }
     if (typeof(T).Equals(typeof(float)))
     {
         return(_RandomUniformFloat(shape, rangeBegin, rangeEnd, seed, device));
     }
     else if (typeof(T).Equals(typeof(double)))
     {
         return(_RandomUniformDouble(shape, rangeBegin, rangeEnd, seed, device));
     }
     else
     {
         throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
     }
 }
        /// <summary>
        /// Value equality.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(Object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return(false);
            }

            // If parameter cannot be cast to Point return false.
            NDShape p = obj as NDShape;

            if ((Object)p == null)
            {
                return(false);
            }

            // Return true if the fields match:
            return(CNTKLib.AreEqual(this, p));
        }
 /// <summary>
 /// Create Value object from dense input as sequence data with sequenceStartFlag.
 /// </summary>
 /// <typeparam name="T">data type</typeparam>
 /// <param name="sampleShape">data shape</param>
 /// <param name="sequence">data sequence</param>
 /// <param name="sequenceStartFlag">true indicates that it is a new sequence. false means a continuation of a previous sequence.</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">whether it a readonly value</param>
 /// <returns></returns>
 public static Value CreateSequence <T>(NDShape sampleShape,
                                        IEnumerable <T> sequence,
                                        bool sequenceStartFlag,
                                        DeviceDescriptor device,
                                        bool readOnly = false)
 {
     if (typeof(T).Equals(typeof(float)))
     {
         float[] sequenceBuffer = sequence.ToArray() as float[];
         return(Value._CreateSequenceFloat(sampleShape, sequenceBuffer, sequenceBuffer.Length, sequenceStartFlag, device, readOnly));
     }
     else if (typeof(T).Equals(typeof(double)))
     {
         double[] sequenceBuffer = sequence.ToArray() as double[];
         return(Value._CreateSequenceDouble(sampleShape, sequenceBuffer, sequenceBuffer.Length, sequenceStartFlag, device, readOnly));
     }
     else
     {
         throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
     }
 }
        /// <summary>
        /// Invidates a section of a NDShape.
        /// </summary>
        /// <param name="sectionOffset"></param>
        /// <param name="sectionShape"></param>
        public void InvalidateSection(IEnumerable <int> sectionOffset, NDShape sectionShape)
        {
            var offsetVector = Helper.AsSizeTVector(sectionOffset);

            _InvalidateSection(offsetVector, sectionShape);
        }
 /// <summary>
 /// Constructor using double dense input.
 /// </summary>
 /// <param name="viewShape">shape of the data</param>
 /// <param name="dataBuffer">data buffer</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">whether the data is readonly</param>
 public NDArrayView(NDShape viewShape, double[] dataBuffer, DeviceDescriptor device, bool readOnly = false) : this(viewShape, dataBuffer, (uint)dataBuffer.Length, device, readOnly)
 {
 }
コード例 #21
0
 /// <summary>
 /// create a constant
 /// </summary>
 /// <param name="shape">shape of the constant</param>
 /// <param name="initValue">initial value</param>
 /// <param name="device">device</param>
 /// <param name="name">name</param>
 public Constant(NDShape shape, float initValue, DeviceDescriptor device, string name = "") :
     this(shape, DataType.Float, initValue, device, name)
 {
 }
        /// <summary>
        /// create a pooling function
        /// </summary>
        /// <param name="operand">input</param>
        /// <param name="poolingType">pooling type</param>
        /// <param name="poolingWindowShape">pooiling window dimensions</param>
        /// <param name="strides">strides to apply the pooling</param>
        /// <param name="autoPadding"></param>
        /// <returns></returns>
        public static Function Pooling(Variable operand, PoolingType poolingType, NDShape poolingWindowShape, NDShape strides, IEnumerable <bool> autoPadding)
        {
            BoolVector autoPaddingVector = Helper.AsBoolVector(autoPadding);

            return(Pooling(operand, poolingType, poolingWindowShape, strides, autoPaddingVector));
        }
        /// <summary>
        /// build a convolution function
        /// </summary>
        /// <param name="convolutionMap">convolution parameters (shape, type of the kernal)</param>
        /// <param name="operand">input variable</param>
        /// <param name="strides">strides to apply convolution</param>
        /// <param name="sharing">whether to share parameters (default = true)</param>
        /// <param name="autoPadding"></param>
        /// <returns></returns>
        public static Function Convolution(Variable convolutionMap, Variable operand, NDShape strides, IEnumerable <bool> sharing, IEnumerable <bool> autoPadding)
        {
            BoolVector sharingVec     = Helper.AsBoolVector(sharing);
            BoolVector autoPaddingVec = Helper.AsBoolVector(autoPadding);

            return(CNTKLib.Convolution(convolutionMap, operand, strides, sharingVec, autoPaddingVec));
        }
        /// <summary>
        /// Create a Placeholder variable to be used as a temporary/placeholder input to a Function.
        /// All placeholder inputs of a Function must be replaced with non-placeholder Variables before Forward evaluation of the Function.
        /// </summary>
        /// <param name="shape"></param>
        /// <param name="dynamicAxes"></param>
        /// <returns></returns>
        public static Variable PlaceholderVariable(NDShape shape, IList <Axis> dynamicAxes)
        {
            AxisVector dynamicAxesVector = Helper.AsAxisVector(dynamicAxes);

            return(CNTKLib.PlaceholderVariable(shape, dynamicAxesVector));
        }
 /// <summary>
 /// Create Value object from dense input as batch data.
 /// </summary>
 /// <typeparam name="T">float or double</typeparam>
 /// <param name="sampleShape">shape of the Value</param>
 /// <param name="batch">batch of data</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">readonly value</param>
 /// <returns>the value</returns>
 public static Value CreateBatch <T>(NDShape sampleShape, IEnumerable <T> batch, DeviceDescriptor device, bool readOnly = false)
 {
     T[] batchAsArray = batch.ToArray();
     return(CreateBatch <T>(sampleShape, batchAsArray, 0, batchAsArray.Count(), device, readOnly));
 }
        /// <summary>
        /// Marks sequence begins in a NDShape.
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="sectionShape"></param>
        public void MarkSequenceBegin(IEnumerable <int> offset, NDShape sectionShape)
        {
            var offsetVector = Helper.AsSizeTVector(offset);

            _MarkSequenceBegin(offsetVector, sectionShape);
        }