/// <summary> /// Converts a dataset to a set of training examples in 2D for use in CNTK. /// </summary> /// <typeparam name="T">Supported by<seealso cref="float"/>, <seealso cref="double"/></typeparam> /// <param name="features">2D Feature Set</param> /// <param name="labels">Set of labels. The dimension of the labels should be the same.</param> /// <param name="minibatchSize">Minipack size</param> /// <returns></returns> public IEnumerable <Minibatch> ConvertDatasetToMinibatch <T>(IEnumerable <T[, ]> features, IEnumerable <T[]> labels, int minibatchSize) where T : IConvertible { var combined = features.Zip(labels, (f, l) => (f, l)); foreach (var segment in GetSegments(combined, minibatchSize)) { var featuresData = segment.SelectMany(p => MatrixToVector(p.f)); var labelsData = segment.SelectMany(p => p.l); Minibatch minibatch = new Minibatch(); minibatch.Size = segment.Count; minibatch.Features = Value.CreateBatch(new int[] { GetRowsCount(segment[0].f), GetColumnsCount(segment[0].f), 1 }, featuresData, Device); minibatch.Labels = Value.CreateBatch(new int[] { segment[0].l.Length }, labelsData, Device); yield return(minibatch); } }
/// <summary> /// Converts a dataset into sets of training examples for use in CNTK. /// </summary> /// <typeparam name="T">Supported by<seealso cref="float"/>, <seealso cref="double"/></typeparam> /// <param name="dataset">Dataset. Each example should contain signs at the beginning of the array with dimensions of inputDim, and at the end of class labels with dimensions of outputDim. /// For example, inputDim = 3, outputDim = 2: [f1, f2, f3, l1, l2]</param> /// <param name="minibatchSize">Minipack size</param> /// <param name="inputDim">Dimension of signs (capacity)</param> /// <returns></returns> public IEnumerable <Minibatch> ConvertDatasetToMinibatch <T>(IEnumerable <T[]> dataset, int inputDim, int minibatchSize) where T : IConvertible { var outputDim = (dataset.FirstOrDefault()?.Length ?? 0) - inputDim; foreach (var minibatchData in GetSegments(dataset, minibatchSize)) { var features = minibatchData.SelectMany(p => p.Take(inputDim)).ToArray(); var labels = minibatchData.SelectMany(p => p.Skip(inputDim)).ToArray(); Minibatch minibatch = new Minibatch(); minibatch.Size = minibatchData.Count; minibatch.Features = Value.CreateBatch(new int[] { inputDim }, features, Device); minibatch.Labels = Value.CreateBatch(new int[] { outputDim }, labels, Device); yield return(minibatch); } }
/// <summary> /// Converts a dataset into sets of training examples for use in recursive networks. /// </summary> /// <typeparam name="T">Supported by<seealso cref="float"/>, <seealso cref="double"/></typeparam> /// <param name="features">A set of sequences (traits). Each sequence can be of variable length, but of the same dimension (the arrays of which the sequence consists must have the same length)</param> /// <param name="labels">Set of labels. The dimension of the labels should be the same.</param> /// <param name="minibatchSize">Minipack size</param> /// <returns></returns> public IEnumerable <Minibatch> ConvertDatasetToMinibatch <T>(IEnumerable <IList <T[]> > features, IEnumerable <T[]> labels, int minibatchSize) where T : IConvertible { var inputDimension = features.FirstOrDefault()?[0].Length ?? 0; var outputDimension = labels.FirstOrDefault()?.Length ?? 0; var combined = features.Zip(labels, (f, l) => (f, l)); foreach (var batchData in GetSegments(combined, minibatchSize)) { //{}-miniBatch, ()-sequence, []-features. //{ ([inputDim], [inputDim], [inputDim]), ([inputDim], [inputDim]) } => { [inputDim * 3], [inputDim * 2] } var featuresTransformed = batchData.Select(p => p.f.SelectMany(q => q)); //{ [outputDim], [outputDim] } => { outputDim * 2 } var labelTransformed = batchData.SelectMany(p => p.l); Minibatch minibatch = new Minibatch(); minibatch.Features = Value.CreateBatchOfSequences(new[] { inputDimension }, featuresTransformed, Device); minibatch.Labels = Value.CreateBatch(new[] { outputDimension }, labelTransformed, Device); minibatch.Size = batchData.Count; yield return(minibatch); } }