ValueMapper <TSrc, TDst> GetMapperWithTransform <TSrc, TMiddle, TDst>(IDataTransform trans)
        {
            var     mapperPreVM = new ValueMapperFromTransformFloat <TMiddle>(_host, trans, _inputColumn, _inputColumn, trans.Source);
            var     mapperPre   = mapperPreVM.GetMapper <TSrc, TMiddle>();
            var     mapperPred  = (_predictor as IValueMapper).GetMapper <TMiddle, TDst>();
            TMiddle middle      = default(TMiddle);

            return((in TSrc src, ref TDst dst) =>
            {
                mapperPre(in src, ref middle);
                mapperPred(in middle, ref dst);
            });
            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>();
            }
Esempio n. 3
0
            public ValueMapperExample(string modelName, string features)
            {
                _env       = EnvHelper.NewTestEnvironment();
                _predictor = _env.LoadPredictorOrNull(File.OpenRead(modelName));
                var inputs = new Input[0];

                var view = _env.CreateStreamingDataView <Input>(inputs);

                _transforms = ComponentCreation.LoadTransforms(_env, File.OpenRead(modelName), view);
                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>();
            }
        public void TestTransform2ValueMapperMultiThread()
        {
            /*using (*/
            var env = EnvHelper.NewTestEnvironment();
            {
                var host = env.Register("unittest");

                var inputs = new[] {
                    new InputOutput {
                        X = new float[] { 0, 1 }, Y = 10
                    },
                    new InputOutput {
                        X = new float[] { 2, 3 }, Y = 100
                    }
                };

                var data = DataViewConstructionUtils.CreateFromEnumerable(host, inputs);

                var trv = ExtLambdaTransform.CreateMap(host, data,
                                                       (InputOutput src, InputOutput dst, EnvHelper.EmptyState state) =>
                {
                    dst.X = new float[] { src.X[0] + 1f, src.X[1] - 1f };
                }, (EnvHelper.EmptyState state) => { });

                var ino = new InputOutput {
                    X = new float[] { -5, -5 }, Y = 3
                };
                var inob = new VBuffer <float>(2, ino.X);
                var ans  = new VBuffer <float>();

                using (var valueMapper = new ValueMapperFromTransformFloat <VBuffer <float> >(host, trv, "X", "X", ignoreOtherColumn: true))
                {
                    var mapper = valueMapper.GetMapper <VBuffer <float>, VBuffer <float> >();

                    var listy = new List <int>();
                    var listx = new List <float>();
                    int y     = 0;
                    for (int i = 0; i < inputs.Length; ++i)
                    {
                        mapper(in inob, ref ans);
                        y = inputs[i].Y;
                        if (ans.Count != 2)
                        {
                            throw new Exception("Issue with dimension.");
                        }
                        listx.AddRange(ans.GetValues().ToArray());
                        listy.Add((int)y);
                    }
                    if (listy.Count != 2)
                    {
                        throw new Exception("Issue with dimension.");
                    }
                    if (listy[0] != 10 || listy[1] != 100)
                    {
                        throw new Exception("Issue with values.");
                    }
                    if (listx.Count != 4)
                    {
                        throw new Exception("Issue with dimension.");
                    }
                    if (listx[0] != -4)
                    {
                        throw new Exception("Issue with values.");
                    }
                    if (listx[1] != -6)
                    {
                        throw new Exception("Issue with values.");
                    }
                    if (listx[2] != -4)
                    {
                        throw new Exception("Issue with values.");
                    }
                    if (listx[3] != -6)
                    {
                        throw new Exception("Issue with values.");
                    }
                    if (inob.Count != 2)
                    {
                        throw new Exception("Issue with dimension.");
                    }
                    if (inob.Values[0] != -5)
                    {
                        throw new Exception("Values were overwritten.");
                    }
                    if (inob.Values[0] != -5)
                    {
                        throw new Exception("Values were overwritten.");
                    }
                }
            }
        }
Esempio n. 5
0
        public void TestTransform2ValueMapperSingleThread()
        {
            using (var env = EnvHelper.NewTestEnvironment(conc: 1))
            {
                var host = env.Register("unittest");

                var inputs = new[] {
                    new InputOutput {
                        X = new float[] { 0, 1 }, Y = 10
                    },
                    new InputOutput {
                        X = new float[] { 2, 3 }, Y = 100
                    }
                };

                var data = host.CreateStreamingDataView(inputs);

                var trv = LambdaTransform.CreateMap(host, data,
                                                    (InputOutput src, InputOutput2 dst, EnvHelper.EmptyState state) =>
                {
                    dst.X2 = new float[] { src.X[0] + 1f, src.X[1] - 1f };
                }, (state) => { });

                var inos = new InputOutput[] { new InputOutput {
                                                   X = new float[] { -5, -5 }, Y = 3
                                               },
                                               new InputOutput {
                                                   X = new float[] { -6, -6 }, Y = 30
                                               } };
                var ans = new VBuffer <float>();

                using (var valueMapper = new ValueMapperFromTransformFloat <VBuffer <float> >(host, trv, "X", "X2", ignoreOtherColumn: true))
                {
                    var mapper = valueMapper.GetMapper <VBuffer <float>, VBuffer <float> >();

                    var listy = new List <int>();
                    var listx = new List <float>();
                    int y     = 0;
                    int tour  = 0;
                    for (int i = 0; i < inputs.Length; ++i)
                    {
                        var temp = new VBuffer <float>(2, inos[tour++].X);
                        mapper(in temp, ref ans);
                        y = inputs[i].Y;
                        if (ans.Count != 2)
                        {
                            throw new Exception("Issue with dimension.");
                        }
                        listx.AddRange(ans.GetValues().ToArray());
                        listy.Add((int)y);
                    }
                    if (listy.Count != 2)
                    {
                        throw new Exception("Issue with dimension.");
                    }
                    if (listy[0] != 10 || listy[1] != 100)
                    {
                        throw new Exception("Issue with values.");
                    }
                    if (listx.Count != 4)
                    {
                        throw new Exception("Issue with dimension.");
                    }
                    if (listx[0] != -4)
                    {
                        throw new Exception("Issue with values.");
                    }
                    if (listx[1] != -6)
                    {
                        throw new Exception("Issue with values.");
                    }
                    if (listx[2] != -5)
                    {
                        throw new Exception("Issue with values.");
                    }
                    if (listx[3] != -7)
                    {
                        throw new Exception("Issue with values.");
                    }
                }
            }
        }