Skip to content

Commit 95dc1fc

Browse files
authored
added flag to disable training code (dotnet#227)
1 parent dea2fe2 commit 95dc1fc

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

src/mlnet.Test/ApprovalTests/ConsoleCodeGeneratorTests.GeneratedTrainCodeTest.approved.txt

+16-6
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,27 @@ namespace MyNamespace
2020
private static string TestDataPath = @"x:\dummypath\dummy_test.csv";
2121
private static string ModelPath = @"./model.zip";
2222

23+
// Set this flag to enable the training process.
24+
private static bool EnableTraining = false;
25+
2326
static void Main(string[] args)
2427
{
25-
//Create MLContext to be shared across the model creation workflow objects
26-
//Set a random seed for repeatable/deterministic results across multiple trainings.
28+
// Create MLContext to be shared across the model creation workflow objects
29+
// Set a random seed for repeatable/deterministic results across multiple trainings.
2730
var mlContext = new MLContext(seed: 1);
2831

29-
// Create, Train, Evaluate and Save a model
30-
BuildTrainEvaluateAndSaveModel(mlContext);
31-
ConsoleHelper.ConsoleWriteHeader("=============== End of training process ===============");
32+
if (EnableTraining)
33+
{
34+
// Create, Train, Evaluate and Save a model
35+
BuildTrainEvaluateAndSaveModel(mlContext);
36+
ConsoleHelper.ConsoleWriteHeader("=============== End of training process ===============");
37+
}
38+
else
39+
{
40+
ConsoleHelper.ConsoleWriteHeader("Skipping the training process. Please set the flag : 'EnableTraining' to 'true' to enable the training process.");
41+
}
3242

33-
// Make a single test prediction loding the model from .ZIP file
43+
// Make a single test prediction loading the model from .ZIP file
3444
TestSinglePrediction(mlContext);
3545

3646
ConsoleHelper.ConsoleWriteHeader("=============== End of process, hit any key to finish ===============");

src/mlnet/Templates/Console/MLCodeGen.cs

+21-26
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,27 @@ public virtual string TransformText()
5050
this.Write(this.ToStringHelper.ToStringWithCulture(TestPath));
5151
this.Write("\";\r\n");
5252
}
53-
this.Write(@" private static string ModelPath = @""./model.zip"";
54-
55-
static void Main(string[] args)
56-
{
57-
//Create MLContext to be shared across the model creation workflow objects
58-
//Set a random seed for repeatable/deterministic results across multiple trainings.
59-
var mlContext = new MLContext(seed: 1);
60-
61-
// Create, Train, Evaluate and Save a model
62-
BuildTrainEvaluateAndSaveModel(mlContext);
63-
ConsoleHelper.ConsoleWriteHeader(""=============== End of training process ==============="");
64-
65-
// Make a single test prediction loding the model from .ZIP file
66-
TestSinglePrediction(mlContext);
67-
68-
ConsoleHelper.ConsoleWriteHeader(""=============== End of process, hit any key to finish ==============="");
69-
Console.ReadKey();
70-
71-
}
72-
73-
private static ITransformer BuildTrainEvaluateAndSaveModel(MLContext mlContext)
74-
{
75-
// Data loading
76-
IDataView trainingDataView = mlContext.Data.ReadFromTextFile<SampleObservation>(
77-
path: TrainDataPath,
78-
hasHeader : ");
53+
this.Write(" private static string ModelPath = @\"./model.zip\";\r\n\r\n // Set this " +
54+
"flag to enable the training process.\r\n private static bool EnableTraining" +
55+
" = false;\r\n\r\n static void Main(string[] args)\r\n {\r\n // " +
56+
"Create MLContext to be shared across the model creation workflow objects \r\n " +
57+
" // Set a random seed for repeatable/deterministic results across multiple" +
58+
" trainings.\r\n var mlContext = new MLContext(seed: 1);\r\n\r\n " +
59+
"if (EnableTraining)\r\n {\r\n // Create, Train, Evaluate a" +
60+
"nd Save a model\r\n BuildTrainEvaluateAndSaveModel(mlContext);\r\n " +
61+
" ConsoleHelper.ConsoleWriteHeader(\"=============== End of training p" +
62+
"rocess ===============\");\r\n }\r\n else\r\n {\r\n " +
63+
" ConsoleHelper.ConsoleWriteHeader(\"Skipping the training process. Plea" +
64+
"se set the flag : \'EnableTraining\' to \'true\' to enable the training process.\");\r" +
65+
"\n }\r\n\r\n // Make a single test prediction loading the model" +
66+
" from .ZIP file\r\n TestSinglePrediction(mlContext);\r\n\r\n Con" +
67+
"soleHelper.ConsoleWriteHeader(\"=============== End of process, hit any key to fi" +
68+
"nish ===============\");\r\n Console.ReadKey();\r\n\r\n }\r\n\r\n " +
69+
"private static ITransformer BuildTrainEvaluateAndSaveModel(MLContext mlContext)\r" +
70+
"\n {\r\n // Data loading\r\n IDataView trainingDataView " +
71+
"= mlContext.Data.ReadFromTextFile<SampleObservation>(\r\n " +
72+
" path: TrainDataPath,\r\n " +
73+
" hasHeader : ");
7974
this.Write(this.ToStringHelper.ToStringWithCulture(HasHeader.ToString().ToLowerInvariant()));
8075
this.Write(",\r\n separatorChar : \'");
8176
this.Write(this.ToStringHelper.ToStringWithCulture(Regex.Escape(Separator.ToString())));

src/mlnet/Templates/Console/MLCodeGen.tt

+16-6
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,27 @@ namespace <#= Namespace #>
2828
<# } #>
2929
private static string ModelPath = @"./model.zip";
3030

31+
// Set this flag to enable the training process.
32+
private static bool EnableTraining = false;
33+
3134
static void Main(string[] args)
3235
{
33-
//Create MLContext to be shared across the model creation workflow objects
34-
//Set a random seed for repeatable/deterministic results across multiple trainings.
36+
// Create MLContext to be shared across the model creation workflow objects
37+
// Set a random seed for repeatable/deterministic results across multiple trainings.
3538
var mlContext = new MLContext(seed: 1);
3639

37-
// Create, Train, Evaluate and Save a model
38-
BuildTrainEvaluateAndSaveModel(mlContext);
39-
ConsoleHelper.ConsoleWriteHeader("=============== End of training process ===============");
40+
if (EnableTraining)
41+
{
42+
// Create, Train, Evaluate and Save a model
43+
BuildTrainEvaluateAndSaveModel(mlContext);
44+
ConsoleHelper.ConsoleWriteHeader("=============== End of training process ===============");
45+
}
46+
else
47+
{
48+
ConsoleHelper.ConsoleWriteHeader("Skipping the training process. Please set the flag : 'EnableTraining' to 'true' to enable the training process.");
49+
}
4050

41-
// Make a single test prediction loding the model from .ZIP file
51+
// Make a single test prediction loading the model from .ZIP file
4252
TestSinglePrediction(mlContext);
4353

4454
ConsoleHelper.ConsoleWriteHeader("=============== End of process, hit any key to finish ===============");

0 commit comments

Comments
 (0)