示例#1
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.");
            }
        }