예제 #1
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.");
            }
        }
예제 #2
0
        // Create Value based on dense input
        // Todo: could this be a extension to Value class??
        // Todo: use Variable instead of varName. VarName as extension method
        // Todo: List can have maximal 2^31-1, enough? Otherwise need to go to array which supports 64bit size
        public Value CreateValue <T>(string varName, List <List <T> > sequences, DeviceDescriptor computeDevice)
        {
            var variable = getVariableByName(varName);
            var dim      = variable.Shape.TotalSize;

            if (typeof(T).Equals(typeof(float)))
            {
                var inputSeqVector = new FloatVectorVector();
                foreach (var seq in sequences)
                {
                    if (seq.Count() % dim != 0)
                    {
                        throw new InvalidDataException("the number of data in sequences does not match the input dimension");
                    }
                    var samples = new FloatVector(seq);
                    inputSeqVector.Add(samples);
                }
                var inputValue = Value.CreateDenseFloat(variable.Shape, inputSeqVector, computeDevice);
                return(inputValue);
            }
            else if (typeof(T).Equals(typeof(double)))
            {
                var inputSeqVector = new DoubleVectorVector();
                foreach (var seq in sequences)
                {
                    if (seq.Count() % dim != 0)
                    {
                        throw new InvalidDataException("the number of data in sequences does not match the input dimension");
                    }
                    var samples = new DoubleVector(seq);
                    inputSeqVector.Add(samples);
                }
                var inputValue = Value.CreateDenseDouble(variable.Shape, inputSeqVector, computeDevice);
                return(inputValue);
            }
            else
            {
                throw new InvalidDataException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
            }
        }