@@ -32,32 +32,29 @@ public virtual string TransformText()
32
32
} else if ( Target == CSharp . GenerateTarget . ModelBuilder ) {
33
33
MB_Annotation ( ) ;
34
34
}
35
- this . Write ( "\r \n using System;\r \n using System.IO;\r \n using System.Linq;\r \n using Microsoft.ML;\r \n using" +
36
- " " ) ;
35
+ this . Write ( "\r \n using System;\r \n using " ) ;
37
36
this . Write ( this . ToStringHelper . ToStringWithCulture ( Namespace ) ) ;
38
37
this . Write ( ".Model;\r \n \r \n namespace " ) ;
39
38
this . Write ( this . ToStringHelper . ToStringWithCulture ( Namespace ) ) ;
40
- this . Write ( ".ConsoleApp\r \n {\r \n class Program\r \n {\r \n //Dataset to use for prediction" +
41
- "s \r \n " ) ;
42
- if ( string . IsNullOrEmpty ( TestDataPath ) ) {
43
- this . Write ( " private const string DATA_FILEPATH = @\" " ) ;
44
- this . Write ( this . ToStringHelper . ToStringWithCulture ( TrainDataPath ) ) ;
45
- this . Write ( "\" ;\r \n " ) ;
46
- } else {
47
- this . Write ( " private const string DATA_FILEPATH = @\" " ) ;
48
- this . Write ( this . ToStringHelper . ToStringWithCulture ( TestDataPath ) ) ;
49
- this . Write ( "\" ;\r \n " ) ;
50
- }
51
- this . Write ( @"
52
- static void Main(string[] args)
53
- {
54
- // Create single instance of sample data from first line of dataset for model input
55
- ModelInput sampleData = CreateSingleDataSample(DATA_FILEPATH);
56
-
57
- // Make a single prediction on the sample data and print results
58
- var predictionResult = ConsumeModel.Predict(sampleData);
59
-
60
- Console.WriteLine(""Using model to make single prediction -- Comparing actual " ) ;
39
+ this . Write ( ".ConsoleApp\r \n {\r \n class Program\r \n {\r \n static void Main(string[] args)" +
40
+ "\r \n {\r \n // Create single instance of sample data from first lin" +
41
+ "e of dataset for model input\r \n " ) ;
42
+ if ( SampleData != null ) {
43
+ this . Write ( " ModelInput sampleData = new ModelInput()\r \n {\r \n " ) ;
44
+ foreach ( var kv in SampleData ) {
45
+ this . Write ( " " ) ;
46
+ this . Write ( this . ToStringHelper . ToStringWithCulture ( kv . Key ) ) ;
47
+ this . Write ( "=" ) ;
48
+ this . Write ( this . ToStringHelper . ToStringWithCulture ( kv . Value ) ) ;
49
+ this . Write ( ",\r \n " ) ;
50
+ }
51
+ this . Write ( " };\r \n " ) ;
52
+ } else {
53
+ this . Write ( " ModelInput sampleData = new ModelInput();\r \n " ) ;
54
+ }
55
+ this . Write ( "\r \n \t \t \t // Make a single prediction on the sample data and print results\r \n \t \t \t var pre" +
56
+ "dictionResult = ConsumeModel.Predict(sampleData);\r \n \r \n \t \t \t Console.WriteLine(\" Using" +
57
+ " model to make single prediction -- Comparing actual " ) ;
61
58
this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
62
59
this . Write ( " with predicted " ) ;
63
60
this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
@@ -70,81 +67,35 @@ static void Main(string[] args)
70
67
this . Write ( "}\" );\r \n " ) ;
71
68
}
72
69
if ( "BinaryClassification" . Equals ( TaskType ) ) {
73
- this . Write ( "\t \t \t Console.WriteLine($\" \\ n\\ nActual " ) ;
74
- this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
75
- this . Write ( ": {sampleData." ) ;
76
- this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
77
- this . Write ( "} \\ nPredicted " ) ;
70
+ this . Write ( "\t \t \t Console.WriteLine($\" \\ n\\ nPredicted " ) ;
78
71
this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
79
72
this . Write ( ": {predictionResult.Prediction}\\ n\\ n\" );\r \n " ) ;
80
73
} else if ( "Regression" . Equals ( TaskType ) || "Recommendation" . Equals ( TaskType ) ) {
81
- this . Write ( "\t \t \t Console.WriteLine($\" \\ n\\ nActual " ) ;
82
- this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
83
- this . Write ( ": {sampleData." ) ;
84
- this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
85
- this . Write ( "} \\ nPredicted " ) ;
74
+ this . Write ( "\t \t \t Console.WriteLine($\" \\ n\\ nPredicted " ) ;
86
75
this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
87
76
this . Write ( ": {predictionResult.Score}\\ n\\ n\" );\r \n " ) ;
88
77
} else if ( "MulticlassClassification" . Equals ( TaskType ) ) {
89
- this . Write ( "\t \t \t Console.WriteLine($\" \\ n\\ nActual " ) ;
90
- this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
91
- this . Write ( ": {sampleData." ) ;
92
- this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
93
- this . Write ( "} \\ nPredicted " ) ;
78
+ this . Write ( "\t \t \t Console.WriteLine($\" \\ n\\ nPredicted " ) ;
94
79
this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
95
80
this . Write ( " value {predictionResult.Prediction} \\ nPredicted " ) ;
96
81
this . Write ( this . ToStringHelper . ToStringWithCulture ( Utils . Normalize ( LabelName ) ) ) ;
97
82
this . Write ( " scores: [{String.Join(\" ,\" , predictionResult.Score)}]\\ n\\ n\" );\r \n " ) ;
98
83
}
99
- this . Write ( @" Console.WriteLine(""=============== End of process, hit any key to finish ==============="");
100
- Console.ReadKey();
101
- }
102
-
103
- // Change this code to create your own sample data
104
- #region CreateSingleDataSample
105
- // Method to load single row of dataset to try a single prediction
106
- private static ModelInput CreateSingleDataSample(string dataFilePath)
107
- {
108
- // Create MLContext
109
- MLContext mlContext = new MLContext();
110
-
111
- // Load dataset
112
- IDataView dataView = mlContext.Data.LoadFromTextFile<ModelInput>(
113
- path: dataFilePath,
114
- hasHeader : " ) ;
115
- this . Write ( this . ToStringHelper . ToStringWithCulture ( HasHeader . ToString ( ) . ToLowerInvariant ( ) ) ) ;
116
- this . Write ( ",\r \n separatorChar : \' " ) ;
117
- this . Write ( this . ToStringHelper . ToStringWithCulture ( Regex . Escape ( Separator . ToString ( ) ) ) ) ;
118
- this . Write ( "\' ,\r \n allowQuoting : " ) ;
119
- this . Write ( this . ToStringHelper . ToStringWithCulture ( AllowQuoting . ToString ( ) . ToLowerInvariant ( ) ) ) ;
120
- this . Write ( ",\r \n allowSparse: " ) ;
121
- this . Write ( this . ToStringHelper . ToStringWithCulture ( AllowSparse . ToString ( ) . ToLowerInvariant ( ) ) ) ;
122
- this . Write ( @");
123
-
124
- // Use first line of dataset as model input
125
- // You can replace this with new test data (hardcoded or from end-user application)
126
- ModelInput sampleForPrediction = mlContext.Data.CreateEnumerable<ModelInput>(dataView, false)
127
- .First();
128
- return sampleForPrediction;
129
- }
130
- #endregion
131
- }
132
- }
133
- " ) ;
84
+ this . Write ( " Console.WriteLine(\" =============== End of process, hit any key to fin" +
85
+ "ish ===============\" );\r \n Console.ReadKey();\r \n }\r \n }\r \n }\r \n " ) ;
134
86
return this . GenerationEnvironment . ToString ( ) ;
135
87
}
136
88
137
89
public string TaskType { get ; set ; }
138
90
public string Namespace { get ; set ; }
139
91
public string LabelName { get ; set ; }
140
- public string TestDataPath { get ; set ; }
141
- public string TrainDataPath { get ; set ; }
142
92
public char Separator { get ; set ; }
143
93
public bool AllowQuoting { get ; set ; }
144
94
public bool AllowSparse { get ; set ; }
145
95
public bool HasHeader { get ; set ; }
146
96
public IList < string > Features { get ; set ; }
147
97
internal CSharp . GenerateTarget Target { get ; set ; }
98
+ public IDictionary < string , string > SampleData { get ; set ; }
148
99
149
100
150
101
void CLI_Annotation ( )
0 commit comments