Ejemplo n.º 1
0
            public void Save(ModelSaveContext ctx)
            {
                _ectx.AssertValue(ctx);

                // *** Binary format ***
                // int: G - number of group columns
                // int[G]: ids of group column names
                // int: K: number of keep columns
                // int[K]: ids of keep column names

                _ectx.AssertNonEmpty(_groupColumns);
                ctx.Writer.Write(_groupColumns.Length);
                foreach (var name in _groupColumns)
                {
                    _ectx.AssertNonEmpty(name);
                    ctx.SaveString(name);
                }

                _ectx.AssertValue(_keepColumns);
                ctx.Writer.Write(_keepColumns.Length);
                foreach (var name in _keepColumns)
                {
                    _ectx.AssertNonEmpty(name);
                    ctx.SaveString(name);
                }
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves the transformer to file.
        /// </summary>
        /// <param name="ctx">The <see cref="ModelSaveContext"/> that facilitates saving to the <see cref="Repository"/>.</param>
        public void Save(ModelSaveContext ctx)
        {
            Host.CheckValue(ctx, nameof(ctx));
            ctx.CheckAtModel();
            ctx.SetVersionInfo(GetVersionInfo());

            // *** Binary format ***
            // model: prediction model.
            // stream: empty data view that contains train schema.
            // ids of strings: feature columns.
            // float: scorer threshold
            // id of string: scorer threshold column

            ctx.SaveModel(Model, DirModel);
            ctx.SaveBinaryStream(DirTransSchema, writer =>
            {
                using (var ch = Host.Start("Saving train schema"))
                {
                    var saver = new BinarySaver(Host, new BinarySaver.Arguments {
                        Silent = true
                    });
                    DataSaverUtils.SaveDataView(ch, saver, new EmptyDataView(Host, TrainSchema), writer.BaseStream);
                }
            });

            for (int i = 0; i < Model.FieldCount; i++)
            {
                ctx.SaveString(FeatureColumns[i]);
            }

            ctx.Writer.Write(_threshold);
            ctx.SaveString(_thresholdColumn);
        }
        internal static void SaveCustomTransformer(IExceptionContext ectx, ModelSaveContext ctx, string contractName, string contractAssembly)
        {
            ectx.CheckValue(ctx, nameof(ctx));
            ectx.CheckValue(contractName, nameof(contractName));

            ctx.CheckAtModel();
            ctx.SetVersionInfo(GetVersionInfo());

            ctx.SaveString(contractName);
            ctx.SaveString(contractAssembly);
        }
Ejemplo n.º 4
0
        private protected override void SaveModel(ModelSaveContext ctx)
        {
            Host.CheckValue(ctx, nameof(ctx));
            ctx.CheckAtModel();
            ctx.SetVersionInfo(GetVersionInfo());

            // *** Binary format ***
            // model: prediction model.
            // stream: empty data view that contains train schema.
            // string: feature column name.
            // string: the name of the columns where tree prediction values are stored.
            // string: the name of the columns where trees' leave are stored.
            // string: the name of the columns where trees' paths are stored.

            ctx.SaveModel(Model, DirModel);
            ctx.SaveBinaryStream(DirTransSchema, writer =>
            {
                using (var ch = Host.Start("Saving train schema"))
                {
                    var saver = new BinarySaver(Host, new BinarySaver.Arguments {
                        Silent = true
                    });
                    DataSaverUtils.SaveDataView(ch, saver, new EmptyDataView(Host, TrainSchema), writer.BaseStream);
                }
            });

            ctx.SaveString(_featureDetachedColumn.Name);
            ctx.SaveStringOrNull(_treesColumnName);
            ctx.SaveStringOrNull(_leavesColumnName);
            ctx.SaveStringOrNull(_pathsColumnName);
        }
Ejemplo n.º 5
0
        private protected override void SaveModel(ModelSaveContext ctx)
        {
            Contracts.AssertValue(ctx);
            ctx.CheckAtModel();
            ctx.SetVersionInfo(GetVersionInfo());

            // *** Binary format ***
            // model: _calibrator
            // scoreColumnName: _scoreColumnName
            ctx.SaveModel(_calibrator, "Calibrator");
            ctx.SaveString(_scoreColumnName);
        }
Ejemplo n.º 6
0
        private protected override void SaveCore(ModelSaveContext ctx)
        {
            Contracts.AssertValue(ctx);
            ctx.SetVersionInfo(GetVersionInfo());

            // *** Binary format ***
            // <base info>
            // float: scorer threshold
            // id of string: scorer threshold column
            base.SaveCore(ctx);

            ctx.Writer.Write(Threshold);
            ctx.SaveString(ThresholdColumn);
        }
        public override void Save(ModelSaveContext ctx)
        {
            Host.CheckValue(ctx, nameof(ctx));
            ctx.CheckAtModel();
            ctx.SetVersionInfo(GetVersionInfo());

            SaveBase(ctx);
            ctx.Writer.WriteBoolByte(_customLookup);
            if (_customLookup)
            {
                ctx.SaveString(_modelFileNameWithPath);
            }
            else
            {
                ctx.Writer.Write((uint)_modelKind);
            }
        }
Ejemplo n.º 8
0
        void ICanSaveModel.Save(ModelSaveContext ctx)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            ctx.SetVersionInfo(GetVersionInfo());

            // ** Binary format **
            // int: number of columns
            // foreach column:
            //   string: column representation

            ctx.Writer.Write(_columns.Length);
            StringBuilder sb = new StringBuilder();

            foreach (var col in _columns)
            {
                sb.Clear();
                _host.Check(col.TryUnparse(sb));
                ctx.SaveString(sb.ToString());
            }
        }
Ejemplo n.º 9
0
        protected void SaveBase(ModelSaveContext ctx)
        {
            Contracts.AssertValue(ctx);

            // *** Binary format ***
            // int: id of the suffix
            // int: the number of input column roles
            // for each input column:
            //   int: id of the column role
            //   int: id of the column name
            ctx.SaveString(Suffix);

            var cols = Mapper.GetInputColumnRoles().ToArray();
            ctx.Writer.Write(cols.Length);
            foreach (var kvp in cols)
            {
                ctx.SaveNonEmptyString(kvp.Key.Value);
                ctx.SaveNonEmptyString(kvp.Value);
            }
        }
Ejemplo n.º 10
0
        public override void Save(ModelSaveContext ctx)
        {
            Host.CheckValue(ctx, nameof(ctx));
            ctx.CheckAtModel();
            ctx.SetVersionInfo(GetVersionInfo());

            // *** Binary format ***
            // <base>
            SaveBase(ctx);

            const string dir = "Stopwords";

            ctx.SaveSubModel(dir,
                             c =>
            {
                Host.CheckValue(c, nameof(ctx));
                c.CheckAtModel();
                c.SetVersionInfo(GetStopwrodsManagerVersionInfo());

                // *** Binary format ***
                // int: number of stopwords
                // int[]: stopwords string ids
                Host.Assert(_stopWordsMap.Count > 0);
                ctx.Writer.Write(_stopWordsMap.Count);
                int id = 0;
                foreach (var nstr in _stopWordsMap)
                {
                    Host.Assert(nstr.Id == id);
                    ctx.SaveString(nstr.Value);
                    id++;
                }

                ctx.SaveTextStream("Stopwords.txt", writer =>
                {
                    foreach (var nstr in _stopWordsMap)
                    {
                        writer.WriteLine("{0}\t{1}", nstr.Id, nstr.Value);
                    }
                });
            });
        }
Ejemplo n.º 11
0
        protected virtual void SaveCore(ModelSaveContext ctx)
        {
            // *** Binary format ***
            // model: prediction model.
            // stream: empty data view that contains train schema.
            // id of string: feature column.

            ctx.SaveModel(Model, DirModel);
            ctx.SaveBinaryStream(DirTransSchema, writer =>
            {
                using (var ch = Host.Start("Saving train schema"))
                {
                    var saver = new BinarySaver(Host, new BinarySaver.Arguments {
                        Silent = true
                    });
                    DataSaverUtils.SaveDataView(ch, saver, new EmptyDataView(Host, TrainSchema), writer.BaseStream);
                }
            });

            ctx.SaveString(FeatureColumn);
        }