Skip to content

Commit 25cc50d

Browse files
authored
Generated Project new structure. (dotnet#305)
* added new templates * writing files to disck * change path * added new templates * misisng braces * fix bugs * format code * added util methods for solution file creation and addition of projects to it * added extra packages to project files * new tests * added correct path for sln * build fix * fix build
1 parent 14091e4 commit 25cc50d

31 files changed

+3249
-705
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//*****************************************************************************************
2+
//* *
3+
//* This is an auto-generated file by Microsoft ML.NET CLI (Command-Line Interface) tool. *
4+
//* *
5+
//*****************************************************************************************
6+
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using Microsoft.ML;
11+
using Microsoft.ML.Data;
12+
13+
namespace TestNamespace.Train
14+
{
15+
public static class ConsoleHelper
16+
{
17+
18+
public static void PrintRegressionMetrics(RegressionMetrics metrics)
19+
{
20+
Console.WriteLine($"*************************************************");
21+
Console.WriteLine($"* Metrics for regression model ");
22+
Console.WriteLine($"*------------------------------------------------");
23+
Console.WriteLine($"* LossFn: {metrics.LossFn:0.##}");
24+
Console.WriteLine($"* R2 Score: {metrics.RSquared:0.##}");
25+
Console.WriteLine($"* Absolute loss: {metrics.L1:#.##}");
26+
Console.WriteLine($"* Squared loss: {metrics.L2:#.##}");
27+
Console.WriteLine($"* RMS loss: {metrics.Rms:#.##}");
28+
Console.WriteLine($"*************************************************");
29+
}
30+
31+
public static void PrintRegressionFoldsAverageMetrics(TrainCatalogBase.CrossValidationResult<RegressionMetrics>[] crossValidationResults)
32+
{
33+
var L1 = crossValidationResults.Select(r => r.Metrics.L1);
34+
var L2 = crossValidationResults.Select(r => r.Metrics.L2);
35+
var RMS = crossValidationResults.Select(r => r.Metrics.L1);
36+
var lossFunction = crossValidationResults.Select(r => r.Metrics.LossFn);
37+
var R2 = crossValidationResults.Select(r => r.Metrics.RSquared);
38+
39+
Console.WriteLine($"*************************************************************************************************************");
40+
Console.WriteLine($"* Metrics for Regression model ");
41+
Console.WriteLine($"*------------------------------------------------------------------------------------------------------------");
42+
Console.WriteLine($"* Average L1 Loss: {L1.Average():0.###} ");
43+
Console.WriteLine($"* Average L2 Loss: {L2.Average():0.###} ");
44+
Console.WriteLine($"* Average RMS: {RMS.Average():0.###} ");
45+
Console.WriteLine($"* Average Loss Function: {lossFunction.Average():0.###} ");
46+
Console.WriteLine($"* Average R-squared: {R2.Average():0.###} ");
47+
Console.WriteLine($"*************************************************************************************************************");
48+
}
49+
50+
public static void PrintBinaryClassificationMetrics(BinaryClassificationMetrics metrics)
51+
{
52+
Console.WriteLine($"************************************************************");
53+
Console.WriteLine($"* Metrics for binary classification model ");
54+
Console.WriteLine($"*-----------------------------------------------------------");
55+
Console.WriteLine($"* Accuracy: {metrics.Accuracy:P2}");
56+
Console.WriteLine($"* Auc: {metrics.Auc:P2}");
57+
Console.WriteLine($"************************************************************");
58+
}
59+
60+
61+
public static void PrintBinaryClassificationFoldsAverageMetrics(
62+
TrainCatalogBase.CrossValidationResult<BinaryClassificationMetrics>[] crossValResults)
63+
{
64+
var metricsInMultipleFolds = crossValResults.Select(r => r.Metrics);
65+
66+
var AccuracyValues = metricsInMultipleFolds.Select(m => m.Accuracy);
67+
var AccuracyAverage = AccuracyValues.Average();
68+
var AccuraciesStdDeviation = CalculateStandardDeviation(AccuracyValues);
69+
var AccuraciesConfidenceInterval95 = CalculateConfidenceInterval95(AccuracyValues);
70+
71+
72+
Console.WriteLine($"*************************************************************************************************************");
73+
Console.WriteLine($"* Metrics for Binary Classification model ");
74+
Console.WriteLine($"*------------------------------------------------------------------------------------------------------------");
75+
Console.WriteLine($"* Average Accuracy: {AccuracyAverage:0.###} - Standard deviation: ({AccuraciesStdDeviation:#.###}) - Confidence Interval 95%: ({AccuraciesConfidenceInterval95:#.###})");
76+
Console.WriteLine($"*************************************************************************************************************");
77+
78+
}
79+
80+
public static void PrintMulticlassClassificationFoldsAverageMetrics(
81+
TrainCatalogBase.CrossValidationResult<MultiClassClassifierMetrics>[] crossValResults)
82+
{
83+
var metricsInMultipleFolds = crossValResults.Select(r => r.Metrics);
84+
85+
var microAccuracyValues = metricsInMultipleFolds.Select(m => m.AccuracyMicro);
86+
var microAccuracyAverage = microAccuracyValues.Average();
87+
var microAccuraciesStdDeviation = CalculateStandardDeviation(microAccuracyValues);
88+
var microAccuraciesConfidenceInterval95 = CalculateConfidenceInterval95(microAccuracyValues);
89+
90+
var macroAccuracyValues = metricsInMultipleFolds.Select(m => m.AccuracyMacro);
91+
var macroAccuracyAverage = macroAccuracyValues.Average();
92+
var macroAccuraciesStdDeviation = CalculateStandardDeviation(macroAccuracyValues);
93+
var macroAccuraciesConfidenceInterval95 = CalculateConfidenceInterval95(macroAccuracyValues);
94+
95+
var logLossValues = metricsInMultipleFolds.Select(m => m.LogLoss);
96+
var logLossAverage = logLossValues.Average();
97+
var logLossStdDeviation = CalculateStandardDeviation(logLossValues);
98+
var logLossConfidenceInterval95 = CalculateConfidenceInterval95(logLossValues);
99+
100+
var logLossReductionValues = metricsInMultipleFolds.Select(m => m.LogLossReduction);
101+
var logLossReductionAverage = logLossReductionValues.Average();
102+
var logLossReductionStdDeviation = CalculateStandardDeviation(logLossReductionValues);
103+
var logLossReductionConfidenceInterval95 = CalculateConfidenceInterval95(logLossReductionValues);
104+
105+
Console.WriteLine($"*************************************************************************************************************");
106+
Console.WriteLine($"* Metrics for Multi-class Classification model ");
107+
Console.WriteLine($"*------------------------------------------------------------------------------------------------------------");
108+
Console.WriteLine($"* Average MicroAccuracy: {microAccuracyAverage:0.###} - Standard deviation: ({microAccuraciesStdDeviation:#.###}) - Confidence Interval 95%: ({microAccuraciesConfidenceInterval95:#.###})");
109+
Console.WriteLine($"* Average MacroAccuracy: {macroAccuracyAverage:0.###} - Standard deviation: ({macroAccuraciesStdDeviation:#.###}) - Confidence Interval 95%: ({macroAccuraciesConfidenceInterval95:#.###})");
110+
Console.WriteLine($"* Average LogLoss: {logLossAverage:#.###} - Standard deviation: ({logLossStdDeviation:#.###}) - Confidence Interval 95%: ({logLossConfidenceInterval95:#.###})");
111+
Console.WriteLine($"* Average LogLossReduction: {logLossReductionAverage:#.###} - Standard deviation: ({logLossReductionStdDeviation:#.###}) - Confidence Interval 95%: ({logLossReductionConfidenceInterval95:#.###})");
112+
Console.WriteLine($"*************************************************************************************************************");
113+
114+
}
115+
116+
public static double CalculateStandardDeviation(IEnumerable<double> values)
117+
{
118+
double average = values.Average();
119+
double sumOfSquaresOfDifferences = values.Select(val => (val - average) * (val - average)).Sum();
120+
double standardDeviation = Math.Sqrt(sumOfSquaresOfDifferences / (values.Count() - 1));
121+
return standardDeviation;
122+
}
123+
124+
public static double CalculateConfidenceInterval95(IEnumerable<double> values)
125+
{
126+
double confidenceInterval95 = 1.96 * CalculateStandardDeviation(values) / Math.Sqrt((values.Count() - 1));
127+
return confidenceInterval95;
128+
}
129+
}
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
</PropertyGroup>
6+
<PropertyGroup>
7+
<RestoreSources>
8+
https://api.nuget.org/v3/index.json;
9+
</RestoreSources>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.ML" Version="0.11.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="MLModel.zip">
17+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//*****************************************************************************************
2+
//* *
3+
//* This is an auto-generated file by Microsoft ML.NET CLI (Command-Line Interface) tool. *
4+
//* *
5+
//*****************************************************************************************
6+
7+
using Microsoft.ML.Data;
8+
9+
namespace TestNamespace.Model.DataModels
10+
{
11+
public class SampleObservation
12+
{
13+
[ColumnName("Label"), LoadColumn(0)]
14+
public bool Label { get; set; }
15+
16+
17+
[ColumnName("col1"), LoadColumn(1)]
18+
public float Col1 { get; set; }
19+
20+
21+
[ColumnName("col2"), LoadColumn(0)]
22+
public float Col2 { get; set; }
23+
24+
25+
[ColumnName("col3"), LoadColumn(0)]
26+
public string Col3 { get; set; }
27+
28+
29+
[ColumnName("col4"), LoadColumn(0)]
30+
public int Col4 { get; set; }
31+
32+
33+
[ColumnName("col5"), LoadColumn(0)]
34+
public uint Col5 { get; set; }
35+
36+
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//*****************************************************************************************
2+
//* *
3+
//* This is an auto-generated file by Microsoft ML.NET CLI (Command-Line Interface) tool. *
4+
//* *
5+
//*****************************************************************************************
6+
7+
using System;
8+
using System.IO;
9+
using System.Linq;
10+
using System.Collections.Generic;
11+
using Microsoft.ML;
12+
using Microsoft.ML.Data;
13+
using Microsoft.Data.DataView;
14+
using TestNamespace.Model.DataModels;
15+
16+
17+
namespace TestNamespace.Predict
18+
{
19+
class Program
20+
{
21+
//Machine Learning model to load and use for predictions
22+
private const string MODEL_FILEPATH = @"MLModel.zip";
23+
24+
//Dataset to use for predictions
25+
private const string DATA_FILEPATH = @"x:\dummypath\dummy_test.csv";
26+
27+
static void Main(string[] args)
28+
{
29+
MLContext mlContext = new MLContext();
30+
31+
//Load ML Model from .zip file
32+
ITransformer mlModel = LoadModelFromFile(mlContext, MODEL_FILEPATH);
33+
34+
// Create sample data to do a single prediction with it
35+
SampleObservation sampleData = CreateSingleDataSample(mlContext, DATA_FILEPATH);
36+
37+
// Test a single prediction
38+
Predict(mlContext, mlModel, sampleData);
39+
40+
Console.WriteLine("=============== End of process, hit any key to finish ===============");
41+
Console.ReadKey();
42+
}
43+
44+
private static void Predict(MLContext mlContext, ITransformer mlModel, SampleObservation sampleData)
45+
{
46+
// Create prediction engine related to the loaded ML model
47+
var predEngine = mlModel.CreatePredictionEngine<SampleObservation, SamplePrediction>(mlContext);
48+
49+
// Try a single prediction
50+
var predictionResult = predEngine.Predict(sampleData);
51+
Console.WriteLine($"Single Prediction --> Actual value: {sampleData.Label} | Predicted value: {predictionResult.Prediction}");
52+
}
53+
54+
private static ITransformer LoadModelFromFile(MLContext mlContext, string modelFilePath)
55+
{
56+
ITransformer mlModel;
57+
using (var stream = new FileStream(modelFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
58+
{
59+
mlModel = mlContext.Model.Load(stream);
60+
}
61+
62+
return mlModel;
63+
}
64+
65+
// Method to load single row of data to try a single prediction
66+
// You can change this code and create your own sample data here (Hardcoded or from any source)
67+
private static SampleObservation CreateSingleDataSample(MLContext mlContext, string dataFilePath)
68+
{
69+
// Read dataset to get a single row for trying a prediction
70+
IDataView dataView = mlContext.Data.LoadFromTextFile<SampleObservation>(
71+
path: dataFilePath,
72+
hasHeader: true,
73+
separatorChar: ',');
74+
75+
// Here (SampleObservation object) you could provide new test data, hardcoded or from the end-user application, instead of the row from the file.
76+
SampleObservation sampleForPrediction = mlContext.Data.CreateEnumerable<SampleObservation>(dataView, false)
77+
.First();
78+
return sampleForPrediction;
79+
}
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.ML" Version="0.11.0" />
9+
<PackageReference Include="Microsoft.ML.LightGBM" Version="0.11.0" />
10+
<PackageReference Include="Microsoft.ML.HalLearners" Version="0.11.0" />
11+
</ItemGroup>
12+
<ItemGroup>
13+
<ProjectReference Include="..\TestNamespace.Model\TestNamespace.Model.csproj" />
14+
</ItemGroup>
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//*****************************************************************************************
2+
//* *
3+
//* This is an auto-generated file by Microsoft ML.NET CLI (Command-Line Interface) tool. *
4+
//* *
5+
//*****************************************************************************************
6+
7+
using Microsoft.ML.Data;
8+
9+
namespace TestNamespace.Model.DataModels
10+
{
11+
public class SamplePrediction
12+
{
13+
// ColumnName attribute is used to change the column name from
14+
// its default value, which is the name of the field.
15+
[ColumnName("PredictedLabel")]
16+
public bool Prediction { get; set; }
17+
18+
public float Score { get; set; }
19+
}
20+
}

0 commit comments

Comments
 (0)