/// <summary> /// The counter constructor of re-creating <see cref="MatrixFactorizationPredictionTransformer"/> from the context where /// the original transform is saved. /// </summary> public MatrixFactorizationPredictionTransformer(IHostEnvironment host, ModelLoadContext ctx) : base(Contracts.CheckRef(host, nameof(host)).Register(nameof(MatrixFactorizationPredictionTransformer)), ctx) { // *** Binary format *** // <base info> // string: the column name of matrix's column ids. // string: the column name of matrix's row ids. MatrixColumnIndexColumnName = ctx.LoadString(); MatrixRowIndexColumnName = ctx.LoadString(); if (!TrainSchema.TryGetColumnIndex(MatrixColumnIndexColumnName, out int xCol)) { throw Host.ExceptSchemaMismatch(nameof(MatrixColumnIndexColumnName), RecommenderUtils.MatrixColumnIndexKind.Value, MatrixColumnIndexColumnName); } MatrixColumnIndexColumnType = TrainSchema.GetColumnType(xCol); if (!TrainSchema.TryGetColumnIndex(MatrixRowIndexColumnName, out int yCol)) { throw Host.ExceptSchemaMismatch(nameof(MatrixRowIndexColumnName), RecommenderUtils.MatrixRowIndexKind.Value, MatrixRowIndexColumnName); } MatrixRowIndexColumnType = TrainSchema.GetColumnType(yCol); BindableMapper = ScoreUtils.GetSchemaBindableMapper(Host, Model); var schema = GetSchema(); var args = new GenericScorer.Arguments { Suffix = "" }; Scorer = new GenericScorer(Host, args, new EmptyDataView(Host, TrainSchema), BindableMapper.Bind(Host, schema), schema); }
/// <summary> /// Build a transformer based on matrix factorization predictor (model) and the input schema (trainSchema). The created /// transformer can only transform IDataView objects compatible to the input schema; that is, that IDataView must contain /// columns specified by <see cref="MatrixColumnIndexColumnName"/>, <see cref="MatrixColumnIndexColumnType"/>, <see cref="MatrixRowIndexColumnName"/>, and <see cref="MatrixRowIndexColumnType"></see>. /// The output column is "Score" by default but user can append a string to it. /// </summary> /// <param name="env">Eviroment object for showing information</param> /// <param name="model">The model trained by one of the training functions in <see cref="MatrixFactorizationTrainer"/></param> /// <param name="trainSchema">Targeted schema that containing columns named as xColumnName</param> /// <param name="matrixColumnIndexColumnName">The name of the column used as role <see cref="RecommenderUtils.MatrixColumnIndexKind"/> in matrix factorization world</param> /// <param name="matrixRowIndexColumnName">The name of the column used as role <see cref="RecommenderUtils.MatrixRowIndexKind"/> in matrix factorization world</param> /// <param name="scoreColumnNameSuffix">A string attached to the output column name of this transformer</param> public MatrixFactorizationPredictionTransformer(IHostEnvironment env, MatrixFactorizationPredictor model, Schema trainSchema, string matrixColumnIndexColumnName, string matrixRowIndexColumnName, string scoreColumnNameSuffix = "") : base(Contracts.CheckRef(env, nameof(env)).Register(nameof(MatrixFactorizationPredictionTransformer)), model, trainSchema) { Host.CheckNonEmpty(matrixColumnIndexColumnName, nameof(matrixRowIndexColumnName)); Host.CheckNonEmpty(matrixColumnIndexColumnName, nameof(matrixRowIndexColumnName)); MatrixColumnIndexColumnName = matrixColumnIndexColumnName; MatrixRowIndexColumnName = matrixRowIndexColumnName; if (!trainSchema.TryGetColumnIndex(MatrixColumnIndexColumnName, out int xCol)) { throw Host.ExceptSchemaMismatch(nameof(MatrixColumnIndexColumnName), RecommenderUtils.MatrixColumnIndexKind.Value, MatrixColumnIndexColumnName); } MatrixColumnIndexColumnType = trainSchema.GetColumnType(xCol); if (!trainSchema.TryGetColumnIndex(MatrixRowIndexColumnName, out int yCol)) { throw Host.ExceptSchemaMismatch(nameof(yCol), RecommenderUtils.MatrixRowIndexKind.Value, MatrixRowIndexColumnName); } BindableMapper = ScoreUtils.GetSchemaBindableMapper(Host, model); var schema = GetSchema(); var args = new GenericScorer.Arguments { Suffix = scoreColumnNameSuffix }; Scorer = new GenericScorer(Host, args, new EmptyDataView(Host, trainSchema), BindableMapper.Bind(Host, schema), schema); }