/// <summary> /// Create Value object from OneHotVector input as batch of sequences data with sequenceStratFlags, for 1D tensor only. /// </summary> /// <typeparam name="T">data type</typeparam> /// <param name="dimension">data dimension</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 <T>(int dimension, 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((uint)dimension, inputSeqVector, seqFlags, device, readOnly)); } else if (typeof(T).Equals(typeof(double))) { return(Value._CreateOneHotDouble((uint)dimension, 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."); } }
/// <summary> /// Return the data of the Value object as a list of sequences with variable length. /// This method returns an IList<IList<T>>. Each element of the outer list represents a sequence. /// Each sequence, represented by List<int>, contains a variable number of samples. /// Each sample is represented by an index of the OneHot vector. The size of the OneHot vector should match that defined in the variable. /// The number of samples = the count of elements in List<int>. /// </summary> /// <param name="outputVariable">the source variable</param> /// <returns></returns> public IList <IList <int> > GetOneHotData(Variable outputVariable) { var sequences = new List <IList <int> >(); var seqVec = new SizeTVectorVector(); _CopyVariableValueTo(outputVariable, seqVec); foreach (var seq in seqVec) { var seqList = new List <int>(seq.Count); foreach (var element in seq) { seqList.Add((int)element); } sequences.Add(seqList); } return(sequences); }
public void CopyVariableValueTo(Variable outputVariable, List <List <int> > sequences) { var seqVec = new SizeTVectorVector(); _CopyVariableValueTo(outputVariable, seqVec); sequences.Clear(); foreach (var seq in seqVec) { var seqList = new List <int>(seq.Count); foreach (var element in seq) { seqList.Add((int)element); } sequences.Add(seqList); } return; }
// // 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<uint>, contains a variable number of samples. // Each sample is represented by an index of the OneHot vector. The size of the OneHot vector should match that defined in the variable. // The number of samples = the count of elements in List<uint>. // public static void CopyVariableValueTo(this Value value, Variable sampleVariable, List <List <uint> > sequences) { if (sampleVariable.Shape[0] != sampleVariable.Shape.TotalSize) { throw new ArgumentException("The sample variable's leading axis dimensionality must equal to the total size of the shape for sparse data"); } var seqVec = new SizeTVectorVector(); value.CopyVariableValueTo(sampleVariable, seqVec); sequences.Clear(); foreach (var seq in seqVec) { sequences.Add(new List <uint>(seq)); } return; }