IDataTransform CreateTransformValueMapper <TSrc, TDst>(IValueMapper valueMapper, string inputColumn, string outputColumn) { var mapper = valueMapper.GetMapper <TSrc, TDst>(); IDataTransform transform; // The lambda transform should be replaced by something which does not creates // multiple threads. The mapper calls a predictor and for some reason, the context is not // well preserved (rare bug). var view = LambdaColumnHelper.Create(_host, "PredictTransform", Source, inputColumn, outputColumn, valueMapper.InputType, valueMapper.OutputType, (in TSrc src, ref TDst dst) => { mapper(in src, ref dst); });
private ValueGetter <TDst> GetValueGetter <TSrc, TDst>(DataViewRow input, int colSrc) { Contracts.AssertValue(input); Contracts.Assert(ValueMapper != null); var featureGetter = input.GetGetter <TSrc>(input.Schema[colSrc]); var map = ValueMapper.GetMapper <TSrc, TDst>(); var features = default(TSrc); return ((ref TDst dst) => { featureGetter(ref features); map(in features, ref dst); }); }
public virtual ValueMapper <VBuffer <float>, VBuffer <float> > GetMapper() { Contracts.Assert(_mappers != null); Contracts.Assert(_mappers.Length == 1); var mapper = _mappers[0].GetMapper <VBuffer <float>, float>(); VBuffer <float> inputs = new VBuffer <float>(); float[] labelClasses = _classes.Values.Select(c => _labelConverter(c)).ToArray(); int maxClass = _dstIndices == null ? _classes.Length : _dstIndices.Max() + 1; bool labelKey = _labelKey; IValueMapper imapperFinal = _reclassificationPredictor == null ? null : _reclassificationPredictor as IValueMapper; var mapperFinal = _reclassificationPredictor == null ? null : imapperFinal.GetMapper <VBuffer <float>, VBuffer <float> >(); var tempOut = new VBuffer <float>(); if (_singleColumn) { if (labelKey) { for (int i = 0; i < labelClasses.Length; ++i) { --labelClasses[i]; } } // Only one new column added which contain the label. return ((in VBuffer <float> src, ref VBuffer <float> dst) => { if (dst.Count != maxClass) { dst = new VBuffer <float>(maxClass, _classes.Length, new float[_classes.Length], _dstIndices); } var values = inputs.Values; var indices = inputs.Indices; if (src.IsDense) { if (values == null || values.Length <= src.Count) { values = new float[src.Length + 1]; } Array.Copy(src.Values, values, src.Length); Contracts.Assert(src.Indices == null); inputs = new VBuffer <float>(src.Length + 1, values); } else { if (src.Indices == null) { if (src.Values == null) { // All missing. for (int i = 0; i < _classes.Length; ++i) { dst.Values[i] = 0; } return; } else { throw Contracts.Except("Inconsistency in input vector. Sparse vector with null indices."); } } int nb = src.Count + 1; if (values == null || values.Length < nb) { values = new float[nb]; } if (indices == null || indices.Length < nb) { indices = new int[nb]; } Array.Copy(src.Values, values, src.Count); Array.Copy(src.Indices, indices, src.Count); indices[src.Count] = src.Length; inputs = new VBuffer <float>(src.Length + 1, src.Count + 1, values, indices); } for (int i = 0; i < _classes.Length; ++i) { inputs.Values[src.Count] = labelClasses[i]; mapper(in inputs, ref dst.Values[i]); } // Only if probabilities // Normalize(dst.Values, _classes.Length); #region debug #if (DEBUG) var dense = dst.DenseValues().ToArray(); if (dense.Length != maxClass) { throw Contracts.Except("Different dimension {0} != {1}-{2}", maxClass, dst.Length, dst.Count); } #endif #endregion if (mapperFinal != null) { mapperFinal(in dst, ref tempOut); dst = tempOut; } });