From 979824f49f2d382ca0ae28b5a876e3a6165b6f00 Mon Sep 17 00:00:00 2001 From: Pete Luferenko Date: Mon, 20 Aug 2018 09:00:40 -0700 Subject: [PATCH 1/8] Code quality --- src/Microsoft.ML.Core/Data/IEstimator.cs | 190 +++++++++++++++ .../DataLoadSave/EstimatorChain.cs | 78 ++++++ .../DataLoadSave/EstimatorExtensions.cs | 65 +++++ .../DataLoadSave/TransformerChain.cs | 227 ++++++++++++++++++ 4 files changed, 560 insertions(+) create mode 100644 src/Microsoft.ML.Core/Data/IEstimator.cs create mode 100644 src/Microsoft.ML.Data/DataLoadSave/EstimatorChain.cs create mode 100644 src/Microsoft.ML.Data/DataLoadSave/EstimatorExtensions.cs create mode 100644 src/Microsoft.ML.Data/DataLoadSave/TransformerChain.cs diff --git a/src/Microsoft.ML.Core/Data/IEstimator.cs b/src/Microsoft.ML.Core/Data/IEstimator.cs new file mode 100644 index 0000000000..2d5200ed5e --- /dev/null +++ b/src/Microsoft.ML.Core/Data/IEstimator.cs @@ -0,0 +1,190 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.ML.Runtime; +using Microsoft.ML.Runtime.Data; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Microsoft.ML.Core.Data +{ + /// + /// A set of 'requirements' to the incoming schema, as well as a set of 'promises' of the outgoing schema. + /// This is more relaxed than the proper , since it's only a subset of the columns, + /// and also since it doesn't specify exact 's for vectors and keys. + /// + public sealed class SchemaShape + { + public readonly Column[] Columns; + + public sealed class Column + { + public enum VectorKind + { + Scalar, + Vector, + VariableVector + } + + public readonly string Name; + public readonly VectorKind Kind; + public readonly DataKind ItemKind; + public readonly bool IsKey; + public readonly string[] MetadataKinds; + + public Column(string name, VectorKind vecKind, DataKind itemKind, bool isKey, string[] metadataKinds) + { + Contracts.CheckNonEmpty(name, nameof(name)); + Contracts.CheckValue(metadataKinds, nameof(metadataKinds)); + + Name = name; + Kind = vecKind; + ItemKind = itemKind; + IsKey = isKey; + MetadataKinds = metadataKinds; + } + } + + public SchemaShape(Column[] columns) + { + Contracts.CheckValue(columns, nameof(columns)); + Columns = columns; + } + + /// + /// Create a schema shape out of the fully defined schema. + /// + public static SchemaShape Create(ISchema schema) + { + Contracts.CheckValue(schema, nameof(schema)); + var cols = new List(); + + for (int iCol = 0; iCol < schema.ColumnCount; iCol++) + { + if (!schema.IsHidden(iCol)) + { + Column.VectorKind vecKind; + var type = schema.GetColumnType(iCol); + if (type.IsKnownSizeVector) + vecKind = Column.VectorKind.Vector; + else if (type.IsVector) + vecKind = Column.VectorKind.VariableVector; + else + vecKind = Column.VectorKind.Scalar; + + var kind = type.ItemType.RawKind; + var isKey = type.ItemType.IsKey; + + var metadataNames = schema.GetMetadataTypes(iCol) + .Select(kvp => kvp.Key) + .ToArray(); + cols.Add(new Column(schema.GetColumnName(iCol), vecKind, kind, isKey, metadataNames)); + } + } + return new SchemaShape(cols.ToArray()); + } + + /// + /// Returns the column with a specified , and null if there is no such column. + /// + public Column FindColumn(string name) + { + Contracts.CheckValue(name, nameof(name)); + return Columns.FirstOrDefault(x => x.Name == name); + } + + // REVIEW: I think we should have an IsCompatible method to check if it's OK to use one schema shape + // as an input to another schema shape. I started writing, but realized that there's more than one way to check for + // the 'compatibility': as in, 'CAN be compatible' vs. 'WILL be compatible'. + } + + /// + /// Exception class for schema validation errors. + /// + public class SchemaException : Exception + { + } + + /// + /// The 'data reader' takes a certain kind of input and turns it into an . + /// + /// The type of input the reader takes. + public interface IDataReader + { + /// + /// Produce the data view from the specified input. + /// Note that 's are lazy, so no actual reading happens here, just schema validation. + /// + IDataView Read(TSource input); + + /// + /// The output schema of the reader. + /// + ISchema GetOutputSchema(); + } + + /// + /// Sometimes we need to 'fit' an . + /// A DataReader estimator is the object that does it. + /// + public interface IDataReaderEstimator + where TReader : IDataReader + { + /// + /// Train and return a data reader. + /// + /// REVIEW: you could consider the transformer to take a different , but we don't have such components + /// yet, so why complicate matters? + /// + TReader Fit(TSource input); + + /// + /// The 'promise' of the output schema. + /// It will be used for schema propagation. + /// + SchemaShape GetOutputSchema(); + } + + /// + /// The transformer is a component that transforms data. + /// It also supports 'schema propagation' to answer the question of 'how the data with this schema look after you transform it?'. + /// + public interface ITransformer + { + /// + /// Schema propagation for transformers. + /// Returns the output schema of the data, if the input schema is like the one provided. + /// Throws iff the input schema is not valid for the transformer. + /// + ISchema GetOutputSchema(ISchema inputSchema); + + /// + /// Take the data in, make transformations, output the data. + /// Note that 's are lazy, so no actual transformations happen here, just schema validation. + /// + IDataView Transform(IDataView input); + } + + /// + /// The estimator (in Spark terminology) is an 'untrained transformer'. It needs to 'fit' on the data to manufacture + /// a transformer. + /// It also provides the 'schema propagation' like transformers do, but over instead of . + /// + public interface IEstimator + where TTransformer : ITransformer + { + /// + /// Train and return a transformer. + /// + TTransformer Fit(IDataView input); + + /// + /// Schema propagation for estimators. + /// Returns the output schema shape of the estimator, if the input schema shape is like the one provided. + /// Throws iff the input schema is not valid for the estimator. + /// + SchemaShape GetOutputSchema(SchemaShape inputSchema); + } +} diff --git a/src/Microsoft.ML.Data/DataLoadSave/EstimatorChain.cs b/src/Microsoft.ML.Data/DataLoadSave/EstimatorChain.cs new file mode 100644 index 0000000000..727dfa9e0b --- /dev/null +++ b/src/Microsoft.ML.Data/DataLoadSave/EstimatorChain.cs @@ -0,0 +1,78 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.ML.Core.Data; +using Microsoft.ML.Runtime.Internal.Utilities; +using System.Linq; + +namespace Microsoft.ML.Runtime.Data +{ + /// + /// Represents a chain (potentially empty) of estimators that end with a . + /// If the chain is empty, is always . + /// + public sealed class EstimatorChain : IEstimator> + where TLastTransformer : class, ITransformer + { + private readonly TransformerScope[] _scopes; + private readonly IEstimator[] _estimators; + public readonly IEstimator LastEstimator; + + private EstimatorChain(IEstimator[] estimators, TransformerScope[] scopes) + { + Contracts.AssertValueOrNull(estimators); + Contracts.AssertValueOrNull(scopes); + Contracts.Assert(Utils.Size(estimators) == Utils.Size(scopes)); + + _estimators = estimators ?? new IEstimator[0]; + _scopes = scopes ?? new TransformerScope[0]; + LastEstimator = estimators.LastOrDefault() as IEstimator; + + Contracts.Assert((_estimators.Length > 0) == (LastEstimator != null)); + } + + /// + /// Create an empty estimator chain. + /// + public EstimatorChain() + { + _estimators = new IEstimator[0]; + _scopes = new TransformerScope[0]; + LastEstimator = null; + } + + public TransformerChain Fit(IDataView input) + { + // REVIEW: before fitting, run schema propagation. + // Currently, it throws. + // GetOutputSchema(SchemaShape.Create(input.Schema); + + IDataView current = input; + var xfs = new ITransformer[_estimators.Length]; + for (int i = 0; i < _estimators.Length; i++) + { + var est = _estimators[i]; + xfs[i] = est.Fit(current); + current = xfs[i].Transform(current); + } + + return new TransformerChain(xfs, _scopes); + } + + public SchemaShape GetOutputSchema(SchemaShape inputSchema) + { + var s = inputSchema; + foreach (var est in _estimators) + s = est.GetOutputSchema(s); + return s; + } + + public EstimatorChain Append(IEstimator estimator, TransformerScope scope = TransformerScope.Everything) + where TNewTrans : class, ITransformer + { + Contracts.CheckValue(estimator, nameof(estimator)); + return new EstimatorChain(_estimators.Append(estimator).ToArray(), _scopes.Append(scope).ToArray()); + } + } +} diff --git a/src/Microsoft.ML.Data/DataLoadSave/EstimatorExtensions.cs b/src/Microsoft.ML.Data/DataLoadSave/EstimatorExtensions.cs new file mode 100644 index 0000000000..d862c69028 --- /dev/null +++ b/src/Microsoft.ML.Data/DataLoadSave/EstimatorExtensions.cs @@ -0,0 +1,65 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.ML.Core.Data; + +namespace Microsoft.ML.Runtime.Data +{ + /// + /// Extension methods that allow chaining estimator and transformer pipes together. + /// + public static class LearningPipelineExtensions + { + /// + /// Create a composite reader estimator by appending an estimator to a reader estimator. + /// + public static CompositeReaderEstimator Append( + this IDataReaderEstimator> start, IEstimator estimator) + where TTrans : class, ITransformer + { + Contracts.CheckValue(start, nameof(start)); + Contracts.CheckValue(estimator, nameof(estimator)); + + return new CompositeReaderEstimator(start).Append(estimator); + } + + /// + /// Create an estimator chain by appending an estimator to an estimator. + /// + public static EstimatorChain Append( + this IEstimator start, IEstimator estimator, + TransformerScope scope = TransformerScope.Everything) + where TTrans : class, ITransformer + { + Contracts.CheckValue(start, nameof(start)); + Contracts.CheckValue(estimator, nameof(estimator)); + + return new EstimatorChain().Append(start).Append(estimator, scope); + } + + /// + /// Create a composite reader by appending a transformer to a data reader. + /// + public static CompositeDataReader Append(this IDataReader reader, TTrans transformer) + where TTrans : class, ITransformer + { + Contracts.CheckValue(reader, nameof(reader)); + Contracts.CheckValue(transformer, nameof(transformer)); + + return new CompositeDataReader(reader).AppendTransformer(transformer); + } + + /// + /// Create a transformer chain by appending a transformer to a transformer. + /// + public static TransformerChain Append(this ITransformer start, TTrans transformer) + where TTrans : class, ITransformer + { + Contracts.CheckValue(start, nameof(start)); + Contracts.CheckValue(transformer, nameof(transformer)); + + return new TransformerChain(start, transformer); + } + } +} diff --git a/src/Microsoft.ML.Data/DataLoadSave/TransformerChain.cs b/src/Microsoft.ML.Data/DataLoadSave/TransformerChain.cs new file mode 100644 index 0000000000..d5f246689a --- /dev/null +++ b/src/Microsoft.ML.Data/DataLoadSave/TransformerChain.cs @@ -0,0 +1,227 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.ML.Core.Data; +using Microsoft.ML.Runtime; +using Microsoft.ML.Runtime.Data; +using Microsoft.ML.Runtime.Internal.Utilities; +using Microsoft.ML.Runtime.Model; +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +[assembly: LoadableClass(typeof(TransformerChain), typeof(TransformerChain), null, typeof(SignatureLoadModel), + "Transformer chain", TransformerChain.LoaderSignature)] + +namespace Microsoft.ML.Runtime.Data +{ + /// + /// This enum allows for 'tagging' the estimators (and subsequently transformers) in the chain to be used + /// 'only for training', 'for training and evaluation' etc. + /// Most notable example is, transformations over the label column should not be used for scoring, so the scope + /// should be or . + /// + [Flags] + public enum TransformerScope + { + None = 0, + Training = 1 << 0, + Testing = 1 << 1, + Scoring = 1 << 2, + TrainTest = Training | Testing, + Everything = Training | Testing | Scoring + } + + /// + /// A chain of transformers (possibly empty) that end with a . + /// For an empty chain, is always . + /// + public sealed class TransformerChain : ITransformer, ICanSaveModel, IEnumerable + where TLastTransformer : class, ITransformer + { + private readonly ITransformer[] _transformers; + private readonly TransformerScope[] _scopes; + public readonly TLastTransformer LastTransformer; + + private const string TransformDirTemplate = "Transform_{0:000}"; + + private static VersionInfo GetVersionInfo() + { + return new VersionInfo( + modelSignature: "XF CHAIN", + verWrittenCur: 0x00010001, // Initial + verReadableCur: 0x00010001, + verWeCanReadBack: 0x00010001, + loaderSignature: TransformerChain.LoaderSignature); + } + + /// + /// Create a transformer chain by specifying transformers and their scopes. + /// + /// Transformers to be chained. + /// Transformer scopes, parallel to . + public TransformerChain(IEnumerable transformers, IEnumerable scopes) + { + Contracts.CheckValueOrNull(transformers); + Contracts.CheckValueOrNull(scopes); + + _transformers = transformers?.ToArray() ?? new ITransformer[0]; + _scopes = scopes?.ToArray() ?? new TransformerScope[0]; + LastTransformer = transformers.LastOrDefault() as TLastTransformer; + + Contracts.Check((_transformers.Length > 0) == (LastTransformer != null)); + Contracts.Check(_transformers.Length == _scopes.Length); + } + + /// + /// Create a transformer chain by specifying all the transformers. The scopes are assumed to be + /// . + /// + /// + public TransformerChain(params ITransformer[] transformers) + { + Contracts.CheckValueOrNull(transformers); + + if (Utils.Size(transformers) == 0) + { + _transformers = new ITransformer[0]; + _scopes = new TransformerScope[0]; + LastTransformer = null; + } + else + { + _transformers = transformers.ToArray(); + _scopes = transformers.Select(x => TransformerScope.Everything).ToArray(); + LastTransformer = transformers.Last() as TLastTransformer; + Contracts.Check(LastTransformer != null); + } + } + + public ISchema GetOutputSchema(ISchema inputSchema) + { + Contracts.CheckValue(inputSchema, nameof(inputSchema)); + + var s = inputSchema; + foreach (var xf in _transformers) + s = xf.GetOutputSchema(s); + return s; + } + + public IDataView Transform(IDataView input) + { + Contracts.CheckValue(input, nameof(input)); + + // Trigger schema propagation prior to transforming. + // REVIEW: does this actually constitute 'early warning', given that Transform call is lazy anyway? + GetOutputSchema(input.Schema); + + var dv = input; + foreach (var xf in _transformers) + dv = xf.Transform(dv); + return dv; + } + + public TransformerChain GetModelFor(TransformerScope scopeFilter) + { + var xfs = new List(); + var scopes = new List(); + for (int i = 0; i < _transformers.Length; i++) + { + if ((_scopes[i] & scopeFilter) != TransformerScope.None) + { + xfs.Add(_transformers[i]); + scopes.Add(_scopes[i]); + } + } + return new TransformerChain(xfs.ToArray(), scopes.ToArray()); + } + + public TransformerChain Append(TNewLast transformer, TransformerScope scope = TransformerScope.Everything) + where TNewLast : class, ITransformer + { + Contracts.CheckValue(transformer, nameof(transformer)); + return new TransformerChain(_transformers.Append(transformer).ToArray(), _scopes.Append(scope).ToArray()); + } + + public void Save(ModelSaveContext ctx) + { + ctx.CheckAtModel(); + ctx.SetVersionInfo(GetVersionInfo()); + + ctx.Writer.Write(_transformers.Length); + + for (int i = 0; i < _transformers.Length; i++) + { + ctx.Writer.Write((int)_scopes[i]); + var dirName = string.Format(TransformDirTemplate, i); + ctx.SaveModel(_transformers[i], dirName); + } + } + + /// + /// The loading constructor of transformer chain. Reverse of . + /// + internal TransformerChain(IHostEnvironment env, ModelLoadContext ctx) + { + int len = ctx.Reader.ReadInt32(); + _transformers = new ITransformer[len]; + _scopes = new TransformerScope[len]; + for (int i = 0; i < len; i++) + { + _scopes[i] = (TransformerScope)(ctx.Reader.ReadInt32()); + var dirName = string.Format(TransformDirTemplate, i); + ctx.LoadModel(env, out _transformers[i], dirName); + } + if (len > 0) + LastTransformer = _transformers[len - 1] as TLastTransformer; + else + LastTransformer = null; + } + + public void SaveTo(IHostEnvironment env, Stream outputStream) + { + using (var ch = env.Start("Saving pipeline")) + { + using (var rep = RepositoryWriter.CreateNew(outputStream, ch)) + { + ch.Trace("Saving transformer chain"); + ModelSaveContext.SaveModel(rep, this, TransformerChain.LoaderSignature); + rep.Commit(); + } + } + } + + public IEnumerator GetEnumerator() => ((IEnumerable)_transformers).GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + + /// + /// Saving/loading routines for transformer chains. + /// + public static class TransformerChain + { + public const string LoaderSignature = "TransformerChain"; + + public static TransformerChain Create(IHostEnvironment env, ModelLoadContext ctx) + => new TransformerChain(env, ctx); + + /// + /// Save any transformer to a stream by wrapping it into a transformer chain. + /// + public static void SaveTo(this ITransformer transformer, IHostEnvironment env, Stream outputStream) + => new TransformerChain(transformer).SaveTo(env, outputStream); + + public static TransformerChain LoadFrom(IHostEnvironment env, Stream stream) + { + using (var rep = RepositoryReader.Open(stream, env)) + { + ModelLoadContext.LoadModel, SignatureLoadModel>(env, out var transformerChain, rep, LoaderSignature); + return transformerChain; + } + } + } +} From e8bb0f37b674e96e2386ba4154741ce1bbf7d48d Mon Sep 17 00:00:00 2001 From: Pete Luferenko Date: Tue, 21 Aug 2018 10:29:28 -0700 Subject: [PATCH 2/8] Temporary commit --- .../DataLoadSave/EstimatorExtensions.cs | 65 ------------- .../Prediction/IPredictionTransformer.cs | 31 ++++++ .../Training/ITrainerEstimator.cs | 14 +++ .../Training/TrainerEstimatorBase.cs | 94 +++++++++++++++++++ 4 files changed, 139 insertions(+), 65 deletions(-) delete mode 100644 src/Microsoft.ML.Data/DataLoadSave/EstimatorExtensions.cs create mode 100644 src/Microsoft.ML.Data/Prediction/IPredictionTransformer.cs create mode 100644 src/Microsoft.ML.Data/Training/ITrainerEstimator.cs create mode 100644 src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs diff --git a/src/Microsoft.ML.Data/DataLoadSave/EstimatorExtensions.cs b/src/Microsoft.ML.Data/DataLoadSave/EstimatorExtensions.cs deleted file mode 100644 index d862c69028..0000000000 --- a/src/Microsoft.ML.Data/DataLoadSave/EstimatorExtensions.cs +++ /dev/null @@ -1,65 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Microsoft.ML.Core.Data; - -namespace Microsoft.ML.Runtime.Data -{ - /// - /// Extension methods that allow chaining estimator and transformer pipes together. - /// - public static class LearningPipelineExtensions - { - /// - /// Create a composite reader estimator by appending an estimator to a reader estimator. - /// - public static CompositeReaderEstimator Append( - this IDataReaderEstimator> start, IEstimator estimator) - where TTrans : class, ITransformer - { - Contracts.CheckValue(start, nameof(start)); - Contracts.CheckValue(estimator, nameof(estimator)); - - return new CompositeReaderEstimator(start).Append(estimator); - } - - /// - /// Create an estimator chain by appending an estimator to an estimator. - /// - public static EstimatorChain Append( - this IEstimator start, IEstimator estimator, - TransformerScope scope = TransformerScope.Everything) - where TTrans : class, ITransformer - { - Contracts.CheckValue(start, nameof(start)); - Contracts.CheckValue(estimator, nameof(estimator)); - - return new EstimatorChain().Append(start).Append(estimator, scope); - } - - /// - /// Create a composite reader by appending a transformer to a data reader. - /// - public static CompositeDataReader Append(this IDataReader reader, TTrans transformer) - where TTrans : class, ITransformer - { - Contracts.CheckValue(reader, nameof(reader)); - Contracts.CheckValue(transformer, nameof(transformer)); - - return new CompositeDataReader(reader).AppendTransformer(transformer); - } - - /// - /// Create a transformer chain by appending a transformer to a transformer. - /// - public static TransformerChain Append(this ITransformer start, TTrans transformer) - where TTrans : class, ITransformer - { - Contracts.CheckValue(start, nameof(start)); - Contracts.CheckValue(transformer, nameof(transformer)); - - return new TransformerChain(start, transformer); - } - } -} diff --git a/src/Microsoft.ML.Data/Prediction/IPredictionTransformer.cs b/src/Microsoft.ML.Data/Prediction/IPredictionTransformer.cs new file mode 100644 index 0000000000..9be9c7c753 --- /dev/null +++ b/src/Microsoft.ML.Data/Prediction/IPredictionTransformer.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.ML.Core.Data; +using Microsoft.ML.Runtime.Data; +using Microsoft.ML.Runtime.Internal.Calibration; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.ML.Runtime +{ + public interface IPredictionTransformer : ITransformer + where TModel : IPredictor + { + string FeatureColumn { get; } + + ColumnType FeatureColumnType { get; } + + TModel Model { get; } + } + + public interface ICalibratedBinaryPredictor + where TCalibrator : ICalibrator + where TModel : IPredictorProducing + { + TCalibrator Calibrator { get; } + TModel Model { get; } + } +} diff --git a/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs b/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs new file mode 100644 index 0000000000..02457434b6 --- /dev/null +++ b/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs @@ -0,0 +1,14 @@ +using Microsoft.ML.Core.Data; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.ML.Runtime.Training +{ + public interface ITrainerEstimator: IEstimator + where TTransformer: IPredictionTransformer + where TPredictor: IPredictor + { + TrainerInfo TrainerInfo { get; } + } +} diff --git a/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs b/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs new file mode 100644 index 0000000000..f77def382c --- /dev/null +++ b/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.ML.Core.Data; +using Microsoft.ML.Runtime.Data; + +namespace Microsoft.ML.Runtime.Training +{ + /// + /// This represents a basic class for 'simple trainer'. + /// A 'simple trainer' accepts one feature column and one label column, also optionally a weight column. + /// It produces a 'prediction transformer'. + /// + /// + /// + public abstract class TrainerEstimatorBase : ITrainerEstimator + where TTransformer : IPredictionTransformer + where TModel : IPredictor + { + /// + /// The feature column that the trainer expects. + /// + public readonly SchemaShape.Column FeatureColumn; + + /// + /// The label column that the trainer expects. Can be null, which indicates that label + /// is not used for training. + /// + public readonly SchemaShape.Column LabelColumn; + + /// + /// The weight column that the trainer expects. Can be null, which indicates that weight is + /// not used for training. + /// + public readonly SchemaShape.Column WeightColumn; + + protected readonly IHost Host; + + /// + /// The information about the trainer: whether it benefits from normalization, caching etc. + /// + public TrainerInfo TrainerInfo { get; } + + public TrainerEstimatorBase(IHost host, + SchemaShape.Column feature, SchemaShape.Column label, SchemaShape.Column weight = null) + { + Contracts.CheckValue(host, nameof(host)); + Host = host; + Host.CheckValue(feature, nameof(feature)); + Host.CheckValueOrNull(label); + Host.CheckValueOrNull(weight); + + FeatureColumn = feature; + LabelColumn = label; + WeightColumn = weight; + } + + protected TTransformer TrainTransformer(IDataView trainSet, + IDataView validationSet = null, IPredictor initPredictor = null) + { + var cachedTrain = TrainerInfo.WantCaching ? new CacheDataView(Host, trainSet, prefetch: null) : trainSet; + + var trainRoles = MakeRoles(cachedTrain); + + RoleMappedData validRoles; + + if (validationSet == null) + validRoles = null; + else + { + var cachedValid = TrainerInfo.WantCaching ? new CacheDataView(Host, validationSet, prefetch: null) : validationSet; + validRoles = MakeRoles(cachedValid); + } + + var pred = TrainModelCore(new TrainContext(trainRoles, validRoles, initPredictor)); + + var emptyData = new EmptyDataView(Host, trainSet.Schema); + var scoreRoles = MakeRoles(emptyData); + return MakeTransformer(pred); + } + + public SchemaShape GetOutputSchema(SchemaShape inputSchema) + { + throw new NotImplementedException(); + } + + protected abstract TModel TrainModelCore(TrainContext trainContext); + + protected abstract TTransformer MakeTransformer(TModel model); + + private RoleMappedData MakeRoles(IDataView data) => + new RoleMappedData(data, label: LabelColumn.Name, feature: FeatureColumn.Name, weight: WeightColumn.Name); + } +} From 777dcb0f2c6cbd1283a084d963d14dcfb8e97b83 Mon Sep 17 00:00:00 2001 From: Pete Luferenko Date: Wed, 22 Aug 2018 14:31:36 -0700 Subject: [PATCH 3/8] First working version (still some tests need fixing) --- src/Microsoft.ML.Core/Data/IEstimator.cs | 46 +- src/Microsoft.ML.Core/Utilities/Contracts.cs | 960 +++++++++--------- .../Prediction/IPredictionTransformer.cs | 10 +- .../Scorers/PredictionTransformer.cs | 279 +++++ .../Training/ITrainerEstimator.cs | 6 +- .../Training/TrainerEstimatorBase.cs | 104 +- .../Standard/LinearClassificationTrainer.cs | 87 +- .../Standard/SdcaMultiClass.cs | 59 +- .../Standard/SdcaRegression.cs | 43 +- .../Standard/StochasticTrainerBase.cs | 101 ++ .../Api/Estimators/CrossValidation.cs | 2 +- .../Estimators/DecomposableTrainAndPredict.cs | 2 +- .../Scenarios/Api/Estimators/Evaluation.cs | 2 +- .../Scenarios/Api/Estimators/Extensibility.cs | 2 +- .../Api/Estimators/FileBasedSavingOfData.cs | 5 +- .../Api/Estimators/IntrospectiveTraining.cs | 4 +- .../Api/Estimators/Metacomponents.cs | 2 +- .../Api/Estimators/MultithreadedPrediction.cs | 2 +- .../Estimators/ReconfigurablePrediction.cs | 5 +- .../Api/Estimators/SimpleTrainAndPredict.cs | 2 +- .../Estimators/TrainSaveModelAndPredict.cs | 2 +- .../Estimators/TrainWithInitialPredictor.cs | 4 +- .../Api/Estimators/TrainWithValidationSet.cs | 3 +- .../Scenarios/Api/Estimators/Wrappers.cs | 85 +- .../Scenarios/Api/ReconfigurablePrediction.cs | 9 +- 25 files changed, 1206 insertions(+), 620 deletions(-) create mode 100644 src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs create mode 100644 src/Microsoft.ML.StandardLearners/Standard/StochasticTrainerBase.cs diff --git a/src/Microsoft.ML.Core/Data/IEstimator.cs b/src/Microsoft.ML.Core/Data/IEstimator.cs index 2d5200ed5e..6f21a1cb01 100644 --- a/src/Microsoft.ML.Core/Data/IEstimator.cs +++ b/src/Microsoft.ML.Core/Data/IEstimator.cs @@ -34,23 +34,59 @@ public enum VectorKind public readonly bool IsKey; public readonly string[] MetadataKinds; - public Column(string name, VectorKind vecKind, DataKind itemKind, bool isKey, string[] metadataKinds) + public Column(string name, VectorKind vecKind, DataKind itemKind, bool isKey, string[] metadataKinds = null) { Contracts.CheckNonEmpty(name, nameof(name)); - Contracts.CheckValue(metadataKinds, nameof(metadataKinds)); + Contracts.CheckValueOrNull(metadataKinds); Name = name; Kind = vecKind; ItemKind = itemKind; IsKey = isKey; - MetadataKinds = metadataKinds; + MetadataKinds = metadataKinds ?? new string[0]; + } + + /// + /// Returns whether is a valid input, if this object represents a + /// requirement. + /// + /// Namely, it returns true iff: + /// - The , , , fields match. + /// - The of is a superset of our . + /// + public bool IsCompatibleWith(Column inputColumn) + { + Contracts.CheckValue(inputColumn, nameof(inputColumn)); + if (Name != inputColumn.Name) + return false; + if (Kind != inputColumn.Kind) + return false; + if (ItemKind != inputColumn.ItemKind) + return false; + if (IsKey != inputColumn.IsKey) + return false; + if (inputColumn.MetadataKinds.Except(MetadataKinds).Any()) + return false; + return true; + } + + public string GetTypeString() + { + string result = ItemKind.ToString(); + if (IsKey) + result = $"Key<{result}>"; + if (Kind == VectorKind.Vector) + result = $"Vector<{result}>"; + else if (Kind == VectorKind.VariableVector) + result = $"VarVector<{result}>"; + return result; } } - public SchemaShape(Column[] columns) + public SchemaShape(IEnumerable columns) { Contracts.CheckValue(columns, nameof(columns)); - Columns = columns; + Columns = columns.ToArray(); } /// diff --git a/src/Microsoft.ML.Core/Utilities/Contracts.cs b/src/Microsoft.ML.Core/Utilities/Contracts.cs index 757b99f75d..1518120f84 100644 --- a/src/Microsoft.ML.Core/Utilities/Contracts.cs +++ b/src/Microsoft.ML.Core/Utilities/Contracts.cs @@ -22,6 +22,7 @@ namespace Microsoft.ML.Runtime.Internal namespace Microsoft.ML.Runtime #endif { + using System.Runtime.Serialization; using Conditional = System.Diagnostics.ConditionalAttribute; using Debug = System.Diagnostics.Debug; @@ -284,9 +285,9 @@ public static Exception Except(this IExceptionContext ctx, Exception inner, stri /// For signalling bad user input. /// public static Exception ExceptUserArg(string name) - =>Process(new ArgumentOutOfRangeException(name)); + => Process(new ArgumentOutOfRangeException(name)); public static Exception ExceptUserArg(this IExceptionContext ctx, string name) - =>Process(new ArgumentOutOfRangeException(name), ctx); + => Process(new ArgumentOutOfRangeException(name), ctx); public static Exception ExceptUserArg(string name, string msg) => Process(new ArgumentOutOfRangeException(name, msg)); public static Exception ExceptUserArg(this IExceptionContext ctx, string name, string msg) @@ -448,274 +449,293 @@ public static Exception ExceptNotSupp(string msg, params object[] args) public static Exception ExceptNotSupp(this IExceptionContext ctx, string msg, params object[] args) => Process(new NotSupportedException(GetMsg(msg, args)), ctx); - // Check - these check a condition and if it fails, throw the corresponding exception. - // NOTE: The ordering of arguments to these is standardized to be: - // * boolean condition - // * parameter name - // * parameter value - // * message string - // - // Note that these do NOT support a params array of arguments since that would - // involve memory allocation whenever the condition is checked. When message string - // args are need, the condition test should be inlined, eg: - // if (!condition) - // throw Contracts.ExceptXxx(fmt, arg1, arg2); - - public static void Check(bool f) - { - if (!f) - throw Except(); - } - public static void Check(this IExceptionContext ctx, bool f) - { - if (!f) - throw Except(ctx); - } - public static void Check(bool f, string msg) - { - if (!f) - throw Except(msg); - } - public static void Check(this IExceptionContext ctx, bool f, string msg) - { - if (!f) - throw Except(ctx, msg); - } - /// - /// CheckUserArg / ExceptUserArg should be used when the validation of user-provided arguments failed. - /// Typically, this is shortly after the arguments are parsed using CmdParser. + /// For signalling schema validation issues. /// - public static void CheckUserArg(bool f, string name) - { - if (!f) - throw ExceptUserArg(name); - } - public static void CheckUserArg(this IExceptionContext ctx, bool f, string name) - { - if (!f) - throw ExceptUserArg(ctx, name); - } - public static void CheckUserArg(bool f, string name, string msg) - { - if (!f) - throw ExceptUserArg(name, msg); - } - public static void CheckUserArg(this IExceptionContext ctx, bool f, string name, string msg) - { - if (!f) - throw ExceptUserArg(ctx, name, msg); - } + public static Exception ExceptSchemaMismatch(string paramName, string columnRole, string columnName) + => Process(new ArgumentOutOfRangeException(paramName, MakeSchemaMismatchMsg(columnRole, columnName))); + public static Exception ExceptSchemaMismatch(this IExceptionContext ctx, string paramName, string columnRole, string columnName) + => Process(new ArgumentOutOfRangeException(paramName, MakeSchemaMismatchMsg(columnRole, columnName)), ctx); + public static Exception ExceptSchemaMismatch(string paramName, string columnRole, string columnName, string expectedType, string actualType) + => Process(new ArgumentOutOfRangeException(paramName, MakeSchemaMismatchMsg(columnRole, columnName, expectedType, actualType))); + public static Exception ExceptSchemaMismatch(this IExceptionContext ctx, string paramName, string columnRole, string columnName, string expectedType, string actualType) + => Process(new ArgumentOutOfRangeException(paramName, MakeSchemaMismatchMsg(columnRole, columnName, expectedType, actualType)), ctx); + + private static string MakeSchemaMismatchMsg(string columnRole, string columnName, string expectedType = null, string actualType = null) + { + if (actualType == null) + return $"Could not find {columnRole} column '{columnName}'"; + return $"Schema mismatch for {columnRole} column '{columnName}': expected {expectedType}, got {actualType}"; + } + + // Check - these check a condition and if it fails, throw the corresponding exception. + // NOTE: The ordering of arguments to these is standardized to be: + // * boolean condition + // * parameter name + // * parameter value + // * message string + // + // Note that these do NOT support a params array of arguments since that would + // involve memory allocation whenever the condition is checked. When message string + // args are need, the condition test should be inlined, eg: + // if (!condition) + // throw Contracts.ExceptXxx(fmt, arg1, arg2); + + public static void Check(bool f) + { + if (!f) + throw Except(); + } + public static void Check(this IExceptionContext ctx, bool f) + { + if (!f) + throw Except(ctx); + } + public static void Check(bool f, string msg) + { + if (!f) + throw Except(msg); + } + public static void Check(this IExceptionContext ctx, bool f, string msg) + { + if (!f) + throw Except(ctx, msg); + } - public static void CheckParam(bool f, string paramName) - { - if (!f) - throw ExceptParam(paramName); - } - public static void CheckParam(this IExceptionContext ctx, bool f, string paramName) - { - if (!f) - throw ExceptParam(ctx, paramName); - } - public static void CheckParam(bool f, string paramName, string msg) - { - if (!f) - throw ExceptParam(paramName, msg); - } - public static void CheckParam(this IExceptionContext ctx, bool f, string paramName, string msg) - { - if (!f) - throw ExceptParam(ctx, paramName, msg); - } - public static void CheckParamValue(bool f, T value, string paramName, string msg) - { - if (!f) - throw ExceptParamValue(value, paramName, msg); - } - public static void CheckParamValue(this IExceptionContext ctx, bool f, T value, string paramName, string msg) - { - if (!f) - throw ExceptParamValue(ctx, value, paramName, msg); - } + /// + /// CheckUserArg / ExceptUserArg should be used when the validation of user-provided arguments failed. + /// Typically, this is shortly after the arguments are parsed using CmdParser. + /// + public static void CheckUserArg(bool f, string name) + { + if (!f) + throw ExceptUserArg(name); + } + public static void CheckUserArg(this IExceptionContext ctx, bool f, string name) + { + if (!f) + throw ExceptUserArg(ctx, name); + } + public static void CheckUserArg(bool f, string name, string msg) + { + if (!f) + throw ExceptUserArg(name, msg); + } + public static void CheckUserArg(this IExceptionContext ctx, bool f, string name, string msg) + { + if (!f) + throw ExceptUserArg(ctx, name, msg); + } - public static T CheckRef(T val, string paramName) where T : class - { - if (object.ReferenceEquals(val, null)) - throw ExceptValue(paramName); - return val; - } - public static T CheckRef(this IExceptionContext ctx, T val, string paramName) where T : class - { - if (object.ReferenceEquals(val, null)) - throw ExceptValue(ctx, paramName); - return val; - } + public static void CheckParam(bool f, string paramName) + { + if (!f) + throw ExceptParam(paramName); + } + public static void CheckParam(this IExceptionContext ctx, bool f, string paramName) + { + if (!f) + throw ExceptParam(ctx, paramName); + } + public static void CheckParam(bool f, string paramName, string msg) + { + if (!f) + throw ExceptParam(paramName, msg); + } + public static void CheckParam(this IExceptionContext ctx, bool f, string paramName, string msg) + { + if (!f) + throw ExceptParam(ctx, paramName, msg); + } + public static void CheckParamValue(bool f, T value, string paramName, string msg) + { + if (!f) + throw ExceptParamValue(value, paramName, msg); + } + public static void CheckParamValue(this IExceptionContext ctx, bool f, T value, string paramName, string msg) + { + if (!f) + throw ExceptParamValue(ctx, value, paramName, msg); + } - public static void CheckValue(T val, string paramName) where T : class - { - if (object.ReferenceEquals(val, null)) - throw ExceptValue(paramName); - } - public static void CheckValue(this IExceptionContext ctx, T val, string paramName) where T : class - { - if (object.ReferenceEquals(val, null)) - throw ExceptValue(ctx, paramName); - } - public static T CheckValue(T val, string paramName, string msg) where T : class - { - if (object.ReferenceEquals(val, null)) - throw ExceptValue(paramName, msg); - return val; - } - public static T CheckValue(this IExceptionContext ctx, T val, string paramName, string msg) where T : class - { - if (object.ReferenceEquals(val, null)) - throw ExceptValue(ctx, paramName, msg); - return val; - } + public static T CheckRef(T val, string paramName) where T : class + { + if (object.ReferenceEquals(val, null)) + throw ExceptValue(paramName); + return val; + } + public static T CheckRef(this IExceptionContext ctx, T val, string paramName) where T : class + { + if (object.ReferenceEquals(val, null)) + throw ExceptValue(ctx, paramName); + return val; + } - public static string CheckNonEmpty(string s, string paramName) - { - if (string.IsNullOrEmpty(s)) - throw ExceptEmpty(paramName); - return s; - } - public static string CheckNonEmpty(this IExceptionContext ctx, string s, string paramName) - { - if (string.IsNullOrEmpty(s)) - throw ExceptEmpty(ctx, paramName); - return s; - } + public static void CheckValue(T val, string paramName) where T : class + { + if (object.ReferenceEquals(val, null)) + throw ExceptValue(paramName); + } + public static void CheckValue(this IExceptionContext ctx, T val, string paramName) where T : class + { + if (object.ReferenceEquals(val, null)) + throw ExceptValue(ctx, paramName); + } + public static T CheckValue(T val, string paramName, string msg) where T : class + { + if (object.ReferenceEquals(val, null)) + throw ExceptValue(paramName, msg); + return val; + } + public static T CheckValue(this IExceptionContext ctx, T val, string paramName, string msg) where T : class + { + if (object.ReferenceEquals(val, null)) + throw ExceptValue(ctx, paramName, msg); + return val; + } - public static string CheckNonWhiteSpace(string s, string paramName) - { - if (string.IsNullOrWhiteSpace(s)) - throw ExceptWhiteSpace(paramName); - return s; - } - public static string CheckNonWhiteSpace(this IExceptionContext ctx, string s, string paramName) - { - if (string.IsNullOrWhiteSpace(s)) - throw ExceptWhiteSpace(ctx, paramName); - return s; - } + public static string CheckNonEmpty(string s, string paramName) + { + if (string.IsNullOrEmpty(s)) + throw ExceptEmpty(paramName); + return s; + } + public static string CheckNonEmpty(this IExceptionContext ctx, string s, string paramName) + { + if (string.IsNullOrEmpty(s)) + throw ExceptEmpty(ctx, paramName); + return s; + } - public static string CheckNonEmpty(string s, string paramName, string msg) - { - if (string.IsNullOrEmpty(s)) - throw ExceptEmpty(paramName, msg); - return s; - } - public static string CheckNonEmpty(this IExceptionContext ctx, string s, string paramName, string msg) - { - if (string.IsNullOrEmpty(s)) - throw ExceptEmpty(ctx, paramName, msg); - return s; - } + public static string CheckNonWhiteSpace(string s, string paramName) + { + if (string.IsNullOrWhiteSpace(s)) + throw ExceptWhiteSpace(paramName); + return s; + } + public static string CheckNonWhiteSpace(this IExceptionContext ctx, string s, string paramName) + { + if (string.IsNullOrWhiteSpace(s)) + throw ExceptWhiteSpace(ctx, paramName); + return s; + } - public static string CheckNonWhiteSpace(string s, string paramName, string msg) - { - if (string.IsNullOrWhiteSpace(s)) - throw ExceptWhiteSpace(paramName, msg); - return s; - } - public static string CheckNonWhiteSpace(this IExceptionContext ctx, string s, string paramName, string msg) - { - if (string.IsNullOrWhiteSpace(s)) - throw ExceptWhiteSpace(ctx, paramName, msg); - return s; - } + public static string CheckNonEmpty(string s, string paramName, string msg) + { + if (string.IsNullOrEmpty(s)) + throw ExceptEmpty(paramName, msg); + return s; + } + public static string CheckNonEmpty(this IExceptionContext ctx, string s, string paramName, string msg) + { + if (string.IsNullOrEmpty(s)) + throw ExceptEmpty(ctx, paramName, msg); + return s; + } - public static T[] CheckNonEmpty(T[] args, string paramName) - { - if (Size(args) == 0) - throw ExceptEmpty(paramName); - return args; - } - public static T[] CheckNonEmpty(this IExceptionContext ctx, T[] args, string paramName) - { - if (Size(args) == 0) - throw ExceptEmpty(ctx, paramName); - return args; - } - public static T[] CheckNonEmpty(T[] args, string paramName, string msg) - { - if (Size(args) == 0) - throw ExceptEmpty(paramName, msg); - return args; - } - public static T[] CheckNonEmpty(this IExceptionContext ctx, T[] args, string paramName, string msg) - { - if (Size(args) == 0) - throw ExceptEmpty(ctx, paramName, msg); - return args; - } - public static ICollection CheckNonEmpty(ICollection args, string paramName) - { - if (Size(args) == 0) - throw ExceptEmpty(paramName); - return args; - } - public static ICollection CheckNonEmpty(this IExceptionContext ctx, ICollection args, string paramName) - { - if (Size(args) == 0) - throw ExceptEmpty(ctx, paramName); - return args; - } - public static ICollection CheckNonEmpty(ICollection args, string paramName, string msg) - { - if (Size(args) == 0) - throw ExceptEmpty(paramName, msg); - return args; - } - public static ICollection CheckNonEmpty(this IExceptionContext ctx, ICollection args, string paramName, string msg) - { - if (Size(args) == 0) - throw ExceptEmpty(ctx, paramName, msg); - return args; - } + public static string CheckNonWhiteSpace(string s, string paramName, string msg) + { + if (string.IsNullOrWhiteSpace(s)) + throw ExceptWhiteSpace(paramName, msg); + return s; + } + public static string CheckNonWhiteSpace(this IExceptionContext ctx, string s, string paramName, string msg) + { + if (string.IsNullOrWhiteSpace(s)) + throw ExceptWhiteSpace(ctx, paramName, msg); + return s; + } - public static void CheckDecode(bool f) - { - if (!f) - throw ExceptDecode(); - } - public static void CheckDecode(this IExceptionContext ctx, bool f) - { - if (!f) - throw ExceptDecode(ctx); - } - public static void CheckDecode(bool f, string msg) - { - if (!f) - throw ExceptDecode(msg); - } - public static void CheckDecode(this IExceptionContext ctx, bool f, string msg) - { - if (!f) - throw ExceptDecode(ctx, msg); - } + public static T[] CheckNonEmpty(T[] args, string paramName) + { + if (Size(args) == 0) + throw ExceptEmpty(paramName); + return args; + } + public static T[] CheckNonEmpty(this IExceptionContext ctx, T[] args, string paramName) + { + if (Size(args) == 0) + throw ExceptEmpty(ctx, paramName); + return args; + } + public static T[] CheckNonEmpty(T[] args, string paramName, string msg) + { + if (Size(args) == 0) + throw ExceptEmpty(paramName, msg); + return args; + } + public static T[] CheckNonEmpty(this IExceptionContext ctx, T[] args, string paramName, string msg) + { + if (Size(args) == 0) + throw ExceptEmpty(ctx, paramName, msg); + return args; + } + public static ICollection CheckNonEmpty(ICollection args, string paramName) + { + if (Size(args) == 0) + throw ExceptEmpty(paramName); + return args; + } + public static ICollection CheckNonEmpty(this IExceptionContext ctx, ICollection args, string paramName) + { + if (Size(args) == 0) + throw ExceptEmpty(ctx, paramName); + return args; + } + public static ICollection CheckNonEmpty(ICollection args, string paramName, string msg) + { + if (Size(args) == 0) + throw ExceptEmpty(paramName, msg); + return args; + } + public static ICollection CheckNonEmpty(this IExceptionContext ctx, ICollection args, string paramName, string msg) + { + if (Size(args) == 0) + throw ExceptEmpty(ctx, paramName, msg); + return args; + } - public static void CheckIO(bool f) - { - if (!f) - throw ExceptIO(); - } - public static void CheckIO(this IExceptionContext ctx, bool f) - { - if (!f) - throw ExceptIO(ctx); - } - public static void CheckIO(bool f, string msg) - { - if (!f) - throw ExceptIO(msg); - } - public static void CheckIO(this IExceptionContext ctx, bool f, string msg) - { - if (!f) - throw ExceptIO(ctx, msg); - } + public static void CheckDecode(bool f) + { + if (!f) + throw ExceptDecode(); + } + public static void CheckDecode(this IExceptionContext ctx, bool f) + { + if (!f) + throw ExceptDecode(ctx); + } + public static void CheckDecode(bool f, string msg) + { + if (!f) + throw ExceptDecode(msg); + } + public static void CheckDecode(this IExceptionContext ctx, bool f, string msg) + { + if (!f) + throw ExceptDecode(ctx, msg); + } + + public static void CheckIO(bool f) + { + if (!f) + throw ExceptIO(); + } + public static void CheckIO(this IExceptionContext ctx, bool f) + { + if (!f) + throw ExceptIO(ctx); + } + public static void CheckIO(bool f, string msg) + { + if (!f) + throw ExceptIO(msg); + } + public static void CheckIO(this IExceptionContext ctx, bool f, string msg) + { + if (!f) + throw ExceptIO(ctx, msg); + } #if !PRIVATE_CONTRACTS /// @@ -727,234 +747,234 @@ public static void CheckAlive(this IHostEnvironment env) throw Process(new OperationCanceledException("Operation was cancelled."), env); } #endif - /// - /// This documents that the parameter can legally be null. - /// - [Conditional("INVARIANT_CHECKS")] - public static void CheckValueOrNull(T val) where T : class - { - } - [Conditional("INVARIANT_CHECKS")] - public static void CheckValueOrNull(this IExceptionContext ctx, T val) where T : class - { - } + /// + /// This documents that the parameter can legally be null. + /// + [Conditional("INVARIANT_CHECKS")] + public static void CheckValueOrNull(T val) where T : class + { + } + [Conditional("INVARIANT_CHECKS")] + public static void CheckValueOrNull(this IExceptionContext ctx, T val) where T : class + { + } - // Assert + // Assert -#region Private assert handling + #region Private assert handling - private static void DbgFailCore(string msg, IExceptionContext ctx = null) - { - var handler = _handler; - - if (handler != null) - handler(msg, ctx); - else if (ctx != null) - Debug.Fail(msg, ctx.ContextDescription); - else - Debug.Fail(msg); - } + private static void DbgFailCore(string msg, IExceptionContext ctx = null) + { + var handler = _handler; + + if (handler != null) + handler(msg, ctx); + else if (ctx != null) + Debug.Fail(msg, ctx.ContextDescription); + else + Debug.Fail(msg); + } - private static void DbgFail(IExceptionContext ctx = null) - { - DbgFailCore("Assertion Failed", ctx); - } - private static void DbgFail(string msg) - { - DbgFailCore(msg); - } - private static void DbgFail(IExceptionContext ctx, string msg) - { - DbgFailCore(msg, ctx); - } - private static void DbgFailValue(IExceptionContext ctx = null) - { - DbgFailCore("Non-null assertion failure", ctx); - } - private static void DbgFailValue(string paramName) - { - DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-null assertion failure: {0}", paramName)); - } - private static void DbgFailValue(IExceptionContext ctx, string paramName) - { - DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-null assertion failure: {0}", paramName), ctx); - } - private static void DbgFailValue(string paramName, string msg) - { - DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-null assertion failure: {0}: {1}", paramName, msg)); - } - private static void DbgFailValue(IExceptionContext ctx, string paramName, string msg) - { - DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-null assertion failure: {0}: {1}", paramName, msg), ctx); - } - private static void DbgFailEmpty(IExceptionContext ctx = null) - { - DbgFailCore("Non-empty assertion failure", ctx); - } - private static void DbgFailEmpty(string msg) - { - DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-empty assertion failure: {0}", msg)); - } - private static void DbgFailEmpty(IExceptionContext ctx, string msg) - { - DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-empty assertion failure: {0}", msg), ctx); - } + private static void DbgFail(IExceptionContext ctx = null) + { + DbgFailCore("Assertion Failed", ctx); + } + private static void DbgFail(string msg) + { + DbgFailCore(msg); + } + private static void DbgFail(IExceptionContext ctx, string msg) + { + DbgFailCore(msg, ctx); + } + private static void DbgFailValue(IExceptionContext ctx = null) + { + DbgFailCore("Non-null assertion failure", ctx); + } + private static void DbgFailValue(string paramName) + { + DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-null assertion failure: {0}", paramName)); + } + private static void DbgFailValue(IExceptionContext ctx, string paramName) + { + DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-null assertion failure: {0}", paramName), ctx); + } + private static void DbgFailValue(string paramName, string msg) + { + DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-null assertion failure: {0}: {1}", paramName, msg)); + } + private static void DbgFailValue(IExceptionContext ctx, string paramName, string msg) + { + DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-null assertion failure: {0}: {1}", paramName, msg), ctx); + } + private static void DbgFailEmpty(IExceptionContext ctx = null) + { + DbgFailCore("Non-empty assertion failure", ctx); + } + private static void DbgFailEmpty(string msg) + { + DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-empty assertion failure: {0}", msg)); + } + private static void DbgFailEmpty(IExceptionContext ctx, string msg) + { + DbgFailCore(string.Format(CultureInfo.InvariantCulture, "Non-empty assertion failure: {0}", msg), ctx); + } -#endregion Private assert handling + #endregion Private assert handling - [Conditional("DEBUG")] - public static void Assert(bool f) - { - if (!f) - DbgFail(); - } - [Conditional("DEBUG")] - public static void Assert(this IExceptionContext ctx, bool f) - { - if (!f) - DbgFail(ctx); - } + [Conditional("DEBUG")] + public static void Assert(bool f) + { + if (!f) + DbgFail(); + } + [Conditional("DEBUG")] + public static void Assert(this IExceptionContext ctx, bool f) + { + if (!f) + DbgFail(ctx); + } - [Conditional("DEBUG")] - public static void Assert(bool f, string msg) - { - if (!f) - DbgFail(msg); - } - [Conditional("DEBUG")] - public static void Assert(this IExceptionContext ctx, bool f, string msg) - { - if (!f) - DbgFail(ctx, msg); - } + [Conditional("DEBUG")] + public static void Assert(bool f, string msg) + { + if (!f) + DbgFail(msg); + } + [Conditional("DEBUG")] + public static void Assert(this IExceptionContext ctx, bool f, string msg) + { + if (!f) + DbgFail(ctx, msg); + } - [Conditional("DEBUG")] - public static void AssertValue(T val) where T : class - { - if (object.ReferenceEquals(val, null)) - DbgFailValue(); - } - [Conditional("DEBUG")] - public static void AssertValue(this IExceptionContext ctx, T val) where T : class - { - if (object.ReferenceEquals(val, null)) - DbgFailValue(ctx); - } + [Conditional("DEBUG")] + public static void AssertValue(T val) where T : class + { + if (object.ReferenceEquals(val, null)) + DbgFailValue(); + } + [Conditional("DEBUG")] + public static void AssertValue(this IExceptionContext ctx, T val) where T : class + { + if (object.ReferenceEquals(val, null)) + DbgFailValue(ctx); + } - [Conditional("DEBUG")] - public static void AssertValue(T val, string paramName) where T : class - { - if (object.ReferenceEquals(val, null)) - DbgFailValue(paramName); - } - [Conditional("DEBUG")] - public static void AssertValue(this IExceptionContext ctx, T val, string paramName) where T : class - { - if (object.ReferenceEquals(val, null)) - DbgFailValue(ctx, paramName); - } + [Conditional("DEBUG")] + public static void AssertValue(T val, string paramName) where T : class + { + if (object.ReferenceEquals(val, null)) + DbgFailValue(paramName); + } + [Conditional("DEBUG")] + public static void AssertValue(this IExceptionContext ctx, T val, string paramName) where T : class + { + if (object.ReferenceEquals(val, null)) + DbgFailValue(ctx, paramName); + } - [Conditional("DEBUG")] - public static void AssertValue(T val, string name, string msg) where T : class - { - if (object.ReferenceEquals(val, null)) - DbgFailValue(name, msg); - } - [Conditional("DEBUG")] - public static void AssertValue(this IExceptionContext ctx, T val, string name, string msg) where T : class - { - if (object.ReferenceEquals(val, null)) - DbgFailValue(ctx, name, msg); - } + [Conditional("DEBUG")] + public static void AssertValue(T val, string name, string msg) where T : class + { + if (object.ReferenceEquals(val, null)) + DbgFailValue(name, msg); + } + [Conditional("DEBUG")] + public static void AssertValue(this IExceptionContext ctx, T val, string name, string msg) where T : class + { + if (object.ReferenceEquals(val, null)) + DbgFailValue(ctx, name, msg); + } - [Conditional("DEBUG")] - public static void AssertNonEmpty(string s) - { - if (string.IsNullOrEmpty(s)) - DbgFailEmpty(); - } - [Conditional("DEBUG")] - public static void AssertNonEmpty(this IExceptionContext ctx, string s) - { - if (string.IsNullOrEmpty(s)) - DbgFailEmpty(ctx); - } + [Conditional("DEBUG")] + public static void AssertNonEmpty(string s) + { + if (string.IsNullOrEmpty(s)) + DbgFailEmpty(); + } + [Conditional("DEBUG")] + public static void AssertNonEmpty(this IExceptionContext ctx, string s) + { + if (string.IsNullOrEmpty(s)) + DbgFailEmpty(ctx); + } - [Conditional("DEBUG")] - public static void AssertNonWhiteSpace(string s) - { - if (string.IsNullOrWhiteSpace(s)) - DbgFailEmpty(); - } - [Conditional("DEBUG")] - public static void AssertNonWhiteSpace(this IExceptionContext ctx, string s) - { - if (string.IsNullOrWhiteSpace(s)) - DbgFailEmpty(ctx); - } + [Conditional("DEBUG")] + public static void AssertNonWhiteSpace(string s) + { + if (string.IsNullOrWhiteSpace(s)) + DbgFailEmpty(); + } + [Conditional("DEBUG")] + public static void AssertNonWhiteSpace(this IExceptionContext ctx, string s) + { + if (string.IsNullOrWhiteSpace(s)) + DbgFailEmpty(ctx); + } - [Conditional("DEBUG")] - public static void AssertNonEmpty(string s, string msg) - { - if (string.IsNullOrEmpty(s)) - DbgFailEmpty(msg); - } - [Conditional("DEBUG")] - public static void AssertNonEmpty(this IExceptionContext ctx, string s, string msg) - { - if (string.IsNullOrEmpty(s)) - DbgFailEmpty(ctx, msg); - } + [Conditional("DEBUG")] + public static void AssertNonEmpty(string s, string msg) + { + if (string.IsNullOrEmpty(s)) + DbgFailEmpty(msg); + } + [Conditional("DEBUG")] + public static void AssertNonEmpty(this IExceptionContext ctx, string s, string msg) + { + if (string.IsNullOrEmpty(s)) + DbgFailEmpty(ctx, msg); + } - [Conditional("DEBUG")] - public static void AssertNonWhiteSpace(string s, string msg) - { - if (string.IsNullOrWhiteSpace(s)) - DbgFailEmpty(msg); - } - [Conditional("DEBUG")] - public static void AssertNonWhiteSpace(this IExceptionContext ctx, string s, string msg) - { - if (string.IsNullOrWhiteSpace(s)) - DbgFailEmpty(ctx, msg); - } + [Conditional("DEBUG")] + public static void AssertNonWhiteSpace(string s, string msg) + { + if (string.IsNullOrWhiteSpace(s)) + DbgFailEmpty(msg); + } + [Conditional("DEBUG")] + public static void AssertNonWhiteSpace(this IExceptionContext ctx, string s, string msg) + { + if (string.IsNullOrWhiteSpace(s)) + DbgFailEmpty(ctx, msg); + } - [Conditional("DEBUG")] - public static void AssertNonEmpty(ICollection args) - { - if (Size(args) == 0) - DbgFail(); - } - [Conditional("DEBUG")] - public static void AssertNonEmpty(this IExceptionContext ctx, ICollection args) - { - if (Size(args) == 0) - DbgFail(ctx); - } + [Conditional("DEBUG")] + public static void AssertNonEmpty(ICollection args) + { + if (Size(args) == 0) + DbgFail(); + } + [Conditional("DEBUG")] + public static void AssertNonEmpty(this IExceptionContext ctx, ICollection args) + { + if (Size(args) == 0) + DbgFail(ctx); + } - [Conditional("DEBUG")] - public static void AssertNonEmpty(ICollection args, string msg) - { - if (Size(args) == 0) - DbgFail(msg); - } - [Conditional("DEBUG")] - public static void AssertNonEmpty(this IExceptionContext ctx, ICollection args, string msg) - { - if (Size(args) == 0) - DbgFail(ctx, msg); - } + [Conditional("DEBUG")] + public static void AssertNonEmpty(ICollection args, string msg) + { + if (Size(args) == 0) + DbgFail(msg); + } + [Conditional("DEBUG")] + public static void AssertNonEmpty(this IExceptionContext ctx, ICollection args, string msg) + { + if (Size(args) == 0) + DbgFail(ctx, msg); + } - /// - /// This documents that the parameter can legally be null. - /// - [Conditional("INVARIANT_CHECKS")] - public static void AssertValueOrNull(T val) where T : class - { - } - [Conditional("INVARIANT_CHECKS")] - public static void AssertValueOrNull(this IExceptionContext ctx, T val) where T : class - { - } + /// + /// This documents that the parameter can legally be null. + /// + [Conditional("INVARIANT_CHECKS")] + public static void AssertValueOrNull(T val) where T : class + { } + [Conditional("INVARIANT_CHECKS")] + public static void AssertValueOrNull(this IExceptionContext ctx, T val) where T : class + { + } +} } diff --git a/src/Microsoft.ML.Data/Prediction/IPredictionTransformer.cs b/src/Microsoft.ML.Data/Prediction/IPredictionTransformer.cs index 9be9c7c753..899c7622dc 100644 --- a/src/Microsoft.ML.Data/Prediction/IPredictionTransformer.cs +++ b/src/Microsoft.ML.Data/Prediction/IPredictionTransformer.cs @@ -11,7 +11,7 @@ namespace Microsoft.ML.Runtime { - public interface IPredictionTransformer : ITransformer + public interface IPredictionTransformer : ITransformer where TModel : IPredictor { string FeatureColumn { get; } @@ -20,12 +20,4 @@ public interface IPredictionTransformer : ITransformer TModel Model { get; } } - - public interface ICalibratedBinaryPredictor - where TCalibrator : ICalibrator - where TModel : IPredictorProducing - { - TCalibrator Calibrator { get; } - TModel Model { get; } - } } diff --git a/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs b/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs new file mode 100644 index 0000000000..f02e4bb79e --- /dev/null +++ b/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs @@ -0,0 +1,279 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using Microsoft.ML.Runtime; +using Microsoft.ML.Runtime.Data; +using Microsoft.ML.Runtime.Data.IO; +using Microsoft.ML.Runtime.Model; + +[assembly: LoadableClass(typeof(BinaryPredictionTransformer>), typeof(BinaryPredictionTransformer), null, typeof(SignatureLoadModel), + "", BinaryPredictionTransformer.LoaderSignature)] + +namespace Microsoft.ML.Runtime.Data +{ + public abstract class PredictionTransformerBase : IPredictionTransformer, ICanSaveModel + where TModel : class, IPredictor + { + protected readonly IHost Host; + protected readonly ISchemaBindableMapper BindableMapper; + protected readonly ISchema TrainSchema; + + public string FeatureColumn { get; } + + public ColumnType FeatureColumnType { get; } + + public TModel Model { get; } + + public PredictionTransformerBase(IHost host, TModel model, ISchema trainSchema, string featureColumn) + { + Contracts.CheckValue(host, nameof(host)); + Host = host; + Host.CheckValue(trainSchema, nameof(trainSchema)); + + Model = model; + FeatureColumn = featureColumn; + trainSchema.TryGetColumnIndex(featureColumn, out int col); + if (col < 0) + throw Host.ExceptSchemaMismatch(nameof(featureColumn), "feature", featureColumn); + FeatureColumnType = trainSchema.GetColumnType(col); + + TrainSchema = trainSchema; + BindableMapper = ScoreUtils.GetSchemaBindableMapper(Host, model); + } + + internal PredictionTransformerBase(IHost host, ModelLoadContext ctx) + { + Host = host; + + ctx.LoadModel(host, out TModel model, "Model"); + Model = model; + + // *** Binary format *** + // model: prediction model. + // stream: empty data view that contains train schema. + // id of string: feature column. + var ms = new MemoryStream(); + ctx.TryLoadBinaryStream("TrainSchema", reader => + { + reader.BaseStream.CopyTo(ms); + }); + + ms.Position = 0; + var loader = new BinaryLoader(host, new BinaryLoader.Arguments(), ms); + TrainSchema = loader.Schema; + + FeatureColumn = ctx.LoadString(); + TrainSchema.TryGetColumnIndex(FeatureColumn, out int col); + if (col < 0) + throw Host.ExceptSchemaMismatch(nameof(FeatureColumn), "feature", FeatureColumn); + FeatureColumnType = TrainSchema.GetColumnType(col); + + BindableMapper = ScoreUtils.GetSchemaBindableMapper(Host, model); + } + + public ISchema GetOutputSchema(ISchema inputSchema) + { + Host.CheckValue(inputSchema, nameof(inputSchema)); + + inputSchema.TryGetColumnIndex(FeatureColumn, out int col); + if (col < 0) + throw Host.ExceptSchemaMismatch(nameof(inputSchema), "feature", FeatureColumn, FeatureColumnType.ToString(), null); + if (!inputSchema.GetColumnType(col).Equals(FeatureColumnType)) + throw Host.ExceptSchemaMismatch(nameof(inputSchema), "feature", FeatureColumn, FeatureColumnType.ToString(), inputSchema.GetColumnType(col).ToString()); + + return Transform(new EmptyDataView(Host, inputSchema)).Schema; + } + + public abstract IDataView Transform(IDataView input); + + public void Save(ModelSaveContext ctx) + { + Host.CheckValue(ctx, nameof(ctx)); + ctx.CheckAtModel(); + SaveCore(ctx); + } + + 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, "Model"); + ctx.SaveBinaryStream("TrainSchema", 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); + } + } + + public sealed class BinaryPredictionTransformer : PredictionTransformerBase + where TModel : class, IPredictorProducing + { + private readonly BinaryClassifierScorer _scorer; + + public readonly string ThresholdColumn; + public readonly float Threshold; + + public BinaryPredictionTransformer(IHostEnvironment env, TModel model, ISchema inputSchema, string featureColumn, + float threshold = 0f, string thresholdColumn = DefaultColumnNames.Score) + : base(Contracts.CheckRef(env, nameof(env)).Register(nameof(BinaryPredictionTransformer)), model, inputSchema, featureColumn) + { + Host.CheckNonEmpty(thresholdColumn, nameof(thresholdColumn)); + var schema = new RoleMappedSchema(inputSchema, null, featureColumn); + Threshold = threshold; + ThresholdColumn = thresholdColumn; + + var args = new BinaryClassifierScorer.Arguments { Threshold = Threshold, ThresholdColumn = ThresholdColumn }; + _scorer = new BinaryClassifierScorer(Host, args, new EmptyDataView(Host, inputSchema), BindableMapper.Bind(Host, schema), schema); + } + + public BinaryPredictionTransformer(IHostEnvironment env, ModelLoadContext ctx) + : base(Contracts.CheckRef(env, nameof(env)).Register(nameof(BinaryPredictionTransformer)), ctx) + { + Threshold = ctx.Reader.ReadSingle(); + ThresholdColumn = ctx.LoadStringOrNull(); + + var schema = new RoleMappedSchema(TrainSchema, null, FeatureColumn); + var args = new BinaryClassifierScorer.Arguments { Threshold = Threshold, ThresholdColumn = ThresholdColumn }; + _scorer = new BinaryClassifierScorer(Host, args, new EmptyDataView(Host, TrainSchema), BindableMapper.Bind(Host, schema), schema); + } + + public override IDataView Transform(IDataView input) + { + Host.CheckValue(input, nameof(input)); + return _scorer.ApplyToData(Host, input); + } + + protected override void SaveCore(ModelSaveContext ctx) + { + Contracts.AssertValue(ctx); + ctx.SetVersionInfo(GetVersionInfo()); + + // *** Binary format *** + // + // float: scorer threshold + // id of string: scorer threshold column + base.SaveCore(ctx); + + ctx.Writer.Write(Threshold); + ctx.SaveStringOrNull(ThresholdColumn); + } + + private static VersionInfo GetVersionInfo() + { + return new VersionInfo( + modelSignature: "BIN PRED", + verWrittenCur: 0x00010001, // Initial + verReadableCur: 0x00010001, + verWeCanReadBack: 0x00010001, + loaderSignature: BinaryPredictionTransformer.LoaderSignature); + } + } + + public sealed class MulticlassPredictionTransformer : PredictionTransformerBase + where TModel : class, IPredictorProducing> + { + private readonly MultiClassClassifierScorer _scorer; + + public MulticlassPredictionTransformer(IHostEnvironment env, TModel model, ISchema inputSchema, string featureColumn, string labelColumn, MultiClassClassifierScorer.Arguments args) + : base(Contracts.CheckRef(env, nameof(env)).Register(nameof(MulticlassPredictionTransformer)), model, inputSchema, featureColumn) + { + var schema = new RoleMappedSchema(inputSchema, labelColumn, featureColumn); + _scorer = new MultiClassClassifierScorer(Host, args, new EmptyDataView(Host, inputSchema), BindableMapper.Bind(Host, schema), schema); + } + + public override IDataView Transform(IDataView input) + { + Host.CheckValue(input, nameof(input)); + return _scorer.ApplyToData(Host, input); + } + + protected override void SaveCore(ModelSaveContext ctx) + { + Contracts.AssertValue(ctx); + ctx.SetVersionInfo(GetVersionInfo()); + + // *** Binary format *** + // + base.SaveCore(ctx); + } + + private static VersionInfo GetVersionInfo() + { + return new VersionInfo( + modelSignature: "MC PRED", + verWrittenCur: 0x00010001, // Initial + verReadableCur: 0x00010001, + verWeCanReadBack: 0x00010001, + loaderSignature: MulticlassPredictionTransformer.LoaderSignature); + } + } + + public sealed class RegressionPredictionTransformer : PredictionTransformerBase + where TModel : class, IPredictorProducing + { + private readonly GenericScorer _scorer; + + public RegressionPredictionTransformer(IHostEnvironment env, TModel model, ISchema inputSchema, string featureColumn) + : base(Contracts.CheckRef(env, nameof(env)).Register(nameof(RegressionPredictionTransformer)), model, inputSchema, featureColumn) + { + var schema = new RoleMappedSchema(inputSchema, null, featureColumn); + _scorer = new GenericScorer(Host, new GenericScorer.Arguments(), new EmptyDataView(Host, inputSchema), BindableMapper.Bind(Host, schema), schema); + } + + public override IDataView Transform(IDataView input) + { + Host.CheckValue(input, nameof(input)); + return _scorer.ApplyToData(Host, input); + } + + protected override void SaveCore(ModelSaveContext ctx) + { + Contracts.AssertValue(ctx); + ctx.SetVersionInfo(GetVersionInfo()); + + // *** Binary format *** + // + base.SaveCore(ctx); + } + + private static VersionInfo GetVersionInfo() + { + return new VersionInfo( + modelSignature: "MC PRED", + verWrittenCur: 0x00010001, // Initial + verReadableCur: 0x00010001, + verWeCanReadBack: 0x00010001, + loaderSignature: RegressionPredictionTransformer.LoaderSignature); + } + } + + internal static class BinaryPredictionTransformer + { + public const string LoaderSignature = "BinaryPredXfer"; + + public static BinaryPredictionTransformer> Create(IHostEnvironment env, ModelLoadContext ctx) + => new BinaryPredictionTransformer>(env, ctx); + } + + internal static class MulticlassPredictionTransformer + { + public const string LoaderSignature = "MulticlassPredXfer"; + } + + internal static class RegressionPredictionTransformer + { + public const string LoaderSignature = "RegressionPredXfer"; + } +} diff --git a/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs b/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs index 02457434b6..fbab940a77 100644 --- a/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs +++ b/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs @@ -5,10 +5,12 @@ namespace Microsoft.ML.Runtime.Training { - public interface ITrainerEstimator: IEstimator + public interface ITrainerEstimator: IEstimator where TTransformer: IPredictionTransformer where TPredictor: IPredictor { - TrainerInfo TrainerInfo { get; } + TrainerInfo Info { get; } + + PredictionKind PredictionKind { get; } } } diff --git a/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs b/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs index f77def382c..3d7ec7e80e 100644 --- a/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs +++ b/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs @@ -1,6 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Text; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Linq; using Microsoft.ML.Core.Data; using Microsoft.ML.Runtime.Data; @@ -11,9 +13,7 @@ namespace Microsoft.ML.Runtime.Training /// A 'simple trainer' accepts one feature column and one label column, also optionally a weight column. /// It produces a 'prediction transformer'. /// - /// - /// - public abstract class TrainerEstimatorBase : ITrainerEstimator + public abstract class TrainerEstimatorBase : ITrainerEstimator, ITrainer where TTransformer : IPredictionTransformer where TModel : IPredictor { @@ -34,31 +34,99 @@ public abstract class TrainerEstimatorBase : ITrainerEstim /// public readonly SchemaShape.Column WeightColumn; + /// + /// The columns that will be created by the fitted transformer. + /// + protected abstract SchemaShape.Column[] OutputColumns { get; } + protected readonly IHost Host; /// /// The information about the trainer: whether it benefits from normalization, caching etc. /// - public TrainerInfo TrainerInfo { get; } + public TrainerInfo Info { get; } + + public abstract PredictionKind PredictionKind { get; } - public TrainerEstimatorBase(IHost host, + public TrainerEstimatorBase(IHost host, TrainerInfo trainerInfo, SchemaShape.Column feature, SchemaShape.Column label, SchemaShape.Column weight = null) { Contracts.CheckValue(host, nameof(host)); Host = host; + Host.CheckValue(trainerInfo, nameof(trainerInfo)); Host.CheckValue(feature, nameof(feature)); Host.CheckValueOrNull(label); Host.CheckValueOrNull(weight); + Info = trainerInfo; FeatureColumn = feature; LabelColumn = label; WeightColumn = weight; } + public TTransformer Fit(IDataView input) => TrainTransformer(input); + + public SchemaShape GetOutputSchema(SchemaShape inputSchema) + { + Host.CheckValue(inputSchema, nameof(inputSchema)); + + CheckInputSchema(inputSchema); + + var outColumns = inputSchema.Columns.ToDictionary(x => x.Name); + foreach (var col in OutputColumns) + outColumns[col.Name] = col; + + return new SchemaShape(outColumns.Values); + } + + public TModel Train(TrainContext context) + { + Host.CheckValue(context, nameof(context)); + return TrainModelCore(context); + } + + private void CheckInputSchema(SchemaShape inputSchema) + { + // Verify that all required input columns are present, and are of the same type. + var featureCol = inputSchema.FindColumn(FeatureColumn.Name); + if (featureCol == null) + throw Host.Except($"Feature column '{FeatureColumn.Name}' is not found"); + if (!FeatureColumn.IsCompatibleWith(featureCol)) + throw Host.Except($"Feature column '{FeatureColumn.Name}' is not compatible"); + + if (WeightColumn != null) + { + var weightCol = inputSchema.FindColumn(WeightColumn.Name); + if (weightCol == null) + throw Host.Except($"Weight column '{WeightColumn.Name}' is not found"); + if (!WeightColumn.IsCompatibleWith(weightCol)) + throw Host.Except($"Weight column '{WeightColumn.Name}' is not compatible"); + } + + // Special treatment for label column: we allow different types of labels, so the trainers + // may define their own requirements on the label column. + if (LabelColumn != null) + { + var labelCol = inputSchema.FindColumn(LabelColumn.Name); + if (labelCol == null) + throw Host.Except($"Label column '{LabelColumn.Name}' is not found"); + CheckLabelCompatible(labelCol); + } + } + + protected virtual void CheckLabelCompatible(SchemaShape.Column labelCol) + { + Contracts.AssertValue(labelCol); + Contracts.AssertValue(LabelColumn); + + if (!LabelColumn.IsCompatibleWith(labelCol)) + throw Host.Except($"Label column '{LabelColumn.Name}' is not compatible"); + } + protected TTransformer TrainTransformer(IDataView trainSet, IDataView validationSet = null, IPredictor initPredictor = null) { - var cachedTrain = TrainerInfo.WantCaching ? new CacheDataView(Host, trainSet, prefetch: null) : trainSet; + var cachedTrain = Info.WantCaching ? new CacheDataView(Host, trainSet, prefetch: null) : trainSet; var trainRoles = MakeRoles(cachedTrain); @@ -68,27 +136,21 @@ protected TTransformer TrainTransformer(IDataView trainSet, validRoles = null; else { - var cachedValid = TrainerInfo.WantCaching ? new CacheDataView(Host, validationSet, prefetch: null) : validationSet; + var cachedValid = Info.WantCaching ? new CacheDataView(Host, validationSet, prefetch: null) : validationSet; validRoles = MakeRoles(cachedValid); } var pred = TrainModelCore(new TrainContext(trainRoles, validRoles, initPredictor)); - - var emptyData = new EmptyDataView(Host, trainSet.Schema); - var scoreRoles = MakeRoles(emptyData); - return MakeTransformer(pred); - } - - public SchemaShape GetOutputSchema(SchemaShape inputSchema) - { - throw new NotImplementedException(); + return MakeTransformer(pred, trainSet.Schema); } protected abstract TModel TrainModelCore(TrainContext trainContext); - protected abstract TTransformer MakeTransformer(TModel model); + protected abstract TTransformer MakeTransformer(TModel model, ISchema trainSchema); private RoleMappedData MakeRoles(IDataView data) => - new RoleMappedData(data, label: LabelColumn.Name, feature: FeatureColumn.Name, weight: WeightColumn.Name); + new RoleMappedData(data, label: LabelColumn?.Name, feature: FeatureColumn.Name, weight: WeightColumn?.Name); + + IPredictor ITrainer.Train(TrainContext context) => Train(context); } } diff --git a/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs b/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs index cfda92c503..a7187dd640 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs @@ -20,6 +20,7 @@ using Microsoft.ML.Runtime.Learners; using Microsoft.ML.Runtime.Numeric; using Microsoft.ML.Runtime.Training; +using Microsoft.ML.Core.Data; [assembly: LoadableClass(typeof(LinearClassificationTrainer), typeof(LinearClassificationTrainer.Arguments), new[] { typeof(SignatureBinaryClassifierTrainer), typeof(SignatureTrainer), typeof(SignatureFeatureScorerTrainer) }, @@ -147,8 +148,9 @@ protected virtual int ComputeNumThreads(FloatLabelCursor.Factory cursorFactory) } } - public abstract class SdcaTrainerBase : LinearTrainerBase - where TPredictor : IPredictor + public abstract class SdcaTrainerBase : StochasticTrainerBase + where TTransformer : IPredictionTransformer + where TModel : IPredictor { // REVIEW: Making it even faster and more accurate: // 1. Train with not-too-many threads. nt = 2 or 4 seems to be good enough. Didn't seem additional benefit over more threads. @@ -240,14 +242,19 @@ protected enum MetricKind protected override bool ShuffleData => _args.Shuffle; - protected SdcaTrainerBase(ArgumentsBase args, IHostEnvironment env, string name) - : base(env, name) + protected SdcaTrainerBase(IHost host, ArgumentsBase args, SchemaShape.Column feature, SchemaShape.Column label, SchemaShape.Column weight = null) + : base(host, feature, label, weight) { _args = args; - _args.Check(env); + _args.Check(host); } - protected sealed override TPredictor TrainCore(IChannel ch, RoleMappedData data, LinearPredictor predictor, int weightSetCount) + protected Float WDot(ref VBuffer features, ref VBuffer weights, Float bias) + { + return VectorUtils.DotProduct(ref weights, ref features) + bias; + } + + protected sealed override TModel TrainCore(IChannel ch, RoleMappedData data, LinearPredictor predictor, int weightSetCount) { Contracts.Assert(predictor == null, "SDCA based trainers don't support continuous training."); Contracts.Assert(weightSetCount >= 1); @@ -603,7 +610,7 @@ protected sealed override TPredictor TrainCore(IChannel ch, RoleMappedData data, return CreatePredictor(weights, bias); } - protected abstract TPredictor CreatePredictor(VBuffer[] weights, Float[] bias); + protected abstract TModel CreatePredictor(VBuffer[] weights, Float[] bias); // Assign an upper bound for number of iterations based on data set size first. // This ensures SDCA will not run forever... @@ -1351,7 +1358,7 @@ public void Add(Double summand) } } - public sealed class LinearClassificationTrainer : SdcaTrainerBase + public sealed class LinearClassificationTrainer : SdcaTrainerBase, ParameterMixingCalibratedPredictor> { public const string LoadNameValue = "SDCA"; public const string UserNameValue = "Fast Linear (SA-SDCA)"; @@ -1383,22 +1390,61 @@ internal override void Check(IHostEnvironment env) protected override bool ShuffleData => _args.Shuffle; - public override PredictionKind PredictionKind => PredictionKind.BinaryClassification; + protected override SchemaShape.Column[] OutputColumns { get; } - public override TrainerInfo Info { get; } + public override PredictionKind PredictionKind => throw new NotImplementedException(); - public LinearClassificationTrainer(IHostEnvironment env, Arguments args) - : base(args, env, LoadNameValue) + public LinearClassificationTrainer(IHostEnvironment env, Arguments args, + string featureColumn, string labelColumn, string weightColumn = null) + : base(Contracts.CheckRef(env, nameof(env)).Register(LoadNameValue), args, MakeFeatureColumn(featureColumn), MakeLabelColumn(labelColumn), MakeWeightColumn(weightColumn)) { _loss = args.LossFunction.CreateComponent(env); - base.Loss = _loss; - Info = new TrainerInfo(calibration: !(_loss is LogLoss)); - NeedShuffle = args.Shuffle; + Loss = _loss; _args = args; _positiveInstanceWeight = _args.PositiveInstanceWeight; + OutputColumns = new[] + { + new SchemaShape.Column(DefaultColumnNames.Score, SchemaShape.Column.VectorKind.Scalar, DataKind.R4, false), + new SchemaShape.Column(DefaultColumnNames.PredictedLabel, SchemaShape.Column.VectorKind.Scalar, DataKind.BL, false) + }; + } + + public LinearClassificationTrainer(IHostEnvironment env, Arguments args) + : this(env, args, args.FeatureColumn, args.LabelColumn) + { + } + + protected override void CheckLabelCompatible(SchemaShape.Column labelCol) + { + Contracts.AssertValue(labelCol); + + Action error = + () => throw Host.ExceptSchemaMismatch(nameof(labelCol), "label", labelCol.Name, "BL, R8, R4 or a Key", labelCol.GetTypeString()); + + if (labelCol.Kind != SchemaShape.Column.VectorKind.Scalar) + error(); + if (!labelCol.IsKey && labelCol.ItemKind != DataKind.R4 && labelCol.ItemKind != DataKind.R8 && labelCol.ItemKind != DataKind.BL) + error(); + } + + private static SchemaShape.Column MakeWeightColumn(string weightColumn) + { + if (weightColumn == null) + return null; + return new SchemaShape.Column(weightColumn, SchemaShape.Column.VectorKind.Scalar, DataKind.R4, false); + } + + private static SchemaShape.Column MakeLabelColumn(string labelColumn) + { + return new SchemaShape.Column(labelColumn, SchemaShape.Column.VectorKind.Scalar, DataKind.BL, false); } - protected override TScalarPredictor CreatePredictor(VBuffer[] weights, Float[] bias) + private static SchemaShape.Column MakeFeatureColumn(string featureColumn) + { + return new SchemaShape.Column(featureColumn, SchemaShape.Column.VectorKind.Vector, DataKind.R4, false, new string[0]); + } + + protected override ParameterMixingCalibratedPredictor CreatePredictor(VBuffer[] weights, Float[] bias) { Host.CheckParam(Utils.Size(weights) == 1, nameof(weights)); Host.CheckParam(Utils.Size(bias) == 1, nameof(bias)); @@ -1407,9 +1453,8 @@ protected override TScalarPredictor CreatePredictor(VBuffer[] weights, Fl VBuffer maybeSparseWeights = default; VBufferUtils.CreateMaybeSparseCopy(ref weights[0], ref maybeSparseWeights, Conversions.Instance.GetIsDefaultPredicate(NumberType.Float)); + var predictor = new LinearBinaryPredictor(Host, ref maybeSparseWeights, bias[0]); - if (!(_loss is LogLoss)) - return predictor; return new ParameterMixingCalibratedPredictor(Host, predictor, new PlattCalibrator(Host, -1, 0)); } @@ -1423,6 +1468,11 @@ protected override void CheckLabel(RoleMappedData examples, out int weightSetCou examples.CheckBinaryLabel(); weightSetCount = 1; } + + protected override BinaryPredictionTransformer MakeTransformer(ParameterMixingCalibratedPredictor model, ISchema trainSchema) + => new BinaryPredictionTransformer(Host, model, trainSchema, FeatureColumn.Name); + + public BinaryPredictionTransformer Train(IDataView trainData, IDataView validationData) => TrainTransformer(trainData, validationData); } public sealed class StochasticGradientDescentClassificationTrainer : @@ -1748,7 +1798,6 @@ public static CommonOutputs.BinaryClassificationOutput TrainBinary(IHostEnvironm () => new LinearClassificationTrainer(host, input), () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn), calibrator: input.Calibrator, maxCalibrationExamples: input.MaxCalibrationExamples); - } } } diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs index 5bfc40785a..6215a3588d 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs @@ -17,6 +17,7 @@ using Microsoft.ML.Runtime.Numeric; using Microsoft.ML.Runtime.Training; using Microsoft.ML.Runtime.Internal.Internallearn; +using Microsoft.ML.Core.Data; [assembly: LoadableClass(SdcaMultiClassTrainer.Summary, typeof(SdcaMultiClassTrainer), typeof(SdcaMultiClassTrainer.Arguments), new[] { typeof(SignatureMultiClassClassifierTrainer), typeof(SignatureTrainer), typeof(SignatureFeatureScorerTrainer) }, @@ -26,11 +27,9 @@ namespace Microsoft.ML.Runtime.Learners { - using TVectorPredictor = IPredictorProducing>; - // SDCA linear multiclass trainer. /// - public class SdcaMultiClassTrainer : SdcaTrainerBase + public class SdcaMultiClassTrainer : SdcaTrainerBase, MulticlassLogisticRegressionPredictor> { public const string LoadNameValue = "SDCAMC"; public const string UserNameValue = "Fast Linear Multi-class Classification (SA-SDCA)"; @@ -47,14 +46,55 @@ public sealed class Arguments : ArgumentsBase private readonly Arguments _args; public override PredictionKind PredictionKind => PredictionKind.MultiClassClassification; + protected override SchemaShape.Column[] OutputColumns { get; } - public SdcaMultiClassTrainer(IHostEnvironment env, Arguments args) - : base(args, env, LoadNameValue) + public SdcaMultiClassTrainer(IHostEnvironment env, Arguments args, + string featureColumn, string labelColumn, string weightColumn = null) + : base(Contracts.CheckRef(env, nameof(env)).Register(LoadNameValue), args, MakeFeatureColumn(featureColumn), MakeLabelColumn(labelColumn), MakeWeightColumn(weightColumn)) { _loss = args.LossFunction.CreateComponent(env); - base.Loss = _loss; - NeedShuffle = args.Shuffle; + Loss = _loss; _args = args; + OutputColumns = new[] + { + new SchemaShape.Column(DefaultColumnNames.Score, SchemaShape.Column.VectorKind.Vector, DataKind.R4, false), + new SchemaShape.Column(DefaultColumnNames.PredictedLabel, SchemaShape.Column.VectorKind.Scalar, DataKind.U4, true) + }; + } + + public SdcaMultiClassTrainer(IHostEnvironment env, Arguments args) + : this(env, args, args.FeatureColumn, args.LabelColumn) + { + } + + protected override void CheckLabelCompatible(SchemaShape.Column labelCol) + { + Contracts.AssertValue(labelCol); + + Action error = + () => throw Host.ExceptSchemaMismatch(nameof(labelCol), "label", labelCol.Name, "R8, R4 or a Key", labelCol.GetTypeString()); + + if (labelCol.Kind != SchemaShape.Column.VectorKind.Scalar) + error(); + if (!labelCol.IsKey && labelCol.ItemKind != DataKind.R4 && labelCol.ItemKind != DataKind.R8) + error(); + } + + private static SchemaShape.Column MakeWeightColumn(string weightColumn) + { + if (weightColumn == null) + return null; + return new SchemaShape.Column(weightColumn, SchemaShape.Column.VectorKind.Scalar, DataKind.R4, false); + } + + private static SchemaShape.Column MakeLabelColumn(string labelColumn) + { + return new SchemaShape.Column(labelColumn, SchemaShape.Column.VectorKind.Scalar, DataKind.U4, true); + } + + private static SchemaShape.Column MakeFeatureColumn(string featureColumn) + { + return new SchemaShape.Column(featureColumn, SchemaShape.Column.VectorKind.Vector, DataKind.R4, false, new string[0]); } /// @@ -339,7 +379,7 @@ protected override bool CheckConvergence( return converged; } - protected override TVectorPredictor CreatePredictor(VBuffer[] weights, Float[] bias) + protected override MulticlassLogisticRegressionPredictor CreatePredictor(VBuffer[] weights, Float[] bias) { Host.CheckValue(weights, nameof(weights)); Host.CheckValue(bias, nameof(bias)); @@ -364,6 +404,9 @@ protected override Float GetInstanceWeight(FloatLabelCursor cursor) { return cursor.Weight; } + + protected override MulticlassPredictionTransformer MakeTransformer(MulticlassLogisticRegressionPredictor model, ISchema trainSchema) + => new MulticlassPredictionTransformer(Host, model, trainSchema, FeatureColumn.Name, LabelColumn.Name, new MultiClassClassifierScorer.Arguments()); } /// diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs index bfa0796bed..4eefbfd084 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs @@ -14,6 +14,7 @@ using Microsoft.ML.Runtime.Learners; using Microsoft.ML.Runtime.Training; using Microsoft.ML.Runtime.Internal.Internallearn; +using Microsoft.ML.Core.Data; [assembly: LoadableClass(SdcaRegressionTrainer.Summary, typeof(SdcaRegressionTrainer), typeof(SdcaRegressionTrainer.Arguments), new[] { typeof(SignatureRegressorTrainer), typeof(SignatureTrainer), typeof(SignatureFeatureScorerTrainer) }, @@ -23,10 +24,8 @@ namespace Microsoft.ML.Runtime.Learners { - using TScalarPredictor = IPredictorWithFeatureWeights; - /// - public sealed class SdcaRegressionTrainer : SdcaTrainerBase + public sealed class SdcaRegressionTrainer : SdcaTrainerBase, LinearRegressionPredictor> { public const string LoadNameValue = "SDCAR"; public const string UserNameValue = "Fast Linear Regression (SA-SDCA)"; @@ -52,17 +51,42 @@ public Arguments() private readonly Arguments _args; public override PredictionKind PredictionKind => PredictionKind.Regression; + protected override SchemaShape.Column[] OutputColumns { get; } - public SdcaRegressionTrainer(IHostEnvironment env, Arguments args) - : base(args, env, LoadNameValue) + public SdcaRegressionTrainer(IHostEnvironment env, Arguments args, string featureColumn, string labelColumn, string weightColumn = null) + : base(Contracts.CheckRef(env, nameof(env)).Register(LoadNameValue), args, MakeFeatureColumn(featureColumn), MakeLabelColumn(labelColumn), MakeWeightColumn(weightColumn)) { _loss = args.LossFunction.CreateComponent(env); - base.Loss = _loss; - NeedShuffle = args.Shuffle; + Loss = _loss; _args = args; + OutputColumns = new[] + { + new SchemaShape.Column(DefaultColumnNames.Score, SchemaShape.Column.VectorKind.Scalar, DataKind.R4, false) + }; + } + + public SdcaRegressionTrainer(IHostEnvironment env, Arguments args) + : this(env, args, args.FeatureColumn, args.LabelColumn) + { + } + private static SchemaShape.Column MakeWeightColumn(string weightColumn) + { + if (weightColumn == null) + return null; + return new SchemaShape.Column(weightColumn, SchemaShape.Column.VectorKind.Scalar, DataKind.R4, false); + } + + private static SchemaShape.Column MakeLabelColumn(string labelColumn) + { + return new SchemaShape.Column(labelColumn, SchemaShape.Column.VectorKind.Scalar, DataKind.R4, false); } - protected override TScalarPredictor CreatePredictor(VBuffer[] weights, Float[] bias) + private static SchemaShape.Column MakeFeatureColumn(string featureColumn) + { + return new SchemaShape.Column(featureColumn, SchemaShape.Column.VectorKind.Vector, DataKind.R4, false); + } + + protected override LinearRegressionPredictor CreatePredictor(VBuffer[] weights, Float[] bias) { Host.CheckParam(Utils.Size(weights) == 1, nameof(weights)); Host.CheckParam(Utils.Size(bias) == 1, nameof(bias)); @@ -116,6 +140,9 @@ protected override Float TuneDefaultL2(IChannel ch, int maxIterations, long rowC ch.Info("Auto-tuning parameters: L2 = {0}.", l2); return l2; } + + protected override RegressionPredictionTransformer MakeTransformer(LinearRegressionPredictor model, ISchema trainSchema) + => new RegressionPredictionTransformer(Host, model, trainSchema, FeatureColumn.Name); } /// diff --git a/src/Microsoft.ML.StandardLearners/Standard/StochasticTrainerBase.cs b/src/Microsoft.ML.StandardLearners/Standard/StochasticTrainerBase.cs new file mode 100644 index 0000000000..2338247c8d --- /dev/null +++ b/src/Microsoft.ML.StandardLearners/Standard/StochasticTrainerBase.cs @@ -0,0 +1,101 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.ML.Core.Data; +using Microsoft.ML.Runtime.Data; +using Microsoft.ML.Runtime.Internal.Calibration; +using Microsoft.ML.Runtime.Training; +using System; + +namespace Microsoft.ML.Runtime.Learners +{ + public abstract class StochasticTrainerBase : TrainerEstimatorBase + where TTransformer : IPredictionTransformer + where TModel : IPredictor + { + public StochasticTrainerBase(IHost host, SchemaShape.Column feature, SchemaShape.Column label, SchemaShape.Column weight = null) + : base(host, new TrainerInfo(), feature, label, weight) + { + } + + /// + /// Whether data is to be shuffled every epoch. + /// + protected abstract bool ShuffleData { get; } + + protected override TModel TrainModelCore(TrainContext context) + { + Host.CheckValue(context, nameof(context)); + TModel pred; + using (var ch = Host.Start("Training")) + { + var preparedData = PrepareDataFromTrainingExamples(ch, context.TrainingSet, out int weightSetCount); + var initPred = context.InitialPredictor; + var linInitPred = (initPred as CalibratedPredictorBase)?.SubPredictor as LinearPredictor; + linInitPred = linInitPred ?? initPred as LinearPredictor; + Host.CheckParam(context.InitialPredictor == null || linInitPred != null, nameof(context), + "Initial predictor was not a linear predictor."); + pred = TrainCore(ch, preparedData, linInitPred, weightSetCount); + ch.Done(); + } + return pred; + } + + protected virtual int ComputeNumThreads(FloatLabelCursor.Factory cursorFactory) + { + int maxThreads = Math.Min(8, Math.Max(1, Environment.ProcessorCount / 2)); + if (0 < Host.ConcurrencyFactor && Host.ConcurrencyFactor < maxThreads) + maxThreads = Host.ConcurrencyFactor; + + return maxThreads; + } + + /// + /// This method ensures that the data meets the requirements of this trainer and its + /// subclasses, injects necessary transforms, and throws if it couldn't meet them. + /// + /// The channel + /// The training examples + /// Gets the length of weights and bias array. For binary classification and regression, + /// this is 1. For multi-class classification, this equals the number of classes on the label. + /// A potentially modified version of + protected RoleMappedData PrepareDataFromTrainingExamples(IChannel ch, RoleMappedData examples, out int weightSetCount) + { + ch.AssertValue(examples); + CheckLabel(examples, out weightSetCount); + examples.CheckFeatureFloatVector(); + var idvToShuffle = examples.Data; + IDataView idvToFeedTrain; + if (idvToShuffle.CanShuffle) + idvToFeedTrain = idvToShuffle; + else + { + var shuffleArgs = new ShuffleTransform.Arguments + { + PoolOnly = false, + ForceShuffle = ShuffleData + }; + idvToFeedTrain = new ShuffleTransform(Host, shuffleArgs, idvToShuffle); + } + + ch.Assert(idvToFeedTrain.CanShuffle); + + var roles = examples.Schema.GetColumnRoleNames(); + var examplesToFeedTrain = new RoleMappedData(idvToFeedTrain, roles); + + ch.AssertValue(examplesToFeedTrain.Schema.Label); + ch.AssertValue(examplesToFeedTrain.Schema.Feature); + if (examples.Schema.Weight != null) + ch.AssertValue(examplesToFeedTrain.Schema.Weight); + + int numFeatures = examplesToFeedTrain.Schema.Feature.Type.VectorSize; + ch.Check(numFeatures > 0, "Training set has no features, aborting training."); + return examplesToFeedTrain; + } + + protected abstract TModel TrainCore(IChannel ch, RoleMappedData data, LinearPredictor predictor, int weightSetCount); + + protected abstract void CheckLabel(RoleMappedData examples, out int weightSetCount); + } +} diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/CrossValidation.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/CrossValidation.cs index fb6c56674b..20cae3cdaa 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/CrossValidation.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/CrossValidation.cs @@ -31,7 +31,7 @@ void New_CrossValidation() .FitAndRead(new MultiFileSource(dataPath)); // Pipeline. var pipeline = new MyTextTransform(env, MakeSentimentTextTransformArgs()) - .Append(new MySdca(env, new LinearClassificationTrainer.Arguments + .Append(new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1, ConvergenceTolerance = 1f diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/DecomposableTrainAndPredict.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/DecomposableTrainAndPredict.cs index eb8a6991c5..adb79f53d3 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/DecomposableTrainAndPredict.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/DecomposableTrainAndPredict.cs @@ -31,7 +31,7 @@ void New_DecomposableTrainAndPredict() var pipeline = new MyConcatTransform(env, "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth") .Append(new MyTermTransform(env, "Label"), TransformerScope.TrainTest) - .Append(new MySdcaMulticlass(env, new SdcaMultiClassTrainer.Arguments { MaxIterations = 100, Shuffle = true, NumThreads = 1 }, "Features", "Label")) + .Append(new SdcaMultiClassTrainer(env, new SdcaMultiClassTrainer.Arguments { MaxIterations = 100, Shuffle = true, NumThreads = 1 }, "Features", "Label")) .Append(new MyKeyToValueTransform(env, "PredictedLabel")); var model = pipeline.Fit(data).GetModelFor(TransformerScope.Scoring); diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Evaluation.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Evaluation.cs index dcb7f515c2..c5b01834ca 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Evaluation.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Evaluation.cs @@ -27,7 +27,7 @@ public void New_Evaluation() // Pipeline. var pipeline = new MyTextLoader(env, MakeSentimentTextLoaderArgs()) .Append(new MyTextTransform(env, MakeSentimentTextTransformArgs())) - .Append(new MySdca(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); + .Append(new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); // Train. var model = pipeline.Fit(new MultiFileSource(dataPath)); diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Extensibility.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Extensibility.cs index 53cfba2fbf..f95b555c1d 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Extensibility.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Extensibility.cs @@ -39,7 +39,7 @@ void New_Extensibility() var pipeline = new MyConcatTransform(env, "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth") .Append(new MyLambdaTransform(env, action)) .Append(new MyTermTransform(env, "Label"), TransformerScope.TrainTest) - .Append(new MySdcaMulticlass(env, new SdcaMultiClassTrainer.Arguments { MaxIterations = 100, Shuffle = true, NumThreads = 1 }, "Features", "Label")) + .Append(new SdcaMultiClassTrainer(env, new SdcaMultiClassTrainer.Arguments { MaxIterations = 100, Shuffle = true, NumThreads = 1 }, "Features", "Label")) .Append(new MyKeyToValueTransform(env, "PredictedLabel")); var model = pipeline.Fit(data).GetModelFor(TransformerScope.Scoring); diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/FileBasedSavingOfData.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/FileBasedSavingOfData.cs index 6e355f8db2..f5ffd8f86d 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/FileBasedSavingOfData.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/FileBasedSavingOfData.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using Microsoft.ML.Runtime; using Microsoft.ML.Runtime.Data; using Microsoft.ML.Runtime.Data.IO; using Microsoft.ML.Runtime.Learners; @@ -35,11 +36,11 @@ void New_FileBasedSavingOfData() using (var file = env.CreateOutputFile("i.idv")) trainData.SaveAsBinary(env, file.CreateWriteStream()); - var trainer = new MySdca(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label"); + var trainer = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label"); var loadedTrainData = new BinaryLoader(env, new BinaryLoader.Arguments(), new MultiFileSource("i.idv")); // Train. - var model = trainer.Train(loadedTrainData); + var model = trainer.Train(new RoleMappedData(loadedTrainData, DefaultColumnNames.Label, DefaultColumnNames.Features)); DeleteOutputPath("i.idv"); } } diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/IntrospectiveTraining.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/IntrospectiveTraining.cs index ae8f36183d..58d80736c6 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/IntrospectiveTraining.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/IntrospectiveTraining.cs @@ -41,14 +41,14 @@ public void New_IntrospectiveTraining() .FitAndRead(new MultiFileSource(dataPath)); var pipeline = new MyTextTransform(env, MakeSentimentTextTransformArgs()) - .Append(new MySdca(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); + .Append(new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); // Train. var model = pipeline.Fit(data); // Get feature weights. VBuffer weights = default; - model.LastTransformer.InnerModel.GetFeatureWeights(ref weights); + model.LastTransformer.Model.GetFeatureWeights(ref weights); } } } diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Metacomponents.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Metacomponents.cs index e6ed7469bb..0c5faf5f8e 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Metacomponents.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Metacomponents.cs @@ -27,7 +27,7 @@ public void New_Metacomponents() var data = new MyTextLoader(env, MakeIrisTextLoaderArgs()) .FitAndRead(new MultiFileSource(dataPath)); - var sdcaTrainer = new MySdca(env, new LinearClassificationTrainer.Arguments { MaxIterations = 100, Shuffle = true, NumThreads = 1 }, "Features", "Label"); + var sdcaTrainer = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { MaxIterations = 100, Shuffle = true, NumThreads = 1 }, "Features", "Label"); var pipeline = new MyConcatTransform(env, "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth") .Append(new MyTermTransform(env, "Label"), TransformerScope.TrainTest) .Append(new MyOva(env, sdcaTrainer)) diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/MultithreadedPrediction.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/MultithreadedPrediction.cs index e8386d166e..0d3ef500df 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/MultithreadedPrediction.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/MultithreadedPrediction.cs @@ -32,7 +32,7 @@ void New_MultithreadedPrediction() // Pipeline. var pipeline = new MyTextLoader(env, MakeSentimentTextLoaderArgs()) .Append(new MyTextTransform(env, MakeSentimentTextTransformArgs())) - .Append(new MySdca(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); + .Append(new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); // Train. var model = pipeline.Fit(new MultiFileSource(dataPath)); diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/ReconfigurablePrediction.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/ReconfigurablePrediction.cs index 12a6a2db20..51ca77df83 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/ReconfigurablePrediction.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/ReconfigurablePrediction.cs @@ -4,6 +4,7 @@ using Microsoft.ML.Models; using Microsoft.ML.Runtime.Data; +using Microsoft.ML.Runtime.Internal.Calibration; using Microsoft.ML.Runtime.Learners; using Xunit; @@ -35,14 +36,14 @@ public void New_ReconfigurablePrediction() var pipeline = new MyTextTransform(env, MakeSentimentTextTransformArgs()) .Fit(data); - var trainer = new MySdca(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label"); + var trainer = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label"); var trainData = pipeline.Transform(data); var model = trainer.Fit(trainData); var scoredTest = model.Transform(pipeline.Transform(testData)); var metrics = new MyBinaryClassifierEvaluator(env, new BinaryClassifierEvaluator.Arguments()).Evaluate(scoredTest, "Label", "Probability"); - var newModel = model.Clone(new BinaryClassifierScorer.Arguments { Threshold = 0.01f, ThresholdColumn = DefaultColumnNames.Probability }); + var newModel = new BinaryPredictionTransformer(env, model.Model, trainData.Schema, model.FeatureColumn, threshold: 0.01f, thresholdColumn: DefaultColumnNames.Probability); var newScoredTest = newModel.Transform(pipeline.Transform(testData)); var newMetrics = new MyBinaryClassifierEvaluator(env, new BinaryClassifierEvaluator.Arguments { Threshold = 0.01f, UseRawScoreThreshold = false }).Evaluate(newScoredTest, "Label", "Probability"); } diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/SimpleTrainAndPredict.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/SimpleTrainAndPredict.cs index 76c12e6068..5d7ff98a23 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/SimpleTrainAndPredict.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/SimpleTrainAndPredict.cs @@ -29,7 +29,7 @@ public void New_SimpleTrainAndPredict() // Pipeline. var pipeline = new MyTextLoader(env, MakeSentimentTextLoaderArgs()) .Append(new MyTextTransform(env, MakeSentimentTextTransformArgs())) - .Append(new MySdca(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); + .Append(new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); // Train. var model = pipeline.Fit(new MultiFileSource(dataPath)); diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainSaveModelAndPredict.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainSaveModelAndPredict.cs index f5b5b98c90..df9dbdc53a 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainSaveModelAndPredict.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainSaveModelAndPredict.cs @@ -30,7 +30,7 @@ public void New_TrainSaveModelAndPredict() // Pipeline. var pipeline = new MyTextLoader(env, MakeSentimentTextLoaderArgs()) .Append(new MyTextTransform(env, MakeSentimentTextTransformArgs())) - .Append(new MySdca(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); + .Append(new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); // Train. var model = pipeline.Fit(new MultiFileSource(dataPath)); diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainWithInitialPredictor.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainWithInitialPredictor.cs index 2e687c9138..7a13d57687 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainWithInitialPredictor.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainWithInitialPredictor.cs @@ -31,7 +31,7 @@ public void New_TrainWithInitialPredictor() // Train the first predictor. - var trainer = new MySdca(env, new LinearClassificationTrainer.Arguments + var trainer = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label"); @@ -39,7 +39,7 @@ public void New_TrainWithInitialPredictor() // Train the second predictor on the same data. var secondTrainer = new MyAveragedPerceptron(env, new AveragedPerceptronTrainer.Arguments(), "Features", "Label"); - var finalModel = secondTrainer.Train(trainData, firstModel.InnerModel); + var finalModel = secondTrainer.Train(trainData, firstModel.Model); } } } diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainWithValidationSet.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainWithValidationSet.cs index 25eebafc45..69b5ace454 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainWithValidationSet.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/TrainWithValidationSet.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using Microsoft.ML.Runtime.Data; +using Microsoft.ML.Runtime.Learners; using Xunit; namespace Microsoft.ML.Tests.Scenarios.Api @@ -31,7 +32,7 @@ public void New_TrainWithValidationSet() var validData = reader.Read(new MultiFileSource(validationDataPath)); // Train model with validation set. - var trainer = new MySdca(env, new Runtime.Learners.LinearClassificationTrainer.Arguments(), "Features", "Label"); + var trainer = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments(), "Features", "Label"); var model = trainer.Train(trainData, validData); } } diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Wrappers.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Wrappers.cs index 1c2156b3e5..d37ca8ae2c 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Wrappers.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Wrappers.cs @@ -175,12 +175,7 @@ public TransformWrapper(IHostEnvironment env, ModelLoadContext ctx) public IDataView Transform(IDataView input) => ApplyTransformUtils.ApplyAllTransformsToData(_env, _xf, input); } - public interface IPredictorTransformer : ITransformer - { - TModel InnerModel { get; } - } - - public class ScorerWrapper : TransformWrapper, IPredictorTransformer + public class ScorerWrapper : TransformWrapper, IPredictionTransformer where TModel : IPredictor { protected readonly string _featureColumn; @@ -189,10 +184,14 @@ public ScorerWrapper(IHostEnvironment env, IDataView scorer, TModel trainedModel : base(env, scorer) { _featureColumn = featureColumn; - InnerModel = trainedModel; + Model = trainedModel; } - public TModel InnerModel { get; } + public TModel Model { get; } + + public string FeatureColumn => _featureColumn; + + public ColumnType FeatureColumnType => throw new NotImplementedException(); } public class BinaryScorerWrapper : ScorerWrapper @@ -222,7 +221,7 @@ private static IDataView MakeScorer(IHostEnvironment env, ISchema schema, string public BinaryScorerWrapper Clone(BinaryClassifierScorer.Arguments scorerArgs) { var scorer = _xf as IDataScorerTransform; - return new BinaryScorerWrapper(_env, InnerModel, scorer.Source.Schema, _featureColumn, scorerArgs); + return new BinaryScorerWrapper(_env, Model, scorer.Source.Schema, _featureColumn, scorerArgs); } } @@ -249,13 +248,6 @@ public SchemaShape GetOutputSchema() } } - public interface ITrainerEstimator: IEstimator - where TTransformer: IPredictorTransformer - where TModel: IPredictor - { - TrainerInfo TrainerInfo { get; } - } - public abstract class TrainerBase : ITrainerEstimator where TTransformer : ScorerWrapper where TModel : IPredictor @@ -264,14 +256,16 @@ public abstract class TrainerBase : ITrainerEstimator, TWeightsPredictor> - { - private readonly LinearClassificationTrainer.Arguments _args; - - public MySdca(IHostEnvironment env, LinearClassificationTrainer.Arguments args, string featureCol, string labelCol) - : base(env, new TrainerInfo(), featureCol, labelCol) - { - _args = args; - } - - protected override TWeightsPredictor TrainCore(TrainContext context) => new LinearClassificationTrainer(_env, _args).Train(context); - - public ITransformer Train(IDataView trainData, IDataView validationData = null) => TrainTransformer(trainData, validationData); - - protected override BinaryScorerWrapper MakeScorer(TWeightsPredictor predictor, RoleMappedData data) - => new BinaryScorerWrapper(_env, predictor, data.Data.Schema, _featureCol, new BinaryClassifierScorer.Arguments()); - } - - public sealed class MySdcaMulticlass : TrainerBase, IPredictor> - { - private readonly SdcaMultiClassTrainer.Arguments _args; - - public MySdcaMulticlass(IHostEnvironment env, SdcaMultiClassTrainer.Arguments args, string featureCol, string labelCol) - : base(env, new TrainerInfo(), featureCol, labelCol) - { - _args = args; - } - - protected override ScorerWrapper MakeScorer(IPredictor predictor, RoleMappedData data) => MakeScorerBasic(predictor, data); - - protected override IPredictor TrainCore(TrainContext context) => new SdcaMultiClassTrainer(_env, _args).Train(context); - } - public sealed class MyAveragedPerceptron : TrainerBase, IPredictor> { private readonly AveragedPerceptronTrainer _trainer; + public override PredictionKind PredictionKind => PredictionKind.BinaryClassification; + public MyAveragedPerceptron(IHostEnvironment env, AveragedPerceptronTrainer.Arguments args, string featureCol, string labelCol) : base(env, new TrainerInfo(caching: false), featureCol, labelCol) { @@ -636,17 +599,19 @@ public SchemaShape GetOutputSchema(SchemaShape inputSchema) public sealed class MyOva : TrainerBase, OvaPredictor> { - private readonly ITrainerEstimator, TScalarPredictor> _binaryEstimator; + private readonly ITrainerEstimator, TScalarPredictor> _binaryEstimator; - public MyOva(IHostEnvironment env, ITrainerEstimator, TScalarPredictor> estimator, + public MyOva(IHostEnvironment env, ITrainerEstimator, TScalarPredictor> estimator, string featureColumn = DefaultColumnNames.Features, string labelColumn = DefaultColumnNames.Label) : base(env, MakeTrainerInfo(estimator), featureColumn, labelColumn) { _binaryEstimator = estimator; } - private static TrainerInfo MakeTrainerInfo(ITrainerEstimator, TScalarPredictor> estimator) - => new TrainerInfo(estimator.TrainerInfo.NeedNormalization, estimator.TrainerInfo.NeedCalibration, false); + public override PredictionKind PredictionKind => PredictionKind.MultiClassClassification; + + private static TrainerInfo MakeTrainerInfo(ITrainerEstimator, TScalarPredictor> estimator) + => new TrainerInfo(estimator.Info.NeedNormalization, estimator.Info.NeedCalibration, false); protected override ScorerWrapper MakeScorer(OvaPredictor predictor, RoleMappedData data) => MakeScorerBasic(predictor, data); @@ -656,13 +621,13 @@ protected override OvaPredictor TrainCore(TrainContext trainContext) var trainRoles = trainContext.TrainingSet; trainRoles.CheckMultiClassLabel(out var numClasses); - var predictors = new IPredictorTransformer[numClasses]; + var predictors = new IPredictionTransformer[numClasses]; for (int iClass = 0; iClass < numClasses; iClass++) { var data = new LabelIndicatorTransform(_env, trainRoles.Data, iClass, "Label"); predictors[iClass] = _binaryEstimator.Fit(data); } - var prs = predictors.Select(x => x.InnerModel); + var prs = predictors.Select(x => x.Model); var finalPredictor = OvaPredictor.Create(_env.Register("ova"), prs.ToArray()); return finalPredictor; } diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/ReconfigurablePrediction.cs b/test/Microsoft.ML.Tests/Scenarios/Api/ReconfigurablePrediction.cs index 5f87b60958..f7283cd8bb 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/ReconfigurablePrediction.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/ReconfigurablePrediction.cs @@ -3,7 +3,9 @@ // See the LICENSE file in the project root for more information. using Microsoft.ML.Models; +using Microsoft.ML.Runtime; using Microsoft.ML.Runtime.Data; +using Microsoft.ML.Runtime.Internal.Calibration; using Microsoft.ML.Runtime.Learners; using Xunit; @@ -38,7 +40,12 @@ void ReconfigurablePrediction() var cached = new CacheDataView(env, trans, prefetch: null); var trainRoles = new RoleMappedData(cached, label: "Label", feature: "Features"); - var predictor = trainer.Train(new Runtime.TrainContext(trainRoles)); + IPredictor predictor = trainer.Train(new Runtime.TrainContext(trainRoles)); + using (var ch = env.Start("Calibrator training")) + { + predictor = CalibratorUtils.TrainCalibrator(env, ch, new PlattCalibratorTrainer(env), int.MaxValue, predictor, trainRoles); + } + var scoreRoles = new RoleMappedData(trans, label: "Label", feature: "Features"); IDataScorerTransform scorer = ScoreUtils.GetScorer(predictor, scoreRoles, env, trainRoles.Schema); From 341beefa34bf918d29bd1080d311aaca83488703 Mon Sep 17 00:00:00 2001 From: Pete Luferenko Date: Wed, 22 Aug 2018 18:34:57 -0700 Subject: [PATCH 4/8] Changed the location of TrainerInfo and reinstated the polymorphic behavior of SDCA for the time being. --- src/Microsoft.ML.Core/Utilities/Contracts.cs | 1 - .../Training/ITrainerEstimator.cs | 9 +++++---- .../Training/TrainerEstimatorBase.cs | 7 ++----- .../Standard/LinearClassificationTrainer.cs | 17 +++++++++++------ .../Standard/StochasticTrainerBase.cs | 5 ++++- .../Api/Estimators/ReconfigurablePrediction.cs | 3 ++- 6 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.ML.Core/Utilities/Contracts.cs b/src/Microsoft.ML.Core/Utilities/Contracts.cs index 1518120f84..bcfc43164e 100644 --- a/src/Microsoft.ML.Core/Utilities/Contracts.cs +++ b/src/Microsoft.ML.Core/Utilities/Contracts.cs @@ -22,7 +22,6 @@ namespace Microsoft.ML.Runtime.Internal namespace Microsoft.ML.Runtime #endif { - using System.Runtime.Serialization; using Conditional = System.Diagnostics.ConditionalAttribute; using Debug = System.Diagnostics.Debug; diff --git a/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs b/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs index fbab940a77..4eb6ea1482 100644 --- a/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs +++ b/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs @@ -1,7 +1,8 @@ -using Microsoft.ML.Core.Data; -using System; -using System.Collections.Generic; -using System.Text; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.ML.Core.Data; namespace Microsoft.ML.Runtime.Training { diff --git a/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs b/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs index 3d7ec7e80e..62625604ab 100644 --- a/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs +++ b/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs @@ -44,21 +44,18 @@ public abstract class TrainerEstimatorBase : ITrainerEstim /// /// The information about the trainer: whether it benefits from normalization, caching etc. /// - public TrainerInfo Info { get; } + public abstract TrainerInfo Info { get; } public abstract PredictionKind PredictionKind { get; } - public TrainerEstimatorBase(IHost host, TrainerInfo trainerInfo, - SchemaShape.Column feature, SchemaShape.Column label, SchemaShape.Column weight = null) + public TrainerEstimatorBase(IHost host, SchemaShape.Column feature, SchemaShape.Column label, SchemaShape.Column weight = null) { Contracts.CheckValue(host, nameof(host)); Host = host; - Host.CheckValue(trainerInfo, nameof(trainerInfo)); Host.CheckValue(feature, nameof(feature)); Host.CheckValueOrNull(label); Host.CheckValueOrNull(weight); - Info = trainerInfo; FeatureColumn = feature; LabelColumn = label; WeightColumn = weight; diff --git a/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs b/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs index a7187dd640..f633f69ebc 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs @@ -1358,7 +1358,7 @@ public void Add(Double summand) } } - public sealed class LinearClassificationTrainer : SdcaTrainerBase, ParameterMixingCalibratedPredictor> + public sealed class LinearClassificationTrainer : SdcaTrainerBase, TScalarPredictor> { public const string LoadNameValue = "SDCA"; public const string UserNameValue = "Fast Linear (SA-SDCA)"; @@ -1392,7 +1392,9 @@ internal override void Check(IHostEnvironment env) protected override SchemaShape.Column[] OutputColumns { get; } - public override PredictionKind PredictionKind => throw new NotImplementedException(); + public override PredictionKind PredictionKind => PredictionKind.BinaryClassification; + + public override TrainerInfo Info { get; } public LinearClassificationTrainer(IHostEnvironment env, Arguments args, string featureColumn, string labelColumn, string weightColumn = null) @@ -1400,6 +1402,7 @@ public LinearClassificationTrainer(IHostEnvironment env, Arguments args, { _loss = args.LossFunction.CreateComponent(env); Loss = _loss; + Info = new TrainerInfo(calibration: !(_loss is LogLoss)); _args = args; _positiveInstanceWeight = _args.PositiveInstanceWeight; OutputColumns = new[] @@ -1444,7 +1447,7 @@ private static SchemaShape.Column MakeFeatureColumn(string featureColumn) return new SchemaShape.Column(featureColumn, SchemaShape.Column.VectorKind.Vector, DataKind.R4, false, new string[0]); } - protected override ParameterMixingCalibratedPredictor CreatePredictor(VBuffer[] weights, Float[] bias) + protected override TScalarPredictor CreatePredictor(VBuffer[] weights, Float[] bias) { Host.CheckParam(Utils.Size(weights) == 1, nameof(weights)); Host.CheckParam(Utils.Size(bias) == 1, nameof(bias)); @@ -1455,6 +1458,8 @@ protected override ParameterMixingCalibratedPredictor CreatePredictor(VBuffer(NumberType.Float)); var predictor = new LinearBinaryPredictor(Host, ref maybeSparseWeights, bias[0]); + if (!(_loss is LogLoss)) + return predictor; return new ParameterMixingCalibratedPredictor(Host, predictor, new PlattCalibrator(Host, -1, 0)); } @@ -1469,10 +1474,10 @@ protected override void CheckLabel(RoleMappedData examples, out int weightSetCou weightSetCount = 1; } - protected override BinaryPredictionTransformer MakeTransformer(ParameterMixingCalibratedPredictor model, ISchema trainSchema) - => new BinaryPredictionTransformer(Host, model, trainSchema, FeatureColumn.Name); + protected override BinaryPredictionTransformer MakeTransformer(TScalarPredictor model, ISchema trainSchema) + => new BinaryPredictionTransformer(Host, model, trainSchema, FeatureColumn.Name); - public BinaryPredictionTransformer Train(IDataView trainData, IDataView validationData) => TrainTransformer(trainData, validationData); + public BinaryPredictionTransformer Train(IDataView trainData, IDataView validationData) => TrainTransformer(trainData, validationData); } public sealed class StochasticGradientDescentClassificationTrainer : diff --git a/src/Microsoft.ML.StandardLearners/Standard/StochasticTrainerBase.cs b/src/Microsoft.ML.StandardLearners/Standard/StochasticTrainerBase.cs index 2338247c8d..7fc70ce621 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/StochasticTrainerBase.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/StochasticTrainerBase.cs @@ -15,7 +15,7 @@ public abstract class StochasticTrainerBase : TrainerEstim where TModel : IPredictor { public StochasticTrainerBase(IHost host, SchemaShape.Column feature, SchemaShape.Column label, SchemaShape.Column weight = null) - : base(host, new TrainerInfo(), feature, label, weight) + : base(host, feature, label, weight) { } @@ -24,6 +24,9 @@ public StochasticTrainerBase(IHost host, SchemaShape.Column feature, SchemaShape /// protected abstract bool ShuffleData { get; } + private static readonly TrainerInfo _info = new TrainerInfo(); + public override TrainerInfo Info => _info; + protected override TModel TrainModelCore(TrainContext context) { Host.CheckValue(context, nameof(context)); diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/ReconfigurablePrediction.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/ReconfigurablePrediction.cs index 51ca77df83..1282519f92 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/ReconfigurablePrediction.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/ReconfigurablePrediction.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using Microsoft.ML.Models; +using Microsoft.ML.Runtime; using Microsoft.ML.Runtime.Data; using Microsoft.ML.Runtime.Internal.Calibration; using Microsoft.ML.Runtime.Learners; @@ -43,7 +44,7 @@ public void New_ReconfigurablePrediction() var scoredTest = model.Transform(pipeline.Transform(testData)); var metrics = new MyBinaryClassifierEvaluator(env, new BinaryClassifierEvaluator.Arguments()).Evaluate(scoredTest, "Label", "Probability"); - var newModel = new BinaryPredictionTransformer(env, model.Model, trainData.Schema, model.FeatureColumn, threshold: 0.01f, thresholdColumn: DefaultColumnNames.Probability); + var newModel = new BinaryPredictionTransformer>(env, model.Model, trainData.Schema, model.FeatureColumn, threshold: 0.01f, thresholdColumn: DefaultColumnNames.Probability); var newScoredTest = newModel.Transform(pipeline.Transform(testData)); var newMetrics = new MyBinaryClassifierEvaluator(env, new BinaryClassifierEvaluator.Arguments { Threshold = 0.01f, UseRawScoreThreshold = false }).Evaluate(newScoredTest, "Label", "Probability"); } From b9e553fbac56642bb8c2b9c09831e0287b2bc2bc Mon Sep 17 00:00:00 2001 From: Pete Luferenko Date: Wed, 22 Aug 2018 18:48:09 -0700 Subject: [PATCH 5/8] Made everything else save-able --- .../Scorers/PredictionTransformer.cs | 52 +++++++++++++++++-- .../Standard/SdcaMultiClass.cs | 2 +- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs b/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs index f02e4bb79e..5dba5430a0 100644 --- a/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs +++ b/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs @@ -12,6 +12,12 @@ [assembly: LoadableClass(typeof(BinaryPredictionTransformer>), typeof(BinaryPredictionTransformer), null, typeof(SignatureLoadModel), "", BinaryPredictionTransformer.LoaderSignature)] +[assembly: LoadableClass(typeof(MulticlassPredictionTransformer>>), typeof(MulticlassPredictionTransformer), null, typeof(SignatureLoadModel), + "", MulticlassPredictionTransformer.LoaderSignature)] + +[assembly: LoadableClass(typeof(RegressionPredictionTransformer>), typeof(RegressionPredictionTransformer), null, typeof(SignatureLoadModel), + "", RegressionPredictionTransformer.LoaderSignature)] + namespace Microsoft.ML.Runtime.Data { public abstract class PredictionTransformerBase : IPredictionTransformer, ICanSaveModel @@ -141,8 +147,13 @@ public BinaryPredictionTransformer(IHostEnvironment env, TModel model, ISchema i public BinaryPredictionTransformer(IHostEnvironment env, ModelLoadContext ctx) : base(Contracts.CheckRef(env, nameof(env)).Register(nameof(BinaryPredictionTransformer)), ctx) { + // *** Binary format *** + // + // float: scorer threshold + // id of string: scorer threshold column + Threshold = ctx.Reader.ReadSingle(); - ThresholdColumn = ctx.LoadStringOrNull(); + ThresholdColumn = ctx.LoadString(); var schema = new RoleMappedSchema(TrainSchema, null, FeatureColumn); var args = new BinaryClassifierScorer.Arguments { Threshold = Threshold, ThresholdColumn = ThresholdColumn }; @@ -167,7 +178,7 @@ protected override void SaveCore(ModelSaveContext ctx) base.SaveCore(ctx); ctx.Writer.Write(Threshold); - ctx.SaveStringOrNull(ThresholdColumn); + ctx.SaveString(ThresholdColumn); } private static VersionInfo GetVersionInfo() @@ -185,14 +196,33 @@ public sealed class MulticlassPredictionTransformer : PredictionTransfor where TModel : class, IPredictorProducing> { private readonly MultiClassClassifierScorer _scorer; + private readonly string _trainLabelColumn; - public MulticlassPredictionTransformer(IHostEnvironment env, TModel model, ISchema inputSchema, string featureColumn, string labelColumn, MultiClassClassifierScorer.Arguments args) + public MulticlassPredictionTransformer(IHostEnvironment env, TModel model, ISchema inputSchema, string featureColumn, string labelColumn) : base(Contracts.CheckRef(env, nameof(env)).Register(nameof(MulticlassPredictionTransformer)), model, inputSchema, featureColumn) { + Host.CheckValueOrNull(labelColumn); + + _trainLabelColumn = labelColumn; var schema = new RoleMappedSchema(inputSchema, labelColumn, featureColumn); + var args = new MultiClassClassifierScorer.Arguments(); _scorer = new MultiClassClassifierScorer(Host, args, new EmptyDataView(Host, inputSchema), BindableMapper.Bind(Host, schema), schema); } + public MulticlassPredictionTransformer(IHostEnvironment env, ModelLoadContext ctx) + : base(Contracts.CheckRef(env, nameof(env)).Register(nameof(MulticlassPredictionTransformer)), ctx) + { + // *** Binary format *** + // + // id of string: train label column + + _trainLabelColumn = ctx.LoadStringOrNull(); + + var schema = new RoleMappedSchema(TrainSchema, _trainLabelColumn, FeatureColumn); + var args = new MultiClassClassifierScorer.Arguments(); + _scorer = new MultiClassClassifierScorer(Host, args, new EmptyDataView(Host, TrainSchema), BindableMapper.Bind(Host, schema), schema); + } + public override IDataView Transform(IDataView input) { Host.CheckValue(input, nameof(input)); @@ -206,7 +236,10 @@ protected override void SaveCore(ModelSaveContext ctx) // *** Binary format *** // + // id of string: train label column base.SaveCore(ctx); + + ctx.SaveStringOrNull(_trainLabelColumn); } private static VersionInfo GetVersionInfo() @@ -232,6 +265,13 @@ public RegressionPredictionTransformer(IHostEnvironment env, TModel model, ISche _scorer = new GenericScorer(Host, new GenericScorer.Arguments(), new EmptyDataView(Host, inputSchema), BindableMapper.Bind(Host, schema), schema); } + internal RegressionPredictionTransformer(IHostEnvironment env, ModelLoadContext ctx) + : base(Contracts.CheckRef(env, nameof(env)).Register(nameof(RegressionPredictionTransformer)), ctx) + { + var schema = new RoleMappedSchema(TrainSchema, null, FeatureColumn); + _scorer = new GenericScorer(Host, new GenericScorer.Arguments(), new EmptyDataView(Host, TrainSchema), BindableMapper.Bind(Host, schema), schema); + } + public override IDataView Transform(IDataView input) { Host.CheckValue(input, nameof(input)); @@ -270,10 +310,16 @@ public static BinaryPredictionTransformer> Create(IHo internal static class MulticlassPredictionTransformer { public const string LoaderSignature = "MulticlassPredXfer"; + + public static MulticlassPredictionTransformer>> Create(IHostEnvironment env, ModelLoadContext ctx) + => new MulticlassPredictionTransformer>>(env, ctx); } internal static class RegressionPredictionTransformer { public const string LoaderSignature = "RegressionPredXfer"; + + public static RegressionPredictionTransformer> Create(IHostEnvironment env, ModelLoadContext ctx) + => new RegressionPredictionTransformer>(env, ctx); } } diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs index 6215a3588d..8d0371c38d 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs @@ -406,7 +406,7 @@ protected override Float GetInstanceWeight(FloatLabelCursor cursor) } protected override MulticlassPredictionTransformer MakeTransformer(MulticlassLogisticRegressionPredictor model, ISchema trainSchema) - => new MulticlassPredictionTransformer(Host, model, trainSchema, FeatureColumn.Name, LabelColumn.Name, new MultiClassClassifierScorer.Arguments()); + => new MulticlassPredictionTransformer(Host, model, trainSchema, FeatureColumn.Name, LabelColumn.Name); } /// From f48bf6da36a3ac4892c73ac9db68c73b9f554272 Mon Sep 17 00:00:00 2001 From: Pete Luferenko Date: Thu, 23 Aug 2018 18:25:17 -0700 Subject: [PATCH 6/8] Fixed analyzer --- tools-local/Microsoft.ML.CodeAnalyzer/ContractsCheckAnalyzer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools-local/Microsoft.ML.CodeAnalyzer/ContractsCheckAnalyzer.cs b/tools-local/Microsoft.ML.CodeAnalyzer/ContractsCheckAnalyzer.cs index 3ac1917e6f..4f96d11e79 100644 --- a/tools-local/Microsoft.ML.CodeAnalyzer/ContractsCheckAnalyzer.cs +++ b/tools-local/Microsoft.ML.CodeAnalyzer/ContractsCheckAnalyzer.cs @@ -82,7 +82,7 @@ internal static class DecodeMessageWithLoadContextDiagnostic "Check", "CheckUserArg", "CheckParam", "CheckParamValue", "CheckRef", "CheckValue", "CheckNonEmpty", "CheckNonWhiteSpace", "CheckDecode", "CheckIO", "CheckAlive", "CheckValueOrNull", "Except", "ExceptUserArg", "ExceptParam", "ExceptParamValue", "ExceptValue", "ExceptEmpty", - "ExceptWhiteSpace", "ExceptDecode", "ExceptIO", "ExceptNotImpl", "ExceptNotSupp", + "ExceptWhiteSpace", "ExceptDecode", "ExceptIO", "ExceptNotImpl", "ExceptNotSupp", "ExceptSchemaMismatch" }); public override void Initialize(AnalysisContext context) From 189082ec1abae8a2fe0f14a37902a85fd0c62ff5 Mon Sep 17 00:00:00 2001 From: Pete Luferenko Date: Fri, 24 Aug 2018 08:00:14 -0700 Subject: [PATCH 7/8] PR comments --- src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs | 4 ++-- .../Standard/LinearClassificationTrainer.cs | 2 +- src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs b/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs index 5dba5430a0..4245e0ad6b 100644 --- a/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs +++ b/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs @@ -124,7 +124,7 @@ protected virtual void SaveCore(ModelSaveContext ctx) } public sealed class BinaryPredictionTransformer : PredictionTransformerBase - where TModel : class, IPredictorProducing + where TModel : class, IPredictorProducing { private readonly BinaryClassifierScorer _scorer; @@ -254,7 +254,7 @@ private static VersionInfo GetVersionInfo() } public sealed class RegressionPredictionTransformer : PredictionTransformerBase - where TModel : class, IPredictorProducing + where TModel : class, IPredictorProducing { private readonly GenericScorer _scorer; diff --git a/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs b/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs index f633f69ebc..3ac26accc2 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; +using Microsoft.ML.Core.Data; using Microsoft.ML.Runtime; using Microsoft.ML.Runtime.CommandLine; using Microsoft.ML.Runtime.Data; @@ -20,7 +21,6 @@ using Microsoft.ML.Runtime.Learners; using Microsoft.ML.Runtime.Numeric; using Microsoft.ML.Runtime.Training; -using Microsoft.ML.Core.Data; [assembly: LoadableClass(typeof(LinearClassificationTrainer), typeof(LinearClassificationTrainer.Arguments), new[] { typeof(SignatureBinaryClassifierTrainer), typeof(SignatureTrainer), typeof(SignatureFeatureScorerTrainer) }, diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs index 4eefbfd084..163da10e7b 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs @@ -5,6 +5,7 @@ using Float = System.Single; using System; +using Microsoft.ML.Core.Data; using Microsoft.ML.Runtime; using Microsoft.ML.Runtime.CommandLine; using Microsoft.ML.Runtime.Data; @@ -14,7 +15,6 @@ using Microsoft.ML.Runtime.Learners; using Microsoft.ML.Runtime.Training; using Microsoft.ML.Runtime.Internal.Internallearn; -using Microsoft.ML.Core.Data; [assembly: LoadableClass(SdcaRegressionTrainer.Summary, typeof(SdcaRegressionTrainer), typeof(SdcaRegressionTrainer.Arguments), new[] { typeof(SignatureRegressorTrainer), typeof(SignatureTrainer), typeof(SignatureFeatureScorerTrainer) }, From d54f7e7d6f0685f45be9d61edeed4becc0fe9de6 Mon Sep 17 00:00:00 2001 From: Pete Luferenko Date: Fri, 24 Aug 2018 10:52:49 -0700 Subject: [PATCH 8/8] PR comments --- .../Scorers/PredictionTransformer.cs | 30 ++++++++++--------- .../Standard/LinearClassificationTrainer.cs | 4 +-- .../Standard/SdcaMultiClass.cs | 4 +-- .../Scenarios/Api/Estimators/Wrappers.cs | 2 +- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs b/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs index 4245e0ad6b..f04febd055 100644 --- a/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs +++ b/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs @@ -23,6 +23,9 @@ namespace Microsoft.ML.Runtime.Data public abstract class PredictionTransformerBase : IPredictionTransformer, ICanSaveModel where TModel : class, IPredictor { + private const string DirModel = "Model"; + private const string DirTransSchema = "TrainSchema"; + protected readonly IHost Host; protected readonly ISchemaBindableMapper BindableMapper; protected readonly ISchema TrainSchema; @@ -41,9 +44,8 @@ public PredictionTransformerBase(IHost host, TModel model, ISchema trainSchema, Model = model; FeatureColumn = featureColumn; - trainSchema.TryGetColumnIndex(featureColumn, out int col); - if (col < 0) - throw Host.ExceptSchemaMismatch(nameof(featureColumn), "feature", featureColumn); + if (!trainSchema.TryGetColumnIndex(featureColumn, out int col)) + throw Host.ExceptSchemaMismatch(nameof(featureColumn), RoleMappedSchema.ColumnRole.Feature.Value, featureColumn); FeatureColumnType = trainSchema.GetColumnType(col); TrainSchema = trainSchema; @@ -54,15 +56,17 @@ internal PredictionTransformerBase(IHost host, ModelLoadContext ctx) { Host = host; - ctx.LoadModel(host, out TModel model, "Model"); + ctx.LoadModel(host, out TModel model, DirModel); Model = model; // *** Binary format *** // model: prediction model. // stream: empty data view that contains train schema. // id of string: feature column. + + // Clone the stream with the schema into memory. var ms = new MemoryStream(); - ctx.TryLoadBinaryStream("TrainSchema", reader => + ctx.TryLoadBinaryStream(DirTransSchema, reader => { reader.BaseStream.CopyTo(ms); }); @@ -72,9 +76,8 @@ internal PredictionTransformerBase(IHost host, ModelLoadContext ctx) TrainSchema = loader.Schema; FeatureColumn = ctx.LoadString(); - TrainSchema.TryGetColumnIndex(FeatureColumn, out int col); - if (col < 0) - throw Host.ExceptSchemaMismatch(nameof(FeatureColumn), "feature", FeatureColumn); + if (!TrainSchema.TryGetColumnIndex(FeatureColumn, out int col)) + throw Host.ExceptSchemaMismatch(nameof(FeatureColumn), RoleMappedSchema.ColumnRole.Feature.Value, FeatureColumn); FeatureColumnType = TrainSchema.GetColumnType(col); BindableMapper = ScoreUtils.GetSchemaBindableMapper(Host, model); @@ -84,11 +87,10 @@ public ISchema GetOutputSchema(ISchema inputSchema) { Host.CheckValue(inputSchema, nameof(inputSchema)); - inputSchema.TryGetColumnIndex(FeatureColumn, out int col); - if (col < 0) - throw Host.ExceptSchemaMismatch(nameof(inputSchema), "feature", FeatureColumn, FeatureColumnType.ToString(), null); + if (!inputSchema.TryGetColumnIndex(FeatureColumn, out int col)) + throw Host.ExceptSchemaMismatch(nameof(inputSchema), RoleMappedSchema.ColumnRole.Feature.Value, FeatureColumn, FeatureColumnType.ToString(), null); if (!inputSchema.GetColumnType(col).Equals(FeatureColumnType)) - throw Host.ExceptSchemaMismatch(nameof(inputSchema), "feature", FeatureColumn, FeatureColumnType.ToString(), inputSchema.GetColumnType(col).ToString()); + throw Host.ExceptSchemaMismatch(nameof(inputSchema), RoleMappedSchema.ColumnRole.Feature.Value, FeatureColumn, FeatureColumnType.ToString(), inputSchema.GetColumnType(col).ToString()); return Transform(new EmptyDataView(Host, inputSchema)).Schema; } @@ -109,8 +111,8 @@ protected virtual void SaveCore(ModelSaveContext ctx) // stream: empty data view that contains train schema. // id of string: feature column. - ctx.SaveModel(Model, "Model"); - ctx.SaveBinaryStream("TrainSchema", writer => + ctx.SaveModel(Model, DirModel); + ctx.SaveBinaryStream(DirTransSchema, writer => { using (var ch = Host.Start("Saving train schema")) { diff --git a/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs b/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs index 3ac26accc2..2da2075728 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs @@ -1422,7 +1422,7 @@ protected override void CheckLabelCompatible(SchemaShape.Column labelCol) Contracts.AssertValue(labelCol); Action error = - () => throw Host.ExceptSchemaMismatch(nameof(labelCol), "label", labelCol.Name, "BL, R8, R4 or a Key", labelCol.GetTypeString()); + () => throw Host.ExceptSchemaMismatch(nameof(labelCol), RoleMappedSchema.ColumnRole.Label.Value, labelCol.Name, "BL, R8, R4 or a Key", labelCol.GetTypeString()); if (labelCol.Kind != SchemaShape.Column.VectorKind.Scalar) error(); @@ -1444,7 +1444,7 @@ private static SchemaShape.Column MakeLabelColumn(string labelColumn) private static SchemaShape.Column MakeFeatureColumn(string featureColumn) { - return new SchemaShape.Column(featureColumn, SchemaShape.Column.VectorKind.Vector, DataKind.R4, false, new string[0]); + return new SchemaShape.Column(featureColumn, SchemaShape.Column.VectorKind.Vector, DataKind.R4, false); } protected override TScalarPredictor CreatePredictor(VBuffer[] weights, Float[] bias) diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs index 8d0371c38d..f775be92bd 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs @@ -72,7 +72,7 @@ protected override void CheckLabelCompatible(SchemaShape.Column labelCol) Contracts.AssertValue(labelCol); Action error = - () => throw Host.ExceptSchemaMismatch(nameof(labelCol), "label", labelCol.Name, "R8, R4 or a Key", labelCol.GetTypeString()); + () => throw Host.ExceptSchemaMismatch(nameof(labelCol), RoleMappedSchema.ColumnRole.Label.Value, labelCol.Name, "R8, R4 or a Key", labelCol.GetTypeString()); if (labelCol.Kind != SchemaShape.Column.VectorKind.Scalar) error(); @@ -94,7 +94,7 @@ private static SchemaShape.Column MakeLabelColumn(string labelColumn) private static SchemaShape.Column MakeFeatureColumn(string featureColumn) { - return new SchemaShape.Column(featureColumn, SchemaShape.Column.VectorKind.Vector, DataKind.R4, false, new string[0]); + return new SchemaShape.Column(featureColumn, SchemaShape.Column.VectorKind.Vector, DataKind.R4, false); } /// diff --git a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Wrappers.cs b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Wrappers.cs index d37ca8ae2c..0efeedee2f 100644 --- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Wrappers.cs +++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Wrappers.cs @@ -191,7 +191,7 @@ public ScorerWrapper(IHostEnvironment env, IDataView scorer, TModel trainedModel public string FeatureColumn => _featureColumn; - public ColumnType FeatureColumnType => throw new NotImplementedException(); + public ColumnType FeatureColumnType => throw _env.ExceptNotSupp(); } public class BinaryScorerWrapper : ScorerWrapper