Example #1
0
        protected override Delegate GetPredictionGetter(Row input, int colSrc)
        {
            Contracts.AssertValue(input);
            Contracts.Assert(0 <= colSrc && colSrc < input.Schema.Count);

            var typeSrc = input.Schema[colSrc].Type;

            Contracts.Assert(typeSrc.IsVector && typeSrc.ItemType == NumberType.Float);
            Contracts.Assert(ValueMapper == null ||
                             typeSrc.VectorSize == ValueMapper.InputType.VectorSize || ValueMapper.InputType.VectorSize == 0);
            Contracts.Assert(Utils.Size(_quantiles) > 0);

            var featureGetter = input.GetGetter <VBuffer <Float> >(colSrc);
            var featureCount  = ValueMapper != null ? ValueMapper.InputType.VectorSize : 0;

            var quantiles = new Float[_quantiles.Length];

            for (int i = 0; i < quantiles.Length; i++)
            {
                quantiles[i] = (Float)_quantiles[i];
            }
            var map = _qpred.GetMapper(quantiles);

            var features = default(VBuffer <Float>);
            ValueGetter <VBuffer <Float> > del =
                (ref VBuffer <Float> value) =>
            {
                featureGetter(ref features);
                Contracts.Check(features.Length == featureCount || featureCount == 0);
                map(in features, ref value);
            };
Example #2
0
 public ImplVecOrdered(IRow row, OneToOneTransformBase.ColInfo info, ColInfoEx ex, int invertHashMaxCount, Delegate dstGetter)
     : base(row, info, ex, invertHashMaxCount)
 {
     _srcGetter = Row.GetGetter <VBuffer <T> >(_info.Source);
     _dstGetter = dstGetter as ValueGetter <VBuffer <uint> >;
     Contracts.AssertValue(_dstGetter);
 }
                public State(IExceptionContext ectx, Row input, TreeEnsembleModelParameters ensemble, int numLeaves, int featureIndex)
                {
                    Contracts.AssertValue(ectx);
                    _ectx = ectx;
                    _ectx.AssertValue(input);
                    _ectx.AssertValue(ensemble);
                    _ectx.Assert(ensemble.TrainedEnsemble.NumTrees > 0);
                    _input     = input;
                    _ensemble  = ensemble;
                    _numTrees  = _ensemble.TrainedEnsemble.NumTrees;
                    _numLeaves = numLeaves;

                    _src           = default(VBuffer <float>);
                    _featureGetter = input.GetGetter <VBuffer <float> >(featureIndex);

                    _cachedPosition = -1;
                    _leafIds        = new int[_numTrees];
                    _pathIds        = new List <int> [_numTrees];
                    for (int i = 0; i < _numTrees; i++)
                    {
                        _pathIds[i] = new List <int>();
                    }

                    _cachedLeafBuilderPosition = -1;
                    _cachedPathBuilderPosition = -1;
                }
Example #4
0
        private static Func <bool> GetIsNewGroupDelegateCore <T>(Row cursor, int col)
        {
            var  getter  = cursor.GetGetter <T>(col);
            bool first   = true;
            T    old     = default(T);
            T    val     = default(T);
            var  compare = EqualityComparer <T> .Default;

            return(() =>
            {
                getter(ref val);
                if (first)
                {
                    first = false;
                    old = val;
                    return true;
                }
                if (compare.Equals(val, old))
                {
                    return false;
                }
                old = val;
                return true;
            });
        }
Example #5
0
 protected static ValueGetter <T> GetGetterFromRow <T>(Row output, int col)
 {
     Contracts.AssertValue(output);
     Contracts.Assert(0 <= col && col < output.Schema.ColumnCount);
     Contracts.Assert(output.IsColumnActive(col));
     return(output.GetGetter <T>(col));
 }
Example #6
0
            internal override void InitializeNextPass(Row row, RoleMappedSchema schema)
            {
                Contracts.Assert(PassNum < 1);
                Contracts.AssertValue(schema.Label);

                var score = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);

                _labelGetter = RowCursorUtils.GetVecGetterAs <Float>(NumberType.Float, row, schema.Label.Index);
                _scoreGetter = row.GetGetter <VBuffer <Float> >(score.Index);
                Contracts.AssertValue(_labelGetter);
                Contracts.AssertValue(_scoreGetter);

                if (schema.Weight != null)
                {
                    _weightGetter = row.GetGetter <Float>(schema.Weight.Index);
                }
            }
        protected ValueGetter <T> GetSrcGetter <T>(Row input, int iinfo)
        {
            Host.AssertValue(input);
            Host.Assert(0 <= iinfo && iinfo < Infos.Length);
            int src = Infos[iinfo].Source;

            Host.Assert(input.IsColumnActive(src));
            return(input.GetGetter <T>(src));
        }
        /// <summary>
        /// Fetches the value of the column by name, in the given row.
        /// Used by the evaluators to retrieve the metrics from the results IDataView.
        /// </summary>
        public static T Fetch <T>(IExceptionContext ectx, Row row, string name)
        {
            if (!row.Schema.TryGetColumnIndex(name, out int col))
            {
                throw ectx.Except($"Could not find column '{name}'");
            }
            T val = default;

            row.GetGetter <T>(col)(ref val);
            return(val);
        }
        public static ValueGetter <Single> GetLabelGetter(Row cursor, int labelIndex)
        {
            var type = cursor.Schema[labelIndex].Type;

            if (type == NumberType.R4)
            {
                return(cursor.GetGetter <Single>(labelIndex));
            }

            if (type == NumberType.R8)
            {
                var getSingleSrc = cursor.GetGetter <Double>(labelIndex);
                return
                    ((ref Single dst) =>
                {
                    Double src = Double.NaN;
                    getSingleSrc(ref src);
                    dst = Convert.ToSingle(src);
                });
            }

            return(GetLabelGetterNotFloat(cursor, labelIndex));
        }
            private ValueGetter <ReadOnlyMemory <char> > GetTextValueGetter <TSrc>(Row input, int colSrc, VBuffer <ReadOnlyMemory <char> > slotNames)
            {
                Contracts.AssertValue(input);
                Contracts.AssertValue(Predictor);

                var featureGetter = input.GetGetter <TSrc>(colSrc);
                var map           = Predictor.GetFeatureContributionMapper <TSrc, VBuffer <float> >(_topContributionsCount, _bottomContributionsCount, _normalize);

                var features      = default(TSrc);
                var contributions = default(VBuffer <float>);

                return
                    ((ref ReadOnlyMemory <char> dst) =>
                {
                    featureGetter(ref features);
                    map(in features, ref contributions);
                    var indices = new Span <int>();
                    var values = new Span <float>();
                    if (contributions.IsDense)
                    {
                        Utils.GetIdentityPermutation(contributions.Length).AsSpan().CopyTo(indices);
                    }
                    else
                    {
                        contributions.GetIndices().CopyTo(indices);
                    }
                    contributions.GetValues().CopyTo(values);
                    var count = values.Length;
                    var sb = new StringBuilder();
                    GenericSpanSortHelper <int> .Sort(indices, values, 0, count);

                    for (var i = 0; i < count; i++)
                    {
                        var val = values[i];
                        var ind = indices[i];
                        var name = GetSlotName(ind, slotNames);
                        sb.AppendFormat("{0}: {1}, ", name, val);
                    }

                    if (sb.Length > 0)
                    {
                        _env.Assert(sb.Length >= 2);
                        sb.Remove(sb.Length - 2, 2);
                    }

                    dst = new ReadOnlyMemory <char>(sb.ToString().ToCharArray());
                });
            }
        private static ValueGetter <StringBuilder> GetGetterAsStringBuilderCore <TSrc>(ColumnType typeSrc, Row row, int col)
        {
            Contracts.Assert(typeof(TSrc) == typeSrc.RawType);

            var getter = row.GetGetter <TSrc>(col);
            var conv   = Conversions.Instance.GetStringConversion <TSrc>(typeSrc);

            var src = default(TSrc);

            return
                ((ref StringBuilder dst) =>
            {
                getter(ref src);
                conv(in src, ref dst);
            });
        }
Example #12
0
        private ValueGetter <TDst> GetValueGetter <TSrc, TDst>(Row input, int colSrc)
        {
            Contracts.AssertValue(input);
            Contracts.Assert(ValueMapper != null);

            var featureGetter = input.GetGetter <TSrc>(colSrc);
            var map           = ValueMapper.GetMapper <TSrc, TDst>();
            var features      = default(TSrc);

            return
                ((ref TDst dst) =>
            {
                featureGetter(ref features);
                map(in features, ref dst);
            });
        }
        private static ValueGetter <Single> GetLabelGetterNotFloat(Row cursor, int labelIndex)
        {
            var type = cursor.Schema[labelIndex].Type;

            Contracts.Assert(type != NumberType.R4 && type != NumberType.R8);

            // boolean type label mapping: True -> 1, False -> 0.
            if (type.IsBool)
            {
                var getBoolSrc = cursor.GetGetter <bool>(labelIndex);
                return
                    ((ref Single dst) =>
                {
                    bool src = default;
                    getBoolSrc(ref src);
                    dst = Convert.ToSingle(src);
                });
            }

            Contracts.Check(type.IsKey, "Only floating point number, boolean, and key type values can be used as label.");
            Contracts.Assert(TestGetLabelGetter(type) == null);
            ulong keyMax = (ulong)type.KeyCount;

            if (keyMax == 0)
            {
                keyMax = ulong.MaxValue;
            }
            var getSrc = RowCursorUtils.GetGetterAs <ulong>(NumberType.U8, cursor, labelIndex);

            return
                ((ref Single dst) =>
            {
                ulong src = 0;
                getSrc(ref src);
                if (0 < src && src <= keyMax)
                {
                    dst = src - 1;
                }
                else
                {
                    dst = Single.NaN;
                }
            });
        }
            private ValueGetter <VBuffer <float> > GetValueGetter <TSrc>(Row input, int colSrc)
            {
                Contracts.AssertValue(input);
                Contracts.AssertValue(Predictor);

                var featureGetter = input.GetGetter <TSrc>(colSrc);

                // REVIEW: Scorer can do call to Sparicification\Norm routine.

                var map      = Predictor.GetFeatureContributionMapper <TSrc, VBuffer <float> >(_topContributionsCount, _bottomContributionsCount, _normalize);
                var features = default(TSrc);

                return
                    ((ref VBuffer <float> dst) =>
                {
                    featureGetter(ref features);
                    map(in features, ref dst);
                });
            }
Example #15
0
            public override ValueGetter <TValue> GetGetter <TValue>(int col)
            {
                bool isSrc;
                int  index = _parent.MapColumnIndex(out isSrc, col);

                if (isSrc)
                {
                    return(_input.GetGetter <TValue>(index));
                }

                Contracts.Assert(_getters[index] != null);
                var fn = _getters[index] as ValueGetter <TValue>;

                if (fn == null)
                {
                    throw Contracts.Except("Invalid TValue in GetGetter: '{0}'", typeof(TValue));
                }
                return(fn);
            }
Example #16
0
            private Delegate[] CreateGetters(Row input, bool[] active)
            {
                Contracts.Assert(Utils.Size(active) == 2);
                Contracts.Assert(_parent._distMapper != null);

                var getters = new Delegate[2];

                if (active[0] || active[1])
                {
                    // Put all captured locals at this scope.
                    var featureGetter = InputRoleMappedSchema.Feature != null?input.GetGetter <VBuffer <Float> >(InputRoleMappedSchema.Feature.Index) : null;

                    Float prob           = 0;
                    Float score          = 0;
                    long  cachedPosition = -1;
                    var   features       = default(VBuffer <Float>);
                    ValueMapper <VBuffer <Float>, Float, Float> mapper;

                    mapper = _parent._distMapper.GetMapper <VBuffer <Float>, Float, Float>();
                    if (active[0])
                    {
                        ValueGetter <Float> getScore =
                            (ref Float dst) =>
                        {
                            EnsureCachedResultValueMapper(mapper, ref cachedPosition, featureGetter, ref features, ref score, ref prob, input);
                            dst = score;
                        };
                        getters[0] = getScore;
                    }
                    if (active[1])
                    {
                        ValueGetter <Float> getProb =
                            (ref Float dst) =>
                        {
                            EnsureCachedResultValueMapper(mapper, ref cachedPosition, featureGetter, ref features, ref score, ref prob, input);
                            dst = prob;
                        };
                        getters[1] = getProb;
                    }
                }
                return(getters);
            }
Example #17
0
        protected override Delegate GetPredictedLabelGetter(Row output, out Delegate scoreGetter)
        {
            Host.AssertValue(output);
            Host.Assert(output.Schema == Bindings.RowMapper.OutputSchema);
            Host.Assert(output.IsColumnActive(Bindings.ScoreColumnIndex));

            ValueGetter <Float> mapperScoreGetter = output.GetGetter <Float>(Bindings.ScoreColumnIndex);

            long  cachedPosition = -1;
            Float score          = 0;

            ValueGetter <Float> scoreFn =
                (ref Float dst) =>
            {
                EnsureCachedPosition(ref cachedPosition, ref score, output, mapperScoreGetter);
                dst = score;
            };

            scoreGetter = scoreFn;

            if (Bindings.PredColType.IsKey)
            {
                ValueGetter <uint> predFnAsKey =
                    (ref uint dst) =>
                {
                    EnsureCachedPosition(ref cachedPosition, ref score, output, mapperScoreGetter);
                    GetPredictedLabelCoreAsKey(score, ref dst);
                };
                return(predFnAsKey);
            }

            ValueGetter <bool> predFn =
                (ref bool dst) =>
            {
                EnsureCachedPosition(ref cachedPosition, ref score, output, mapperScoreGetter);
                GetPredictedLabelCore(score, ref dst);
            };

            return(predFn);
        }
Example #18
0
        protected override Delegate GetPredictedLabelGetter(Row output, out Delegate scoreGetter)
        {
            Host.AssertValue(output);
            Host.Assert(output.Schema == Bindings.RowMapper.OutputSchema);
            Host.Assert(output.IsColumnActive(Bindings.ScoreColumnIndex));

            ValueGetter <VBuffer <Float> > mapperScoreGetter = output.GetGetter <VBuffer <Float> >(Bindings.ScoreColumnIndex);

            long            cachedPosition = -1;
            VBuffer <Float> score          = default(VBuffer <Float>);
            int             scoreLength    = Bindings.PredColType.KeyCount;

            ValueGetter <uint> predFn =
                (ref uint dst) =>
            {
                EnsureCachedPosition(ref cachedPosition, ref score, output, mapperScoreGetter);
                Host.Check(score.Length == scoreLength);
                int index = VectorUtils.ArgMax(in score);
                if (index < 0)
                {
                    dst = 0;
                }
                else
                {
                    dst = (uint)index + 1;
                }
            };
            ValueGetter <VBuffer <Float> > scoreFn =
                (ref VBuffer <Float> dst) =>
            {
                EnsureCachedPosition(ref cachedPosition, ref score, output, mapperScoreGetter);
                Host.Check(score.Length == scoreLength);
                score.CopyTo(ref dst);
            };

            scoreGetter = scoreFn;
            return(predFn);
        }
        private static ValueGetter <TDst> GetGetterAsCore <TSrc, TDst>(ColumnType typeSrc, ColumnType typeDst, Row row, int col)
        {
            Contracts.Assert(typeof(TSrc) == typeSrc.RawType);
            Contracts.Assert(typeof(TDst) == typeDst.RawType);

            var  getter = row.GetGetter <TSrc>(col);
            bool identity;
            var  conv = Conversions.Instance.GetStandardConversion <TSrc, TDst>(typeSrc, typeDst, out identity);

            if (identity)
            {
                Contracts.Assert(typeof(TSrc) == typeof(TDst));
                return((ValueGetter <TDst>)(Delegate) getter);
            }

            var src = default(TSrc);

            return
                ((ref TDst dst) =>
            {
                getter(ref src);
                conv(in src, ref dst);
            });
        }
 public override ValueGetter <TValue> GetGetter <TValue>()
 {
     return(_row.GetGetter <TValue>(_col));
 }
Example #21
0
 public override ValueGetter <TValue> GetGetter <TValue>(int col) => _row.GetGetter <TValue>(col);
Example #22
0
        private protected override Delegate[] CreateGettersCore(Row input, Func <int, bool> activeCols, out Action disposer)
        {
            Host.Assert(LabelIndex >= 0);
            Host.Assert(ScoreIndex >= 0);

            disposer = null;

            long cachedPosition = -1;
            var  label          = default(VBuffer <Float>);
            var  score          = default(VBuffer <Float>);

            ValueGetter <VBuffer <Float> > nullGetter = (ref VBuffer <Float> vec) => vec = default(VBuffer <Float>);
            var labelGetter = activeCols(LabelOutput) || activeCols(L1Output) || activeCols(L2Output) || activeCols(DistCol)
                ? RowCursorUtils.GetVecGetterAs <Float>(NumberType.Float, input, LabelIndex)
                : nullGetter;
            var scoreGetter = activeCols(ScoreOutput) || activeCols(L1Output) || activeCols(L2Output) || activeCols(DistCol)
                ? input.GetGetter <VBuffer <Float> >(ScoreIndex)
                : nullGetter;
            Action updateCacheIfNeeded =
                () =>
            {
                if (cachedPosition != input.Position)
                {
                    labelGetter(ref label);
                    scoreGetter(ref score);
                    cachedPosition = input.Position;
                }
            };

            var getters = new Delegate[5];

            if (activeCols(LabelOutput))
            {
                ValueGetter <VBuffer <Float> > labelFn =
                    (ref VBuffer <Float> dst) =>
                {
                    updateCacheIfNeeded();
                    label.CopyTo(ref dst);
                };
                getters[LabelOutput] = labelFn;
            }
            if (activeCols(ScoreOutput))
            {
                ValueGetter <VBuffer <Float> > scoreFn =
                    (ref VBuffer <Float> dst) =>
                {
                    updateCacheIfNeeded();
                    score.CopyTo(ref dst);
                };
                getters[ScoreOutput] = scoreFn;
            }
            if (activeCols(L1Output))
            {
                ValueGetter <double> l1Fn =
                    (ref double dst) =>
                {
                    updateCacheIfNeeded();
                    dst = VectorUtils.L1Distance(in label, in score);
                };
                getters[L1Output] = l1Fn;
            }
Example #23
0
 protected override ValueGetter <Single> GetScoreGetter(Row row)
 {
     return(row.GetGetter <Single>(_bindings.ScoreIndex));
 }
 private static Delegate GetGetterAsDelegateCore <TValue>(Row row, int col)
 {
     return(row.GetGetter <TValue>(col));
 }