Skip to content

Commit

Permalink
added flag to disable training code (dotnet#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
srsaggam authored and Dmitry-A committed Aug 22, 2019
1 parent ccd369d commit cc7894f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,27 @@ namespace MyNamespace
private static string TestDataPath = @"x:\dummypath\dummy_test.csv";
private static string ModelPath = @"./model.zip";

// Set this flag to enable the training process.
private static bool EnableTraining = false;

static void Main(string[] args)
{
//Create MLContext to be shared across the model creation workflow objects
//Set a random seed for repeatable/deterministic results across multiple trainings.
// Create MLContext to be shared across the model creation workflow objects
// Set a random seed for repeatable/deterministic results across multiple trainings.
var mlContext = new MLContext(seed: 1);

// Create, Train, Evaluate and Save a model
BuildTrainEvaluateAndSaveModel(mlContext);
ConsoleHelper.ConsoleWriteHeader("=============== End of training process ===============");
if (EnableTraining)
{
// Create, Train, Evaluate and Save a model
BuildTrainEvaluateAndSaveModel(mlContext);
ConsoleHelper.ConsoleWriteHeader("=============== End of training process ===============");
}
else
{
ConsoleHelper.ConsoleWriteHeader("Skipping the training process. Please set the flag : 'EnableTraining' to 'true' to enable the training process.");
}

// Make a single test prediction loding the model from .ZIP file
// Make a single test prediction loading the model from .ZIP file
TestSinglePrediction(mlContext);

ConsoleHelper.ConsoleWriteHeader("=============== End of process, hit any key to finish ===============");
Expand Down
47 changes: 21 additions & 26 deletions src/mlnet/Templates/Console/MLCodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,27 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(TestPath));
this.Write("\";\r\n");
}
this.Write(@" private static string ModelPath = @""./model.zip"";
static void Main(string[] args)
{
//Create MLContext to be shared across the model creation workflow objects
//Set a random seed for repeatable/deterministic results across multiple trainings.
var mlContext = new MLContext(seed: 1);
// Create, Train, Evaluate and Save a model
BuildTrainEvaluateAndSaveModel(mlContext);
ConsoleHelper.ConsoleWriteHeader(""=============== End of training process ==============="");
// Make a single test prediction loding the model from .ZIP file
TestSinglePrediction(mlContext);
ConsoleHelper.ConsoleWriteHeader(""=============== End of process, hit any key to finish ==============="");
Console.ReadKey();
}
private static ITransformer BuildTrainEvaluateAndSaveModel(MLContext mlContext)
{
// Data loading
IDataView trainingDataView = mlContext.Data.ReadFromTextFile<SampleObservation>(
path: TrainDataPath,
hasHeader : ");
this.Write(" private static string ModelPath = @\"./model.zip\";\r\n\r\n // Set this " +
"flag to enable the training process.\r\n private static bool EnableTraining" +
" = false;\r\n\r\n static void Main(string[] args)\r\n {\r\n // " +
"Create MLContext to be shared across the model creation workflow objects \r\n " +
" // Set a random seed for repeatable/deterministic results across multiple" +
" trainings.\r\n var mlContext = new MLContext(seed: 1);\r\n\r\n " +
"if (EnableTraining)\r\n {\r\n // Create, Train, Evaluate a" +
"nd Save a model\r\n BuildTrainEvaluateAndSaveModel(mlContext);\r\n " +
" ConsoleHelper.ConsoleWriteHeader(\"=============== End of training p" +
"rocess ===============\");\r\n }\r\n else\r\n {\r\n " +
" ConsoleHelper.ConsoleWriteHeader(\"Skipping the training process. Plea" +
"se set the flag : \'EnableTraining\' to \'true\' to enable the training process.\");\r" +
"\n }\r\n\r\n // Make a single test prediction loading the model" +
" from .ZIP file\r\n TestSinglePrediction(mlContext);\r\n\r\n Con" +
"soleHelper.ConsoleWriteHeader(\"=============== End of process, hit any key to fi" +
"nish ===============\");\r\n Console.ReadKey();\r\n\r\n }\r\n\r\n " +
"private static ITransformer BuildTrainEvaluateAndSaveModel(MLContext mlContext)\r" +
"\n {\r\n // Data loading\r\n IDataView trainingDataView " +
"= mlContext.Data.ReadFromTextFile<SampleObservation>(\r\n " +
" path: TrainDataPath,\r\n " +
" hasHeader : ");
this.Write(this.ToStringHelper.ToStringWithCulture(HasHeader.ToString().ToLowerInvariant()));
this.Write(",\r\n separatorChar : \'");
this.Write(this.ToStringHelper.ToStringWithCulture(Regex.Escape(Separator.ToString())));
Expand Down
22 changes: 16 additions & 6 deletions src/mlnet/Templates/Console/MLCodeGen.tt
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,27 @@ namespace <#= Namespace #>
<# } #>
private static string ModelPath = @"./model.zip";

// Set this flag to enable the training process.
private static bool EnableTraining = false;

static void Main(string[] args)
{
//Create MLContext to be shared across the model creation workflow objects
//Set a random seed for repeatable/deterministic results across multiple trainings.
// Create MLContext to be shared across the model creation workflow objects
// Set a random seed for repeatable/deterministic results across multiple trainings.
var mlContext = new MLContext(seed: 1);

// Create, Train, Evaluate and Save a model
BuildTrainEvaluateAndSaveModel(mlContext);
ConsoleHelper.ConsoleWriteHeader("=============== End of training process ===============");
if (EnableTraining)
{
// Create, Train, Evaluate and Save a model
BuildTrainEvaluateAndSaveModel(mlContext);
ConsoleHelper.ConsoleWriteHeader("=============== End of training process ===============");
}
else
{
ConsoleHelper.ConsoleWriteHeader("Skipping the training process. Please set the flag : 'EnableTraining' to 'true' to enable the training process.");
}

// Make a single test prediction loding the model from .ZIP file
// Make a single test prediction loading the model from .ZIP file
TestSinglePrediction(mlContext);

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

0 comments on commit cc7894f

Please sign in to comment.