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..bcfc43164e 100644
--- a/src/Microsoft.ML.Core/Utilities/Contracts.cs
+++ b/src/Microsoft.ML.Core/Utilities/Contracts.cs
@@ -284,9 +284,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 +448,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 +746,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
new file mode 100644
index 0000000000..899c7622dc
--- /dev/null
+++ b/src/Microsoft.ML.Data/Prediction/IPredictionTransformer.cs
@@ -0,0 +1,23 @@
+// 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; }
+ }
+}
diff --git a/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs b/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs
new file mode 100644
index 0000000000..f04febd055
--- /dev/null
+++ b/src/Microsoft.ML.Data/Scorers/PredictionTransformer.cs
@@ -0,0 +1,327 @@
+// 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)]
+
+[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
+ 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;
+
+ 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;
+ if (!trainSchema.TryGetColumnIndex(featureColumn, out int col))
+ throw Host.ExceptSchemaMismatch(nameof(featureColumn), RoleMappedSchema.ColumnRole.Feature.Value, 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, 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(DirTransSchema, reader =>
+ {
+ reader.BaseStream.CopyTo(ms);
+ });
+
+ ms.Position = 0;
+ var loader = new BinaryLoader(host, new BinaryLoader.Arguments(), ms);
+ TrainSchema = loader.Schema;
+
+ FeatureColumn = ctx.LoadString();
+ 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);
+ }
+
+ public ISchema GetOutputSchema(ISchema inputSchema)
+ {
+ Host.CheckValue(inputSchema, nameof(inputSchema));
+
+ 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), RoleMappedSchema.ColumnRole.Feature.Value, 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, DirModel);
+ ctx.SaveBinaryStream(DirTransSchema, writer =>
+ {
+ using (var ch = Host.Start("Saving train schema"))
+ {
+ var saver = new BinarySaver(Host, new BinarySaver.Arguments { Silent = true });
+ DataSaverUtils.SaveDataView(ch, saver, new EmptyDataView(Host, TrainSchema), writer.BaseStream);
+ }
+ });
+
+ ctx.SaveString(FeatureColumn);
+ }
+ }
+
+ 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)
+ {
+ // *** Binary format ***
+ //
+ // float: scorer threshold
+ // id of string: scorer threshold column
+
+ Threshold = ctx.Reader.ReadSingle();
+ ThresholdColumn = ctx.LoadString();
+
+ 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.SaveString(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;
+ private readonly string _trainLabelColumn;
+
+ 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));
+ return _scorer.ApplyToData(Host, input);
+ }
+
+ protected override void SaveCore(ModelSaveContext ctx)
+ {
+ Contracts.AssertValue(ctx);
+ ctx.SetVersionInfo(GetVersionInfo());
+
+ // *** Binary format ***
+ //
+ // id of string: train label column
+ base.SaveCore(ctx);
+
+ ctx.SaveStringOrNull(_trainLabelColumn);
+ }
+
+ 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);
+ }
+
+ 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));
+ 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";
+
+ 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.Data/Training/ITrainerEstimator.cs b/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs
new file mode 100644
index 0000000000..4eb6ea1482
--- /dev/null
+++ b/src/Microsoft.ML.Data/Training/ITrainerEstimator.cs
@@ -0,0 +1,17 @@
+// 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
+{
+ public interface ITrainerEstimator: IEstimator
+ where TTransformer: IPredictionTransformer
+ where TPredictor: IPredictor
+ {
+ TrainerInfo Info { get; }
+
+ PredictionKind PredictionKind { 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..62625604ab
--- /dev/null
+++ b/src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs
@@ -0,0 +1,153 @@
+// 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;
+
+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, ITrainer
+ 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;
+
+ ///
+ /// 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 abstract TrainerInfo Info { get; }
+
+ public abstract PredictionKind PredictionKind { 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;
+ }
+
+ 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 = Info.WantCaching ? new CacheDataView(Host, trainSet, prefetch: null) : trainSet;
+
+ var trainRoles = MakeRoles(cachedTrain);
+
+ RoleMappedData validRoles;
+
+ if (validationSet == null)
+ validRoles = null;
+ else
+ {
+ var cachedValid = Info.WantCaching ? new CacheDataView(Host, validationSet, prefetch: null) : validationSet;
+ validRoles = MakeRoles(cachedValid);
+ }
+
+ var pred = TrainModelCore(new TrainContext(trainRoles, validRoles, initPredictor));
+ return MakeTransformer(pred, trainSet.Schema);
+ }
+
+ protected abstract TModel TrainModelCore(TrainContext trainContext);
+
+ 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);
+
+ 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..2da2075728 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;
@@ -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, TScalarPredictor>
{
public const string LoadNameValue = "SDCA";
public const string UserNameValue = "Fast Linear (SA-SDCA)";
@@ -1383,19 +1390,61 @@ internal override void Check(IHostEnvironment env)
protected override bool ShuffleData => _args.Shuffle;
+ protected override SchemaShape.Column[] OutputColumns { get; }
+
public override PredictionKind PredictionKind => PredictionKind.BinaryClassification;
public override TrainerInfo Info { get; }
- 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;
+ Loss = _loss;
Info = new TrainerInfo(calibration: !(_loss is LogLoss));
- NeedShuffle = args.Shuffle;
_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), RoleMappedSchema.ColumnRole.Label.Value, 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);
+ }
+
+ private static SchemaShape.Column MakeFeatureColumn(string featureColumn)
+ {
+ return new SchemaShape.Column(featureColumn, SchemaShape.Column.VectorKind.Vector, DataKind.R4, false);
}
protected override TScalarPredictor CreatePredictor(VBuffer[] weights, Float[] bias)
@@ -1407,6 +1456,7 @@ 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;
@@ -1423,6 +1473,11 @@ protected override void CheckLabel(RoleMappedData examples, out int weightSetCou
examples.CheckBinaryLabel();
weightSetCount = 1;
}
+
+ 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 sealed class StochasticGradientDescentClassificationTrainer :
@@ -1748,7 +1803,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..f775be92bd 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), RoleMappedSchema.ColumnRole.Label.Value, 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);
}
///
@@ -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);
}
///
diff --git a/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs b/src/Microsoft.ML.StandardLearners/Standard/SdcaRegression.cs
index bfa0796bed..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;
@@ -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..7fc70ce621
--- /dev/null
+++ b/src/Microsoft.ML.StandardLearners/Standard/StochasticTrainerBase.cs
@@ -0,0 +1,104 @@
+// 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, feature, label, weight)
+ {
+ }
+
+ ///
+ /// Whether data is to be shuffled every epoch.
+ ///
+ 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));
+ 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..1282519f92 100644
--- a/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/ReconfigurablePrediction.cs
+++ b/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/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;
@@ -35,14 +37,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..0efeedee2f 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 _env.ExceptNotSupp();
}
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);
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)