/// <summary>
        /// Loads a predictor from the model stream. Returns null iff there's no predictor.
        /// </summary>
        /// <param name="env">The host environment to use.</param>
        /// <param name="modelStream">The model stream.</param>
        public static Predictor LoadPredictorOrNull(this IHostEnvironment env, Stream modelStream)
        {
            Contracts.CheckValue(modelStream, nameof(modelStream));
            var p = ModelFileUtils.LoadPredictorOrNull(env, modelStream);

            return(p == null ? null : new Predictor(p));
        }
        public void TestI_Q_KMeansInnerAPIWithDataFrame()
        {
            var methodName       = System.Reflection.MethodBase.GetCurrentMethod().Name;
            var outModelFilePath = FileHelper.GetOutputFile("outModelFilePath.zip", methodName);
            var iris             = FileHelper.GetTestFile("iris.txt");
            /*using (*/
            var env = new ConsoleEnvironment();
            {
                ComponentHelper.AddStandardComponents(env);

                var        df      = Scikit.ML.DataManipulation.DataFrameIO.ReadCsv(iris, sep: '\t', dtypes: new DataViewType[] { NumberDataViewType.Single });
                var        conc    = env.CreateTransform("Concat{col=Feature:Sepal_length,Sepal_width}", df);
                var        roleMap = env.CreateExamples(conc, "Feature", label: "Label");
                var        trainer = CreateTrainer(env, "km");
                IPredictor model;
                using (var ch = env.Start("test"))
                    model = TrainUtils.Train(env, ch, roleMap, trainer, null, 0);

                using (var ch = env.Start("Save"))
                    using (var fs = File.Create(outModelFilePath))
                        TrainUtils.SaveModel(env, ch, fs, model, roleMap);

                IPredictor ipred;
                using (var fs = File.OpenRead(outModelFilePath))
                    ipred = ModelFileUtils.LoadPredictorOrNull(env, fs);

                var scorer = ScoreUtils.GetScorer(ipred, roleMap, env, null);
                var dfout  = Scikit.ML.DataManipulation.DataFrameIO.ReadView(scorer);
                Assert.AreEqual(dfout.Shape, new Tuple <int, int>(150, 13));
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Looks for a predictor among tagged predictors.
 /// If the tag ends by .zip, it assumes it is a file.
 /// </summary>
 /// <param name="env">environment</param>
 /// <param name="input">IDataView</param>
 /// <param name="tag">tag name</param>
 /// <returns>predictor</returns>
 public static IPredictor GetTaggedPredictor(IHostEnvironment env, IDataView input, string tag)
 {
     if (string.IsNullOrEmpty(tag))
     {
         throw env.Except("tag must not be null.");
     }
     if (tag.EndsWith(".zip"))
     {
         using (Stream modelStream = new FileStream(tag, FileMode.Open, FileAccess.Read))
         {
             var ipred = ModelFileUtils.LoadPredictorOrNull(env, modelStream);
             return(ipred);
         }
     }
     else
     {
         var tagged = TagHelper.EnumerateTaggedView(true, input).Where(c => c.Item1 == tag);
         if (!tagged.Any())
         {
             throw env.Except("Unable to find any view with tag '{0}'.", tag);
         }
         if (tagged.Skip(1).Any())
         {
             var allTagged = TagHelper.EnumerateTaggedView(true, input).ToArray();
             throw env.Except("Ambiguous tag '{0}' - {1}/{2}.", tag,
                              allTagged.Where(c => c.Item1 == tag).Count(), allTagged.Length);
         }
         var predictor = tagged.First().Item2.TaggedPredictor;
         if (predictor == null)
         {
             env.Except("Tagged view '{0}' does not host a predictor.", tag);
         }
         return(predictor);
     }
 }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="env">environment</param>
        /// <param name="modelStream">stream</param>
        /// <param name="output">name of the output column</param>
        /// <param name="outputIsFloat">output is a gloat (true) or a vector of floats (false)</param>
        /// <param name="conc">number of concurrency threads</param>
        /// <param name="features">features name</param>
        public ValueMapperPredictionEngineFloat(IHostEnvironment env, Stream modelStream,
                                                string output   = "Probability", bool outputIsFloat = true, int conc = 1,
                                                string features = "Features")
        {
            _env = env;
            if (_env == null)
            {
                throw Contracts.Except("env must not be null");
            }
            var inputs = new FloatVectorInput[0];
            var view   = DataViewConstructionUtils.CreateFromEnumerable <FloatVectorInput>(_env, inputs);

            long modelPosition = modelStream.Position;

            _predictor = ModelFileUtils.LoadPredictorOrNull(_env, modelStream);
            if (_predictor == null)
            {
                throw _env.Except("Unable to load a model.");
            }
            modelStream.Seek(modelPosition, SeekOrigin.Begin);
            _transforms = ModelFileUtils.LoadTransforms(_env, view, modelStream);
            if (_transforms == null)
            {
                throw _env.Except("Unable to load a model.");
            }

            var data = _env.CreateExamples(_transforms, features);

            if (data == null)
            {
                throw _env.Except("Cannot create rows.");
            }
            var scorer = _env.CreateDefaultScorer(data, _predictor);

            if (scorer == null)
            {
                throw _env.Except("Cannot create a scorer.");
            }

            _valueMapper = new ValueMapperFromTransformFloat <VBuffer <float> >(_env,
                                                                                scorer, features, output, conc: conc);
            if (_valueMapper == null)
            {
                throw _env.Except("Cannot create a mapper.");
            }
            if (outputIsFloat)
            {
                _mapper       = _valueMapper.GetMapper <VBuffer <float>, float>();
                _mapperVector = null;
            }
            else
            {
                _mapper       = null;
                _mapperVector = _valueMapper.GetMapper <VBuffer <float>, VBuffer <float> >();
            }
        }
        /// <summary>
        /// Loads a pipeline saved in zip format.
        /// </summary>
        protected void Load(Stream fs)
        {
            var transformPipe = ModelFileUtils.LoadPipeline(_env, fs, new MultiFileSource(null), true);
            var ipred         = ModelFileUtils.LoadPredictorOrNull(_env, fs);

            IDataView root;

            for (root = transformPipe; root is IDataTransform && !(root is PassThroughTransform); root = ((IDataTransform)root).Source)
            {
                ;
            }
            if (!(root is PassThroughTransform))
            {
                var tr = new PassThroughTransform(_env, new PassThroughTransform.Arguments(), root);
                transformPipe = ApplyTransformUtils.ApplyAllTransformsToData(_env, transformPipe, tr, root);
            }

            var stack = new List <IDataView>();

            for (root = transformPipe; root is IDataTransform; root = ((IDataTransform)root).Source)
            {
                stack.Add(root);
            }
            stack.Reverse();

            _transforms = new StepTransform[stack.Count];
            for (int i = 0; i < _transforms.Length; ++i)
            {
                _transforms[i] = new StepTransform()
                {
                    transform = stack[i] as IDataTransform, transformSettings = null
                }
            }
            ;

            if (ipred == null)
            {
                _predictor = new StepPredictor()
                {
                    predictor = null, roleMapData = null, trainer = null, trainerSettings = null
                }
            }
            ;
            else
            {
                _roles = ModelFileUtils.LoadRoleMappingsOrNull(_env, fs).ToList();
                var data = new RoleMappedData(transformPipe, _roles);
                _predictor = new StepPredictor()
                {
                    predictor = ipred, roleMapData = data, trainer = null, trainerSettings = null
                };
            }
            _fastValueMapper = null;
        }
            public ValueMapperExample(string modelName, string features)
            {
                _env       = EnvHelper.NewTestEnvironment();
                _predictor = ModelFileUtils.LoadPredictorOrNull(_env, File.OpenRead(modelName));
                var inputs = new Input[0];

                var view = DataViewConstructionUtils.CreateFromEnumerable(_env, inputs);

                _transforms = ModelFileUtils.LoadTransforms(_env, view, File.OpenRead(modelName));
                var data   = _env.CreateExamples(_transforms, features);
                var scorer = _env.CreateDefaultScorer(data, _predictor);

                _valueMapper = new ValueMapperFromTransformFloat <VBuffer <float> >(_env, scorer, "Features", "Probability");
                _mapper      = _valueMapper.GetMapper <VBuffer <float>, float>();
            }
        /// <summary>
        /// Serialization constructor
        /// </summary>
        public TrainedModel(SerializationInfo info, StreamingContext context)
        {
            Properties  = (ModelProperties)info.GetValue(nameof(Properties), typeof(ModelProperties));
            ItemIdIndex = (string[])info.GetValue(nameof(ItemIdIndex), typeof(string[]));

            byte[] sarModelBytes = (byte[])info.GetValue(nameof(RecommenderData), typeof(byte[]));
            using (var stream = new MemoryStream(sarModelBytes))
            {
                using (var environment = new TlcEnvironment(verbose: true))
                {
                    IPredictor predictor = ModelFileUtils.LoadPredictorOrNull(environment, stream);
                    RecommenderData = new ModelRecommenderData(predictor as IUserHistoryToItemsRecommender);
                }
            }
        }
        internal PredictorModelImpl(IHostEnvironment env, Stream stream)
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(stream, nameof(stream));
            using (var ch = env.Start("Loading predictor model"))
            {
                // REVIEW: address the asymmetry in the way we're loading and saving the model.
                TransformModel = new TransformModelImpl(env, stream);

                var roles = ModelFileUtils.LoadRoleMappingsOrNull(env, stream);
                env.CheckDecode(roles != null, "Predictor model must contain role mappings");
                _roleMappings = roles.ToArray();

                Predictor = ModelFileUtils.LoadPredictorOrNull(env, stream);
                env.CheckDecode(Predictor != null, "Predictor model must contain a predictor");
            }
        }
Esempio n. 9
0
        public void TestMLRWithStatsBackCompatibility()
        {
            string dropModelPath = GetDataPath("backcompat/MlrWithStats.zip");
            string trainData     = GetDataPath("iris.data");

            using (FileStream fs = File.OpenRead(dropModelPath))
            {
                var result = ModelFileUtils.LoadPredictorOrNull(Env, fs) as MaximumEntropyModelParameters;
                var stats  = result?.Statistics;

                Assert.Equal(132.012238f, stats.Deviance);
                Assert.Equal(329.583679f, stats.NullDeviance);
                Assert.Equal(11, stats.ParametersCount);
                Assert.Equal(150, stats.TrainingExampleCount);
            }

            Done();
        }
Esempio n. 10
0
        public void TestLRWithStatsBackCompatibility()
        {
            string dropModelPath = GetDataPath("backcompat/LrWithStats.zip");
            string trainData     = GetDataPath("adult.tiny.with-schema.txt");

            using (FileStream fs = File.OpenRead(dropModelPath))
            {
                var result       = ModelFileUtils.LoadPredictorOrNull(Env, fs) as CalibratedModelParametersBase <LinearBinaryModelParameters, PlattCalibrator>;
                var subPredictor = result?.SubModel as LinearBinaryModelParameters;
                var stats        = subPredictor?.Statistics;

                CompareNumbersWithTolerance(stats.Deviance, 458.970917);
                CompareNumbersWithTolerance(stats.NullDeviance, 539.276367);
                Assert.Equal(7, stats.ParametersCount);
                Assert.Equal(500, stats.TrainingExampleCount);
            }

            Done();
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="env">environment</param>
        /// <param name="modelStream">stream</param>
        /// <param name="conc">number of concurrency threads</param>
        /// <param name="features">features column</param>
        public ValueMapperPredictionEngine(IHostEnvironment env, Stream modelStream, string features = "Features")
        {
            _env = env;
            if (_env == null)
            {
                throw Contracts.Except("env must not be null");
            }
            var inputs = new TRowValue[0];
            var view   = DataViewConstructionUtils.CreateFromEnumerable <TRowValue>(_env, inputs);

            long modelPosition = modelStream.Position;

            _predictor = ModelFileUtils.LoadPredictorOrNull(_env, modelStream);
            if (_predictor == null)
            {
                throw _env.Except("Unable to load a model.");
            }
            modelStream.Seek(modelPosition, SeekOrigin.Begin);
            _transforms = ModelFileUtils.LoadTransforms(_env, view, modelStream);
            if (_transforms == null)
            {
                throw _env.Except("Unable to load a model.");
            }

            var data = _env.CreateExamples(_transforms, features);

            if (data == null)
            {
                throw _env.Except("Cannot create rows.");
            }
            var scorer = _env.CreateDefaultScorer(data, _predictor);

            if (scorer == null)
            {
                throw _env.Except("Cannot create a scorer.");
            }
            _CreateMapper(scorer);
        }
Esempio n. 12
0
 /// <summary>
 /// Loads a predictor from the model stream. Returns null iff there's no predictor.
 /// </summary>
 /// <param name="env">The host environment to use.</param>
 /// <param name="modelStream">The model stream.</param>
 public static IPredictor LoadPredictorOrNull(this IHostEnvironment env, Stream modelStream)
 {
     Contracts.CheckValue(modelStream, nameof(modelStream));
     return(ModelFileUtils.LoadPredictorOrNull(env, modelStream));
 }
            /// <summary>
            /// Loads multiple artifacts of interest from the input model file, given the context
            /// established by the command line arguments.
            /// </summary>
            /// <param name="ch">The channel to which to provide output.</param>
            /// <param name="wantPredictor">Whether we want a predictor from the model file. If
            /// <c>false</c> we will not even attempt to load a predictor. If <c>null</c> we will
            /// load the predictor, if present. If <c>true</c> we will load the predictor, or fail
            /// noisily if we cannot.</param>
            /// <param name="predictor">The predictor in the model, or <c>null</c> if
            /// <paramref name="wantPredictor"/> was false, or <paramref name="wantPredictor"/> was
            /// <c>null</c> and no predictor was present.</param>
            /// <param name="wantTrainSchema">Whether we want the training schema. Unlike
            /// <paramref name="wantPredictor"/>, this has no "hard fail if not present" option. If
            /// this is <c>true</c>, it is still possible for <paramref name="trainSchema"/> to remain
            /// <c>null</c> if there were no role mappings, or pipeline.</param>
            /// <param name="trainSchema">The training schema if <paramref name="wantTrainSchema"/>
            /// is true, and there were role mappings stored in the model.</param>
            /// <param name="pipe">The data pipe constructed from the combination of the
            /// model and command line arguments.</param>
            protected void LoadModelObjects(
                IChannel ch,
                bool?wantPredictor, out IPredictor predictor,
                bool wantTrainSchema, out RoleMappedSchema trainSchema,
                out ILegacyDataLoader pipe)
            {
                // First handle the case where there is no input model file.
                // Everything must come from the command line.

                using (var file = Host.OpenInputFile(ImplOptions.InputModelFile))
                    using (var strm = file.OpenReadStream())
                        using (var rep = RepositoryReader.Open(strm, Host))
                        {
                            // First consider loading the predictor.
                            if (wantPredictor == false)
                            {
                                predictor = null;
                            }
                            else
                            {
                                ch.Trace("Loading predictor");
                                predictor = ModelFileUtils.LoadPredictorOrNull(Host, rep);
                                if (wantPredictor == true)
                                {
                                    Host.Check(predictor != null, "Could not load predictor from model file");
                                }
                            }

                            // Next create the loader.
                            var loaderFactory           = ImplOptions.Loader;
                            ILegacyDataLoader trainPipe = null;
                            if (loaderFactory != null)
                            {
                                // The loader is overridden from the command line.
                                pipe = loaderFactory.CreateComponent(Host, new MultiFileSource(ImplOptions.DataFile));
                                if (ImplOptions.LoadTransforms == true)
                                {
                                    Host.CheckUserArg(!string.IsNullOrWhiteSpace(ImplOptions.InputModelFile), nameof(ImplOptions.InputModelFile));
                                    pipe = LoadTransformChain(pipe);
                                }
                            }
                            else
                            {
                                var loadTrans = ImplOptions.LoadTransforms ?? true;
                                pipe = LoadLoader(rep, ImplOptions.DataFile, loadTrans);
                                if (loadTrans)
                                {
                                    trainPipe = pipe;
                                }
                            }

                            if (Utils.Size(ImplOptions.Transforms) > 0)
                            {
                                pipe = LegacyCompositeDataLoader.Create(Host, pipe, ImplOptions.Transforms);
                            }

                            // Next consider loading the training data's role mapped schema.
                            trainSchema = null;
                            if (wantTrainSchema)
                            {
                                // First try to get the role mappings.
                                var trainRoleMappings = ModelFileUtils.LoadRoleMappingsOrNull(Host, rep);
                                if (trainRoleMappings != null)
                                {
                                    // Next create the training schema. In the event that the loaded pipeline happens
                                    // to be the training pipe, we can just use that. If it differs, then we need to
                                    // load the full pipeline from the model, relying upon the fact that all loaders
                                    // can be loaded with no data at all, to get their schemas.
                                    if (trainPipe == null)
                                    {
                                        trainPipe = ModelFileUtils.LoadLoader(Host, rep, new MultiFileSource(null), loadTransforms: true);
                                    }
                                    trainSchema = new RoleMappedSchema(trainPipe.Schema, trainRoleMappings);
                                }
                                // If the role mappings are null, an alternative would be to fail. However the idea
                                // is that the scorer should always still succeed, although perhaps with reduced
                                // functionality, even when the training schema is null, since not all versions of
                                // TLC models will have the role mappings preserved, I believe. And, we do want to
                                // maintain backwards compatibility.
                            }
                        }
            }
Esempio n. 14
0
        /// <summary>
        /// Finalizes the test on a predictor, calls the predictor with a scorer,
        /// saves the data, saves the models, loads it back, saves the data again,
        /// checks the output is the same.
        /// </summary>
        /// <param name="env">environment</param>
        /// <param name="outModelFilePath">output filename</param>
        /// <param name="predictor">predictor</param>
        /// <param name="roles">label, feature, ...</param>
        /// <param name="outData">first output data</param>
        /// <param name="outData2">second output data</param>
        /// <param name="kind">prediction kind</param>
        /// <param name="checkError">checks errors</param>
        /// <param name="ratio">check the error is below that threshold (if checkError is true)</param>
        /// <param name="ratioReadSave">check the predictions difference after reloading the model are below this threshold</param>
        public static void FinalizeSerializationTest(IHostEnvironment env,
                                                     string outModelFilePath, IPredictor predictor,
                                                     RoleMappedData roles, string outData, string outData2,
                                                     PredictionKind kind, bool checkError = true,
                                                     float ratio    = 0.8f, float ratioReadSave = 0.06f,
                                                     bool checkType = true)
        {
            string labelColumn = kind != PredictionKind.Clustering ? roles.Schema.Label.Value.Name : null;

            #region save, reading, running

            // Saves model.
            using (var ch = env.Start("Save"))
                using (var fs = File.Create(outModelFilePath))
                    TrainUtils.SaveModel(env, ch, fs, predictor, roles);
            if (!File.Exists(outModelFilePath))
            {
                throw new FileNotFoundException(outModelFilePath);
            }

            // Loads the model back.
            using (var fs = File.OpenRead(outModelFilePath))
            {
                var pred_local = ModelFileUtils.LoadPredictorOrNull(env, fs);
                if (pred_local == null)
                {
                    throw new Exception(string.Format("Unable to load '{0}'", outModelFilePath));
                }
                if (checkType && predictor.GetType() != pred_local.GetType())
                {
                    throw new Exception(string.Format("Type mismatch {0} != {1}", predictor.GetType(), pred_local.GetType()));
                }
            }

            // Checks the outputs.
            var sch1   = SchemaHelper.ToString(roles.Schema.Schema);
            var scorer = PredictorHelper.CreateDefaultScorer(env, roles, predictor);

            var sch2 = SchemaHelper.ToString(scorer.Schema);
            if (string.IsNullOrEmpty(sch1) || string.IsNullOrEmpty(sch2))
            {
                throw new Exception("Empty schemas");
            }

            var saver   = env.CreateSaver("Text");
            var columns = new int[scorer.Schema.Count];
            for (int i = 0; i < columns.Length; ++i)
            {
                columns[i] = saver.IsColumnSavable(scorer.Schema[i].Type) ? i : -1;
            }
            columns = columns.Where(c => c >= 0).ToArray();
            using (var fs2 = File.Create(outData))
                saver.SaveData(fs2, scorer, columns);

            if (!File.Exists(outModelFilePath))
            {
                throw new FileNotFoundException(outData);
            }

            // Check we have the same output.
            using (var fs = File.OpenRead(outModelFilePath))
            {
                var model = ModelFileUtils.LoadPredictorOrNull(env, fs);
                scorer = PredictorHelper.CreateDefaultScorer(env, roles, model);
                saver  = env.CreateSaver("Text");
                using (var fs2 = File.Create(outData2))
                    saver.SaveData(fs2, scorer, columns);
            }

            var t1 = File.ReadAllLines(outData);
            var t2 = File.ReadAllLines(outData2);
            if (t1.Length != t2.Length)
            {
                throw new Exception(string.Format("Not the same number of lines: {0} != {1}", t1.Length, t2.Length));
            }
            var linesN = new List <int>();
            for (int i = 0; i < t1.Length; ++i)
            {
                if (t1[i] != t2[i])
                {
                    linesN.Add(i);
                }
            }
            if (linesN.Count > (int)(t1.Length * ratioReadSave))
            {
                var rows = linesN.Select(i => string.Format("1-Mismatch on line {0}/{3}:\n{1}\n{2}", i, t1[i], t2[i], t1.Length)).ToList();
                rows.Add($"Number of differences: {linesN.Count}/{t1.Length}");
                throw new Exception(string.Join("\n", rows));
            }

            #endregion

            #region clustering

            if (kind == PredictionKind.Clustering)
            {
                // Nothing to do here.
                return;
            }

            #endregion

            #region supervized

            string expectedOuput = kind == PredictionKind.Regression ? "Score" : "PredictedLabel";

            // Get label and basic checking about performance.
            using (var cursor = scorer.GetRowCursor(scorer.Schema))
            {
                int ilabel, ipred;
                ilabel = SchemaHelper.GetColumnIndex(cursor.Schema, labelColumn);
                ipred  = SchemaHelper.GetColumnIndex(cursor.Schema, expectedOuput);
                var ty1   = cursor.Schema[ilabel].Type;
                var ty2   = cursor.Schema[ipred].Type;
                var dist1 = new Dictionary <int, int>();
                var dist2 = new Dictionary <int, int>();
                var conf  = new Dictionary <Tuple <int, int>, long>();

                if (kind == PredictionKind.MulticlassClassification)
                {
                    #region Multiclass

                    if (!ty2.IsKey())
                    {
                        throw new Exception(string.Format("Label='{0}' Predicted={1}'\nSchema: {2}", ty1, ty2, SchemaHelper.ToString(cursor.Schema)));
                    }

                    if (ty1.RawKind() == DataKind.Single)
                    {
                        var   lgetter = cursor.GetGetter <float>(SchemaHelper._dc(ilabel, cursor));
                        var   pgetter = cursor.GetGetter <uint>(SchemaHelper._dc(ipred, cursor));
                        float ans     = 0;
                        uint  pre     = 0;
                        while (cursor.MoveNext())
                        {
                            lgetter(ref ans);
                            pgetter(ref pre);

                            // The scorer +1 to the argmax.
                            ++ans;

                            var key = new Tuple <int, int>((int)pre, (int)ans);
                            if (!conf.ContainsKey(key))
                            {
                                conf[key] = 1;
                            }
                            else
                            {
                                ++conf[key];
                            }
                            if (!dist1.ContainsKey((int)ans))
                            {
                                dist1[(int)ans] = 1;
                            }
                            else
                            {
                                ++dist1[(int)ans];
                            }
                            if (!dist2.ContainsKey((int)pre))
                            {
                                dist2[(int)pre] = 1;
                            }
                            else
                            {
                                ++dist2[(int)pre];
                            }
                        }
                    }
                    else if (ty1.RawKind() == DataKind.UInt32 && ty1.IsKey())
                    {
                        var  lgetter = cursor.GetGetter <uint>(SchemaHelper._dc(ilabel, cursor));
                        var  pgetter = cursor.GetGetter <uint>(SchemaHelper._dc(ipred, cursor));
                        uint ans     = 0;
                        uint pre     = 0;
                        while (cursor.MoveNext())
                        {
                            lgetter(ref ans);
                            pgetter(ref pre);

                            var key = new Tuple <int, int>((int)pre, (int)ans);
                            if (!conf.ContainsKey(key))
                            {
                                conf[key] = 1;
                            }
                            else
                            {
                                ++conf[key];
                            }
                            if (!dist1.ContainsKey((int)ans))
                            {
                                dist1[(int)ans] = 1;
                            }
                            else
                            {
                                ++dist1[(int)ans];
                            }
                            if (!dist2.ContainsKey((int)pre))
                            {
                                dist2[(int)pre] = 1;
                            }
                            else
                            {
                                ++dist2[(int)pre];
                            }
                        }
                    }
                    else
                    {
                        throw new NotImplementedException(string.Format("Not implemented for type {0}", ty1.ToString()));
                    }
                    #endregion
                }
                else if (kind == PredictionKind.BinaryClassification)
                {
                    #region binary classification

                    if (ty2.RawKind() != DataKind.Boolean)
                    {
                        throw new Exception(string.Format("Label='{0}' Predicted={1}'\nSchema: {2}", ty1, ty2, SchemaHelper.ToString(cursor.Schema)));
                    }

                    if (ty1.RawKind() == DataKind.Single)
                    {
                        var   lgetter = cursor.GetGetter <float>(SchemaHelper._dc(ilabel, cursor));
                        var   pgetter = cursor.GetGetter <bool>(SchemaHelper._dc(ipred, cursor));
                        float ans     = 0;
                        bool  pre     = default(bool);
                        while (cursor.MoveNext())
                        {
                            lgetter(ref ans);
                            pgetter(ref pre);

                            if (ans != 0 && ans != 1)
                            {
                                throw Contracts.Except("The problem is not binary, expected answer is {0}", ans);
                            }

                            var key = new Tuple <int, int>(pre ? 1 : 0, (int)ans);
                            if (!conf.ContainsKey(key))
                            {
                                conf[key] = 1;
                            }
                            else
                            {
                                ++conf[key];
                            }
                            if (!dist1.ContainsKey((int)ans))
                            {
                                dist1[(int)ans] = 1;
                            }
                            else
                            {
                                ++dist1[(int)ans];
                            }
                            if (!dist2.ContainsKey(pre ? 1 : 0))
                            {
                                dist2[pre ? 1 : 0] = 1;
                            }
                            else
                            {
                                ++dist2[pre ? 1 : 0];
                            }
                        }
                    }
                    else if (ty1.RawKind() == DataKind.UInt32)
                    {
                        var  lgetter = cursor.GetGetter <uint>(SchemaHelper._dc(ilabel, cursor));
                        var  pgetter = cursor.GetGetter <bool>(SchemaHelper._dc(ipred, cursor));
                        uint ans     = 0;
                        bool pre     = default(bool);
                        while (cursor.MoveNext())
                        {
                            lgetter(ref ans);
                            pgetter(ref pre);
                            if (ty1.IsKey())
                            {
                                --ans;
                            }

                            if (ans != 0 && ans != 1)
                            {
                                throw Contracts.Except("The problem is not binary, expected answer is {0}", ans);
                            }

                            var key = new Tuple <int, int>(pre ? 1 : 0, (int)ans);
                            if (!conf.ContainsKey(key))
                            {
                                conf[key] = 1;
                            }
                            else
                            {
                                ++conf[key];
                            }
                            if (!dist1.ContainsKey((int)ans))
                            {
                                dist1[(int)ans] = 1;
                            }
                            else
                            {
                                ++dist1[(int)ans];
                            }
                            if (!dist2.ContainsKey(pre ? 1 : 0))
                            {
                                dist2[pre ? 1 : 0] = 1;
                            }
                            else
                            {
                                ++dist2[pre ? 1 : 0];
                            }
                        }
                    }
                    else if (ty1.RawKind() == DataKind.Boolean)
                    {
                        var  lgetter = cursor.GetGetter <bool>(SchemaHelper._dc(ilabel, cursor));
                        var  pgetter = cursor.GetGetter <bool>(SchemaHelper._dc(ipred, cursor));
                        bool ans     = default(bool);
                        bool pre     = default(bool);
                        while (cursor.MoveNext())
                        {
                            lgetter(ref ans);
                            pgetter(ref pre);

                            var key = new Tuple <int, int>(pre ? 1 : 0, ans ? 1 : 0);
                            if (!conf.ContainsKey(key))
                            {
                                conf[key] = 1;
                            }
                            else
                            {
                                ++conf[key];
                            }

                            if (!dist1.ContainsKey(ans ? 1 : 0))
                            {
                                dist1[ans ? 1 : 0] = 1;
                            }
                            else
                            {
                                ++dist1[ans ? 1 : 0];
                            }
                            if (!dist2.ContainsKey(pre ? 1 : 0))
                            {
                                dist2[pre ? 1 : 0] = 1;
                            }
                            else
                            {
                                ++dist2[pre ? 1 : 0];
                            }
                        }
                    }
                    else
                    {
                        throw new NotImplementedException(string.Format("Not implemented for type {0}", ty1));
                    }

                    #endregion
                }
                else if (kind == PredictionKind.Regression)
                {
                    #region regression

                    if (ty1.RawKind() != DataKind.Single)
                    {
                        throw new Exception(string.Format("Label='{0}' Predicted={1}'\nSchema: {2}", ty1, ty2, SchemaHelper.ToString(cursor.Schema)));
                    }
                    if (ty2.RawKind() != DataKind.Single)
                    {
                        throw new Exception(string.Format("Label='{0}' Predicted={1}'\nSchema: {2}", ty1, ty2, SchemaHelper.ToString(cursor.Schema)));
                    }

                    var   lgetter = cursor.GetGetter <float>(SchemaHelper._dc(ilabel, cursor));
                    var   pgetter = cursor.GetGetter <float>(SchemaHelper._dc(ipred, cursor));
                    float ans     = 0;
                    float pre     = 0f;
                    float error   = 0f;
                    while (cursor.MoveNext())
                    {
                        lgetter(ref ans);
                        pgetter(ref pre);
                        error += (ans - pre) * (ans - pre);
                        if (!dist1.ContainsKey((int)ans))
                        {
                            dist1[(int)ans] = 1;
                        }
                        else
                        {
                            ++dist1[(int)ans];
                        }
                        if (!dist2.ContainsKey((int)pre))
                        {
                            dist2[(int)pre] = 1;
                        }
                        else
                        {
                            ++dist2[(int)pre];
                        }
                    }

                    if (float.IsNaN(error) || float.IsInfinity(error))
                    {
                        throw new Exception("Regression wen wrong. Error is infinite.");
                    }

                    #endregion
                }
                else
                {
                    throw new NotImplementedException(string.Format("Not implemented for kind {0}", kind));
                }

                var nbError = conf.Where(c => c.Key.Item1 != c.Key.Item2).Select(c => c.Value).Sum();
                var nbTotal = conf.Select(c => c.Value).Sum();

                if (checkError && (nbError * 1.0 > nbTotal * ratio || dist2.Count <= 1))
                {
                    var sconf = string.Join("\n", conf.OrderBy(c => c.Key)
                                            .Select(c => string.Format("pred={0} exp={1} count={2}", c.Key.Item1, c.Key.Item2, c.Value)));
                    var sdist2 = string.Join("\n", dist1.OrderBy(c => c.Key)
                                             .Select(c => string.Format("label={0} count={1}", c.Key, c.Value)));
                    var sdist1 = string.Join("\n", dist2.OrderBy(c => c.Key).Take(20)
                                             .Select(c => string.Format("label={0} count={1}", c.Key, c.Value)));
                    throw new Exception(string.Format("Too many errors {0}/{1}={7}\n###########\nConfusion:\n{2}\n########\nDIST1\n{3}\n###########\nDIST2\n{4}\nOutput:\n{5}\n...\n{6}",
                                                      nbError, nbTotal,
                                                      sconf, sdist1, sdist2,
                                                      string.Join("\n", t1.Take(Math.Min(30, t1.Length))),
                                                      string.Join("\n", t1.Skip(Math.Max(0, t1.Length - 30))),
                                                      nbError * 1.0 / nbTotal));
                }
            }

            #endregion
        }