-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Converted LpNorm, GcNorm and Whitening transforms into transformers/estimators… #961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
// 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.Data.StaticPipe.Runtime; | ||
using Microsoft.ML.Runtime; | ||
using Microsoft.ML.Runtime.Data; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using static Microsoft.ML.Runtime.Data.LpNormNormalizerTransform; | ||
|
||
namespace Microsoft.ML.Transforms | ||
{ | ||
/// <include file='doc.xml' path='doc/members/member[@name="LpNormalize"]/*'/> | ||
public sealed class LpNormalizer : TrivialWrapperEstimator | ||
{ | ||
/// <include file='doc.xml' path='doc/members/member[@name="LpNormalize"]/*'/> | ||
/// <param name="env">The environment.</param> | ||
/// <param name="inputColumn">The column containing text to tokenize.</param> | ||
/// <param name="outputColumn">The column containing output tokens. Null means <paramref name="inputColumn"/> is replaced.</param> | ||
/// <param name="normKind">Type of norm to use to normalize each sample.</param> | ||
/// <param name="subMean">Subtract mean from each value before normalizing.</param> | ||
public LpNormalizer(IHostEnvironment env, string inputColumn, string outputColumn = null, NormalizerKind normKind = NormalizerKind.L2Norm, bool subMean = false) | ||
: this(env, new[] { (inputColumn, outputColumn ?? inputColumn) }, normKind, subMean) | ||
{ | ||
} | ||
|
||
/// <include file='doc.xml' path='doc/members/member[@name="LpNormalize"]/*'/> | ||
/// <param name="env">The environment.</param> | ||
/// <param name="columns">Pairs of columns to run the tokenization on.</param> | ||
/// <param name="normKind">Type of norm to use to normalize each sample.</param> | ||
/// <param name="subMean">Subtract mean from each value before normalizing.</param> | ||
public LpNormalizer(IHostEnvironment env, (string input, string output)[] columns, NormalizerKind normKind = NormalizerKind.L2Norm, bool subMean = false) | ||
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(LpNormalizer)), MakeTransformer(env, columns, normKind, subMean)) | ||
{ | ||
} | ||
|
||
private static TransformWrapper MakeTransformer(IHostEnvironment env, (string input, string output)[] columns, NormalizerKind normKind, bool subMean) | ||
{ | ||
Contracts.AssertValue(env); | ||
env.CheckNonEmpty(columns, nameof(columns)); | ||
foreach (var (input, output) in columns) | ||
{ | ||
env.CheckValue(input, nameof(input)); | ||
env.CheckValue(output, nameof(input)); | ||
} | ||
|
||
var args = new LpNormNormalizerTransform.Arguments | ||
{ | ||
Column = columns.Select(x => new LpNormNormalizerTransform.Column { Source = x.input, Name = x.output }).ToArray(), | ||
SubMean = subMean, | ||
NormKind = normKind | ||
}; | ||
|
||
// Create a valid instance of data. | ||
var schema = new SimpleSchema(env, columns.Select(x => new KeyValuePair<string, ColumnType>(x.input, new VectorType(NumberType.R4))).ToArray()); | ||
var emptyData = new EmptyDataView(env, schema); | ||
|
||
return new TransformWrapper(env, new LpNormNormalizerTransform(env, args, emptyData)); | ||
} | ||
} | ||
|
||
/// <include file='doc.xml' path='doc/members/member[@name="GcNormalize"]/*'/> | ||
public sealed class GcNormalizer : TrivialWrapperEstimator | ||
{ | ||
/// <include file='doc.xml' path='doc/members/member[@name="GcNormalize"]/*'/> | ||
/// <param name="env">The environment.</param> | ||
/// <param name="inputColumn">The column containing text to tokenize.</param> | ||
/// <param name="outputColumn">The column containing output tokens. Null means <paramref name="inputColumn"/> is replaced.</param> | ||
/// <param name="subMean">Subtract mean from each value before normalizing.</param> | ||
/// <param name="useStdDev">Normalize by standard deviation rather than L2 norm.</param> | ||
/// <param name="scale">Scale features by this value.</param> | ||
public GcNormalizer(IHostEnvironment env, string inputColumn, string outputColumn = null, bool subMean = true, bool useStdDev = false, float scale = 1) | ||
: this(env, new[] { (inputColumn, outputColumn ?? inputColumn) }, subMean, useStdDev , scale) | ||
{ | ||
} | ||
|
||
/// <include file='doc.xml' path='doc/members/member[@name="GcNormalize"]/*'/> | ||
/// <param name="env">The environment.</param> | ||
/// <param name="columns">Pairs of columns to run the tokenization on.</param> | ||
/// <param name="subMean">Subtract mean from each value before normalizing.</param> | ||
/// <param name="useStdDev">Normalize by standard deviation rather than L2 norm.</param> | ||
/// <param name="scale">Scale features by this value.</param> | ||
public GcNormalizer(IHostEnvironment env, (string input, string output)[] columns, bool subMean = true, bool useStdDev = false, float scale = 1) | ||
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(GcNormalizer)), MakeTransformer(env, columns, subMean, useStdDev, scale)) | ||
{ | ||
} | ||
|
||
private static TransformWrapper MakeTransformer(IHostEnvironment env, (string input, string output)[] columns, bool subMean, bool useStdDev, float scale) | ||
{ | ||
Contracts.AssertValue(env); | ||
env.CheckNonEmpty(columns, nameof(columns)); | ||
foreach (var (input, output) in columns) | ||
{ | ||
env.CheckValue(input, nameof(input)); | ||
env.CheckValue(output, nameof(input)); | ||
} | ||
|
||
var args = new LpNormNormalizerTransform.GcnArguments | ||
{ | ||
Column = columns.Select(x => new LpNormNormalizerTransform.GcnColumn { Source = x.input, Name = x.output }).ToArray(), | ||
SubMean = subMean, | ||
UseStdDev = useStdDev, | ||
Scale = scale | ||
}; | ||
|
||
// Create a valid instance of data. | ||
var schema = new SimpleSchema(env, columns.Select(x => new KeyValuePair<string, ColumnType>(x.input, new VectorType(NumberType.R4))).ToArray()); | ||
var emptyData = new EmptyDataView(env, schema); | ||
|
||
return new TransformWrapper(env, new LpNormNormalizerTransform(env, args, emptyData)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Extensions for statically typed LpNormalizer estimator. | ||
/// </summary> | ||
public static class LpNormNormalizerExtensions | ||
{ | ||
private sealed class OutPipelineColumn : Vector<float> | ||
{ | ||
public readonly Vector<float> Input; | ||
|
||
public OutPipelineColumn(Vector<float> input, NormalizerKind normKind, bool subMean) | ||
: base(new Reconciler(normKind, subMean), input) | ||
{ | ||
Input = input; | ||
} | ||
} | ||
|
||
private sealed class Reconciler : EstimatorReconciler | ||
{ | ||
private readonly NormalizerKind _normKind; | ||
private readonly bool _subMean; | ||
|
||
public Reconciler(NormalizerKind normKind, bool subMean) | ||
{ | ||
_normKind = normKind; | ||
_subMean = subMean; | ||
} | ||
|
||
public override IEstimator<ITransformer> Reconcile(IHostEnvironment env, | ||
PipelineColumn[] toOutput, | ||
IReadOnlyDictionary<PipelineColumn, string> inputNames, | ||
IReadOnlyDictionary<PipelineColumn, string> outputNames, | ||
IReadOnlyCollection<string> usedNames) | ||
{ | ||
Contracts.Assert(toOutput.Length == 1); | ||
|
||
var pairs = new List<(string input, string output)>(); | ||
foreach (var outCol in toOutput) | ||
pairs.Add((inputNames[((OutPipelineColumn)outCol).Input], outputNames[outCol])); | ||
|
||
return new LpNormalizer(env, pairs.ToArray(), _normKind, _subMean); | ||
} | ||
} | ||
|
||
/// <include file='doc.xml' path='doc/members/member[@name="LpNormalize"]/*'/> | ||
/// <param name="input">The column to apply to.</param> | ||
/// <param name="normKind">Type of norm to use to normalize each sample.</param> | ||
/// <param name="subMean">Subtract mean from each value before normalizing.</param> | ||
public static Vector<float> LpNormalize(this Vector<float> input, NormalizerKind normKind = NormalizerKind.L2Norm, bool subMean = false) => new OutPipelineColumn(input, normKind, subMean); | ||
} | ||
|
||
/// <summary> | ||
/// Extensions for statically typed GcNormalizer estimator. | ||
/// </summary> | ||
public static class GcNormalizerExtensions | ||
{ | ||
private sealed class OutPipelineColumn : Vector<float> | ||
{ | ||
public readonly Vector<float> Input; | ||
|
||
public OutPipelineColumn(Vector<float> input, bool subMean, bool useStdDev, float scale) | ||
: base(new Reconciler(subMean, useStdDev, scale), input) | ||
{ | ||
Input = input; | ||
} | ||
} | ||
|
||
private sealed class Reconciler : EstimatorReconciler | ||
{ | ||
private readonly bool _subMean; | ||
private readonly bool _useStdDev; | ||
private readonly float _scale; | ||
|
||
public Reconciler(bool subMean, bool useStdDev, float scale) | ||
{ | ||
_subMean = subMean; | ||
_useStdDev = useStdDev; | ||
_scale = scale; | ||
} | ||
|
||
public override IEstimator<ITransformer> Reconcile(IHostEnvironment env, | ||
PipelineColumn[] toOutput, | ||
IReadOnlyDictionary<PipelineColumn, string> inputNames, | ||
IReadOnlyDictionary<PipelineColumn, string> outputNames, | ||
IReadOnlyCollection<string> usedNames) | ||
{ | ||
Contracts.Assert(toOutput.Length == 1); | ||
|
||
var pairs = new List<(string input, string output)>(); | ||
foreach (var outCol in toOutput) | ||
pairs.Add((inputNames[((OutPipelineColumn)outCol).Input], outputNames[outCol])); | ||
|
||
return new GcNormalizer(env, pairs.ToArray(), _subMean, _useStdDev, _scale); | ||
} | ||
} | ||
|
||
/// <include file='doc.xml' path='doc/members/member[@name="LpNormalize"]/*'/> | ||
/// <param name="input">The column to apply to.</param> | ||
/// <param name="subMean">Subtract mean from each value before normalizing.</param> | ||
/// <param name="useStdDev">Normalize by standard deviation rather than L2 norm.</param> | ||
/// <param name="scale">Scale features by this value.</param> | ||
public static Vector<float> GcNormalize(this Vector<float> input, | ||
bool subMean = true, | ||
bool useStdDev = false, | ||
float scale = 1) => new OutPipelineColumn(input, subMean, useStdDev, scale); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While 'Lp' is fine (it's a mathematical notation L_p), 'Gc' is probably not. 'GlobalContrast' ? Does it even make sense to have both as extensions though? They seem to share a lot in common #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they have different set of parameters and mixing them into one would not look good. I have renamed
GcNormalize
toGlobalContrastNormalize
.In reply to: 219260214 [](ancestors = 219260214)