/// <summary> /// Create Value object from sparse input as sequence data with sequenceStartFlag, for 1D tensor. Only CreateSequence() for now. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dimension"></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>(int dimension, 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((uint)dimension, (uint)sequenceLength, colStarts, rowIndices, nonZeroValues as float[], numNonZeroValues, sequenceStartFlag, device, readOnly)); } else if (typeof(T).Equals(typeof(double))) { return(Value._CreateSequenceDouble((uint)dimension, (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."); } }
/// <summary> /// Create Value object from OneHotVector input as sequence data with sequenceStartFlag, for 1D tensor only. /// </summary> /// <typeparam name="T">data type</typeparam> /// <param name="dimension">data dimension</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>(int dimension, IEnumerable <int> sequence, bool sequenceStartFlag, DeviceDescriptor device, bool readOnly = false) { var inputVector = Helper.AsSizeTVector(sequence); if (typeof(T).Equals(typeof(float))) { return(Value._CreateSequenceFloat((uint)dimension, inputVector, sequenceStartFlag, device, readOnly)); } else if (typeof(T).Equals(typeof(double))) { return(Value._CreateSequenceDouble((uint)dimension, inputVector, 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> /// 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."); } }