Beispiel #1
0
        protected void DebugChecking2(RoleMappedData td, ITrainer trainer)
        {
            var scorer = PredictorHelper.CreateDefaultScorer(Host, td, trainer.CreatePredictor());

            if (trainer.PredictionKind == PredictionKind.Ranking)
            {
                string schemas = SchemaHelper.ToString(scorer.Schema);
                if (!schemas.Contains("Score"))
                {
                    throw Host.Except("Issue with the schema: {0}", schemas);
                }
            }

            using (var cursor = scorer.GetRowCursor(i => true))
            {
                int ilab, ipred, ifeat;
                cursor.Schema.TryGetColumnIndex(td.Schema.Label.Name, out ilab);
                if (trainer.PredictionKind == PredictionKind.Ranking)
                {
                    cursor.Schema.TryGetColumnIndex("Score", out ipred);
                    cursor.Schema.TryGetColumnIndex(td.Schema.Feature.Name, out ifeat);
                    var getter  = cursor.GetGetter <uint>(ilab);
                    var fgetter = cursor.GetGetter <VBuffer <float> >(ifeat);
                    var pgetter = cursor.GetGetter <float>(ipred);
                    if (pgetter == null)
                    {
                        throw Host.Except("Issue with the schema: {0}", SchemaHelper.ToString(cursor.Schema));
                    }
                    uint            lab         = 0;
                    var             counts      = new Dictionary <uint, int>();
                    var             counts_pred = new Dictionary <float, int>();
                    float           pre         = 0;
                    VBuffer <float> features    = default(VBuffer <float>);
                    int             nbrows      = 0;
                    int             err         = 0;
                    while (cursor.MoveNext())
                    {
                        getter(ref lab);
                        pgetter(ref pre);
                        fgetter(ref features);
                        counts[lab]      = counts.ContainsKey(lab) ? counts[lab] + 1 : 0;
                        counts_pred[pre] = counts_pred.ContainsKey(pre) ? counts_pred[pre] + 1 : 0;
                        if (trainer.PredictionKind == PredictionKind.Ranking)
                        {
                            var elab = features.Values[features.Count - 1];
                            if (pre > 0 && lab < 0)
                            {
                                ++err;
                            }
                        }
                        else if (!lab.Equals(pre))
                        {
                            ++err;
                        }
                        ++nbrows;
                    }
                    if (nbrows == 0)
                    {
                        throw Host.Except("No results.");
                    }
                    if (err * 2 > nbrows)
                    {
                        throw Host.Except("No training.");
                    }
                }
                else
                {
                    cursor.Schema.TryGetColumnIndex("PredictedLabel", out ipred);
                    var  getter      = cursor.GetGetter <bool>(ilab);
                    var  pgetter     = cursor.GetGetter <bool>(ipred);
                    var  counts      = new Dictionary <bool, int>();
                    var  counts_pred = new Dictionary <bool, int>();
                    bool lab         = false;
                    bool pre         = false;
                    int  nbrows      = 0;
                    int  err         = 0;
                    while (cursor.MoveNext())
                    {
                        getter(ref lab);
                        pgetter(ref pre);
                        counts[lab]      = counts.ContainsKey(lab) ? counts[lab] + 1 : 0;
                        counts_pred[pre] = counts_pred.ContainsKey(pre) ? counts_pred[pre] + 1 : 0;
                        if (!lab.Equals(pre))
                        {
                            ++err;
                        }
                        ++nbrows;
                    }
                    if (nbrows == 0)
                    {
                        throw Host.Except("No results.");
                    }
                    if (err * 2 > nbrows)
                    {
                        throw Host.Except("No training.");
                    }
                }
            }
        }