Skip to content
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

Reformatting Test, Projection and TimeSeries of Transform to Width 85 #3947

Merged
merged 3 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ namespace Samples.Dynamic
public sealed class VectorWhiten
{

/// This example requires installation of additional nuget package <a href="https://www.nuget.org/packages/Microsoft.ML.Mkl.Components/">Microsoft.ML.Mkl.Components</a>.
/// This example requires installation of additional nuget package
/// <a href="https://www.nuget.org/packages/Microsoft.ML.Mkl.Components/">Microsoft.ML.Mkl.Components</a>.
public static void Example()
{
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
// as well as the source of randomness.
// Create a new ML context, for ML.NET operations. It can be used for
// exception tracking and logging, as well as the source of randomness.
var ml = new MLContext();

// Get a small dataset as an IEnumerable and convert it to an IDataView.
Expand All @@ -32,20 +33,29 @@ public static void Example()
// 6 7 8 9 0 1 2 3 4 5

// A small printing utility.
Action<string, IEnumerable<VBuffer<float>>> printHelper = (colName, column) =>
Action<string, IEnumerable<VBuffer<float>>> printHelper = (colName,
column) =>
{
Console.WriteLine($"{colName} column obtained post-transformation.");
Console.WriteLine($"{colName} column obtained " +
$"post-transformation.");

foreach (var row in column)
Console.WriteLine($"{string.Join(" ", row.DenseValues().Select(x => x.ToString("f3")))} ");
Console.WriteLine(string.Join(" ", row.DenseValues().Select(x =>
x.ToString("f3")))+" ");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctrl+k+d is your friend.

};

// A pipeline to project Features column into white noise vector.
var whiteningPipeline = ml.Transforms.VectorWhiten(nameof(SampleVectorOfNumbersData.Features),
kind: Microsoft.ML.Transforms.WhiteningKind.ZeroPhaseComponentAnalysis);
var whiteningPipeline = ml.Transforms.VectorWhiten(nameof(
SampleVectorOfNumbersData.Features), kind: Microsoft.ML.Transforms
.WhiteningKind.ZeroPhaseComponentAnalysis);

// The transformed (projected) data.
var transformedData = whiteningPipeline.Fit(trainData).Transform(trainData);
var transformedData = whiteningPipeline.Fit(trainData).Transform(
trainData);

// Getting the data of the newly created column, so we can preview it.
var whitening = transformedData.GetColumn<VBuffer<float>>(transformedData.Schema[nameof(SampleVectorOfNumbersData.Features)]);
var whitening = transformedData.GetColumn<VBuffer<float>>(
transformedData.Schema[nameof(SampleVectorOfNumbersData.Features)]);

printHelper(nameof(SampleVectorOfNumbersData.Features), whitening);

Expand All @@ -68,11 +78,16 @@ private class SampleVectorOfNumbersData
/// <summary>
/// Returns a few rows of the infertility dataset.
/// </summary>
private static IEnumerable<SampleVectorOfNumbersData> GetVectorOfNumbersData()
private static IEnumerable<SampleVectorOfNumbersData>
GetVectorOfNumbersData()
{
var data = new List<SampleVectorOfNumbersData>();
data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } });
data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } });
data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 0,
1, 2, 3, 4, 5, 6, 7, 8, 9 } });

data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 1,
2, 3, 4, 5, 6, 7, 8, 9, 0 } });

data.Add(new SampleVectorOfNumbersData
{
Features = new float[10] { 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ namespace Samples.Dynamic
{
public sealed class VectorWhitenWithOptions
{
/// This example requires installation of additional nuget package <a href="https://www.nuget.org/packages/Microsoft.ML.Mkl.Components/">Microsoft.ML.Mkl.Components</a>.
/// This example requires installation of additional nuget package
/// <a href="https://www.nuget.org/packages/Microsoft.ML.Mkl.Components/">Microsoft.ML.Mkl.Components</a>.
public static void Example()
{
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
// as well as the source of randomness.
// Create a new ML context, for ML.NET operations. It can be used for
// exception tracking and logging, as well as the source of randomness.
var ml = new MLContext();

// Get a small dataset as an IEnumerable and convert it to an IDataView.
Expand All @@ -31,20 +32,30 @@ public static void Example()
// 6 7 8 9 0 1 2 3 4 5

// A small printing utility.
Action<string, IEnumerable<VBuffer<float>>> printHelper = (colName, column) =>
Action<string, IEnumerable<VBuffer<float>>> printHelper = (colName,
column) =>
{
Console.WriteLine($"{colName} column obtained post-transformation.");
Console.WriteLine($"{colName} column obtained" +
$"post-transformation.");

foreach (var row in column)
Console.WriteLine($"{string.Join(" ", row.DenseValues().Select(x => x.ToString("f3")))} ");
Console.WriteLine(string.Join(" ", row.DenseValues().Select(x =>
x.ToString("f3")))+" ");
};


// A pipeline to project Features column into white noise vector.
var whiteningPipeline = ml.Transforms.VectorWhiten(nameof(SampleVectorOfNumbersData.Features), kind: Microsoft.ML.Transforms.WhiteningKind.PrincipalComponentAnalysis, rank: 4);
var whiteningPipeline = ml.Transforms.VectorWhiten(nameof(
SampleVectorOfNumbersData.Features), kind: Microsoft.ML.Transforms
.WhiteningKind.PrincipalComponentAnalysis, rank: 4);

// The transformed (projected) data.
var transformedData = whiteningPipeline.Fit(trainData).Transform(trainData);
var transformedData = whiteningPipeline.Fit(trainData).Transform(
trainData);

// Getting the data of the newly created column, so we can preview it.
var whitening = transformedData.GetColumn<VBuffer<float>>(transformedData.Schema[nameof(SampleVectorOfNumbersData.Features)]);
var whitening = transformedData.GetColumn<VBuffer<float>>(
transformedData.Schema[nameof(SampleVectorOfNumbersData.Features)]);

printHelper(nameof(SampleVectorOfNumbersData.Features), whitening);

Expand All @@ -66,11 +77,16 @@ private class SampleVectorOfNumbersData
/// <summary>
/// Returns a few rows of the infertility dataset.
/// </summary>
private static IEnumerable<SampleVectorOfNumbersData> GetVectorOfNumbersData()
private static IEnumerable<SampleVectorOfNumbersData>
GetVectorOfNumbersData()
{
var data = new List<SampleVectorOfNumbersData>();
data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } });
data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } });
data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 0,
1, 2, 3, 4, 5, 6, 7, 8, 9 } });

data.Add(new SampleVectorOfNumbersData { Features = new float[10] { 1,
2, 3, 4, 5, 6, 7, 8, 9, 0 } });

data.Add(new SampleVectorOfNumbersData
{
Features = new float[10] { 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ public static class ApplyCustomWordEmbedding
{
public static void Example()
{
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
// as well as the source of randomness.
// Create a new ML context, for ML.NET operations. It can be used for
// exception tracking and logging, as well as the source of randomness.
var mlContext = new MLContext();

// Create an empty list as the dataset. The 'ApplyWordEmbedding' does not require training data as
// the estimator ('WordEmbeddingEstimator') created by 'ApplyWordEmbedding' API is not a trainable estimator.
// Create an empty list as the dataset. The 'ApplyWordEmbedding' does
// not require training data as the estimator ('WordEmbeddingEstimator')
// created by 'ApplyWordEmbedding' API is not a trainable estimator.
// The empty list is only needed to pass input schema to the pipeline.
var emptySamples = new List<TextData>();

Expand All @@ -33,25 +34,33 @@ public static void Example()
file.WriteLine("buy 0 0 20");
}

// A pipeline for converting text into a 9-dimension word embedding vector using the custom word embedding model.
// The 'ApplyWordEmbedding' computes the minimum, average and maximum values for each token's embedding vector.
// Tokens in 'custommodel.txt' model are represented as 3-dimension vector.
// Therefore, the output is of 9-dimension [min, avg, max].
// A pipeline for converting text into a 9-dimension word embedding
// vector using the custom word embedding model. The
// 'ApplyWordEmbedding' computes the minimum, average and maximum values
// for each token's embedding vector. Tokens in 'custommodel.txt' model
// are represented as 3-dimension vector. Therefore, the output is of
// 9 -dimension [min, avg, max].
//
// The 'ApplyWordEmbedding' API requires vector of text as input.
// The pipeline first normalizes and tokenizes text then applies word embedding transformation.
// The pipeline first normalizes and tokenizes text then applies word
// embedding transformation.
var textPipeline = mlContext.Transforms.Text.NormalizeText("Text")
.Append(mlContext.Transforms.Text.TokenizeIntoWords("Tokens", "Text"))
.Append(mlContext.Transforms.Text.ApplyWordEmbedding("Features", pathToCustomModel, "Tokens"));
.Append(mlContext.Transforms.Text.TokenizeIntoWords("Tokens",
"Text"))
.Append(mlContext.Transforms.Text.ApplyWordEmbedding("Features",
pathToCustomModel, "Tokens"));

// Fit to data.
var textTransformer = textPipeline.Fit(emptyDataView);

// Create the prediction engine to get the embedding vector from the input text/string.
var predictionEngine = mlContext.Model.CreatePredictionEngine<TextData, TransformedTextData>(textTransformer);
// Create the prediction engine to get the embedding vector from the
// input text/string.
var predictionEngine = mlContext.Model.CreatePredictionEngine<TextData,
TransformedTextData>(textTransformer);

// Call the prediction API to convert the text into embedding vector.
var data = new TextData() { Text = "This is a great product. I would like to buy it again." };
var data = new TextData() { Text = "This is a great product. I would " +
"like to buy it again." };
var prediction = predictionEngine.Predict(data);

// Print the length of the embedding vector.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,48 @@ public static class ApplyWordEmbedding
{
public static void Example()
{
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
// as well as the source of randomness.
// Create a new ML context, for ML.NET operations. It can be used for
// exception tracking and logging, as well as the source of randomness.
var mlContext = new MLContext();

// Create an empty list as the dataset. The 'ApplyWordEmbedding' does not require training data as
// the estimator ('WordEmbeddingEstimator') created by 'ApplyWordEmbedding' API is not a trainable estimator.
// Create an empty list as the dataset. The 'ApplyWordEmbedding' does
// not require training data as the estimator ('WordEmbeddingEstimator')
// created by 'ApplyWordEmbedding' API is not a trainable estimator.
// The empty list is only needed to pass input schema to the pipeline.
var emptySamples = new List<TextData>();

// Convert sample list to an empty IDataView.
var emptyDataView = mlContext.Data.LoadFromEnumerable(emptySamples);

// A pipeline for converting text into a 150-dimension embedding vector using pretrained 'SentimentSpecificWordEmbedding' model.
// The 'ApplyWordEmbedding' computes the minimum, average and maximum values for each token's embedding vector.
// Tokens in 'SentimentSpecificWordEmbedding' model are represented as 50-dimension vector.
// Therefore, the output is of 150-dimension [min, avg, max].
// A pipeline for converting text into a 150-dimension embedding vector
// using pretrained 'SentimentSpecificWordEmbedding' model. The
// 'ApplyWordEmbedding' computes the minimum, average and maximum values
// for each token's embedding vector. Tokens in
// 'SentimentSpecificWordEmbedding' model are represented as
// 50 -dimension vector. Therefore, the output is of 150-dimension [min,
// avg, max].
//
// The 'ApplyWordEmbedding' API requires vector of text as input.
// The pipeline first normalizes and tokenizes text then applies word embedding transformation.
// The pipeline first normalizes and tokenizes text then applies word
// embedding transformation.
var textPipeline = mlContext.Transforms.Text.NormalizeText("Text")
.Append(mlContext.Transforms.Text.TokenizeIntoWords("Tokens", "Text"))
.Append(mlContext.Transforms.Text.ApplyWordEmbedding("Features", "Tokens",
WordEmbeddingEstimator.PretrainedModelKind.SentimentSpecificWordEmbedding));
.Append(mlContext.Transforms.Text.TokenizeIntoWords("Tokens",
"Text"))
.Append(mlContext.Transforms.Text.ApplyWordEmbedding("Features",
"Tokens", WordEmbeddingEstimator.PretrainedModelKind
.SentimentSpecificWordEmbedding));

// Fit to data.
var textTransformer = textPipeline.Fit(emptyDataView);

// Create the prediction engine to get the embedding vector from the input text/string.
var predictionEngine = mlContext.Model.CreatePredictionEngine<TextData, TransformedTextData>(textTransformer);
// Create the prediction engine to get the embedding vector from the
// input text/string.
var predictionEngine = mlContext.Model.CreatePredictionEngine<TextData,
TransformedTextData>(textTransformer);

// Call the prediction API to convert the text into embedding vector.
var data = new TextData() { Text = "This is a great product. I would like to buy it again." };
var data = new TextData() { Text = "This is a great product. I would " +
"like to buy it again." };
var prediction = predictionEngine.Predict(data);

// Print the length of the embedding vector.
Expand Down
Loading