-
Notifications
You must be signed in to change notification settings - Fork 1.9k
XML documentation references cs code for examples #1105
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 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4ab6eee
Adding the Samples.StaticPipe project.
sfilipi 6d271ea
Adding a sample for SDCA Regression
sfilipi 2236dbe
Splitting the dataset creation to another file,
sfilipi b5f9592
Merge branch 'master' into docsSamples
sfilipi c758415
Renaming the file
sfilipi af43f81
addressing most of eerhardt's comments
sfilipi 1e6b160
Eric's comments: referencing the example only from one class, since i…
sfilipi 24917ae
merge from master + renaming the folder.
sfilipi 512af14
removing the dead project. Adding back timeseries test.
sfilipi 92343cc
switching to the LocalEnvironemnt per Eric's suggestion.
sfilipi 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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 System.Text; | ||
|
||
namespace Microsoft.ML.Samples.StaticPipe | ||
{ | ||
public static class DatasetCreator | ||
{ | ||
public static (string trainPath, string testPath) CreateRegressionDataset() | ||
{ | ||
// creating a small sample dataset, and writting it to file | ||
string trainDataPath = @"RegressionTrainDataset.txt"; | ||
string testDataPath = @"RegressionTestDataset.txt"; | ||
|
||
string header = "feature_a, feature_b, target"; | ||
|
||
int a = 0; | ||
int b = 0; | ||
int target = 0; | ||
|
||
var csvTrain = new StringBuilder().AppendLine(header); | ||
var csvTest = new StringBuilder().AppendLine(header); | ||
|
||
Random rnd = new Random(); | ||
for (int i = 0; i < 1000; i++) | ||
{ | ||
a = rnd.Next(i - 5, i + 5); | ||
b = rnd.Next(0, 10); | ||
|
||
target = 2*a + b; | ||
|
||
if (i % 15 == 0) | ||
csvTest.AppendLine($"{a}, {b}, {target}"); | ||
else | ||
csvTrain.AppendLine($"{a}, {b} , {target}"); | ||
} | ||
|
||
|
||
if (!File.Exists(trainDataPath)) | ||
File.WriteAllText(trainDataPath, csvTrain.ToString()); | ||
else | ||
{ | ||
new Exception("Train dataset file already exists"); | ||
} | ||
|
||
if (!File.Exists(testDataPath)) | ||
File.WriteAllText(testDataPath, csvTest.ToString()); | ||
else | ||
{ | ||
new Exception("Test dataset file already exists"); | ||
} | ||
|
||
return (trainDataPath, testDataPath); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
docs/samples/Microsoft.ML.Samples/Microsoft.ML.Samples.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Microsoft.ML.StandardLearners\Microsoft.ML.StandardLearners.csproj" /> | ||
|
||
<NativeAssemblyReference Include="CpuMathNative" /> | ||
|
||
<ProjectReference Include="..\..\..\src\Microsoft.ML.Analyzer\Microsoft.ML.Analyzer.csproj"> | ||
<ReferenceOutputAssembly>false</ReferenceOutputAssembly> | ||
<OutputItemType>Analyzer</OutputItemType> | ||
</ProjectReference> | ||
|
||
</ItemGroup> | ||
|
||
</Project> |
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,16 @@ | ||
// 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.Samples.StaticPipe; | ||
|
||
namespace Microsoft.ML.Samples | ||
{ | ||
internal static class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Trainers.SdcaRegression(); | ||
} | ||
} | ||
} |
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,72 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.ML.Runtime.Data; | ||
using Microsoft.ML.Runtime.Learners; | ||
using Microsoft.ML.StaticPipe; | ||
using System; | ||
|
||
namespace Microsoft.ML.Samples.StaticPipe | ||
{ | ||
public static class Trainers | ||
{ | ||
public static void SdcaRegression() | ||
{ | ||
var (trainDataPath, testDataPath) = DatasetCreator.CreateRegressionDataset(); | ||
|
||
//creating the ML.Net IHostEnvironment object, needed for the pipeline | ||
var env = new ConsoleEnvironment(seed: 0); | ||
|
||
// creating the ML context, based on the task performed. | ||
var regressionContext = new RegressionContext(env); | ||
|
||
// Creating a data reader, based on the format of the data | ||
var reader = TextLoader.CreateReader(env, c => ( | ||
label: c.LoadFloat(2), | ||
features: c.LoadFloat(0, 1) | ||
), | ||
separator: ',', hasHeader: true); | ||
|
||
// Read the data | ||
var trainData = reader.Read(new MultiFileSource(trainDataPath)); | ||
|
||
// The predictor that gets produced out of training | ||
LinearRegressionPredictor pred = null; | ||
|
||
// Create the estimator | ||
var learningPipeline = reader.MakeNewEstimator() | ||
.Append(r => (r.label, score: regressionContext.Trainers.Sdca( | ||
r.label, | ||
r.features, | ||
l1Threshold: 0f, | ||
maxIterations: 100, | ||
onFit: p => pred = p) | ||
) | ||
); | ||
|
||
// fit this pipeline to the training data | ||
var model = learningPipeline.Fit(trainData); | ||
|
||
// check the weights that the model learned | ||
VBuffer<float> weights = default; | ||
pred.GetFeatureWeights(ref weights); | ||
|
||
Console.WriteLine($"weight 0 - {weights.Values[0]}"); | ||
Console.WriteLine($"weight 1 - {weights.Values[1]}"); | ||
|
||
// test the model we just trained, using the test file. | ||
var testData = reader.Read(new MultiFileSource(testDataPath)); | ||
var data = model.Transform(testData); | ||
|
||
//Evaluate how the model is doing on the test data | ||
var metrics = regressionContext.Evaluate(data, r => r.label, r => r.score); | ||
|
||
Console.WriteLine($"L1 - {metrics.L1}"); | ||
Console.WriteLine($"L2 - {metrics.L2}"); | ||
Console.WriteLine($"LossFunction - {metrics.LossFn}"); | ||
Console.WriteLine($"RMS - {metrics.Rms}"); | ||
Console.WriteLine($"RSquared - {metrics.RSquared}"); | ||
} | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
fix #Resolved