-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathProgram.cs
190 lines (167 loc) · 7.52 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.IO;
using System.Linq;
using CNTK;
using CNTKUtil;
using XPlot.Plotly;
using Microsoft.ML;
using Microsoft.ML.Data;
namespace HousePricePrediction
{
/// <summary>
/// The HouseBlockData class holds one single housing block data record.
/// </summary>
public class HouseBlockData
{
[LoadColumn(0)] public float Longitude { get; set; }
[LoadColumn(1)] public float Latitude { get; set; }
[LoadColumn(2)] public float HousingMedianAge { get; set; }
[LoadColumn(3)] public float TotalRooms { get; set; }
[LoadColumn(4)] public float TotalBedrooms { get; set; }
[LoadColumn(5)] public float Population { get; set; }
[LoadColumn(6)] public float Households { get; set; }
[LoadColumn(7)] public float MedianIncome { get; set; }
[LoadColumn(8)] public float MedianHouseValue { get; set; }
public float[] GetFeatures() => new float[] { Longitude, Latitude, HousingMedianAge, TotalRooms, TotalBedrooms, Population, Households, MedianIncome };
public float GetLabel() => MedianHouseValue / 1000.0f;
}
class Program
{
/// <summary>
/// The main entry point of the application.
/// </summary>
/// <param name="args">The command line arguments.</param>
[STAThread]
public static void Main(string[] args)
{
// create the machine learning context
var context = new MLContext();
// check the current device for running neural networks
Console.WriteLine($"Using device: {NetUtil.CurrentDevice.AsString()}");
// load the dataset
Console.WriteLine("Loading data...");
var data = context.Data.LoadFromTextFile<HouseBlockData>(
path: "california_housing.csv",
hasHeader:true,
separatorChar: ',');
// split into training and testing partitions
var partitions = context.Data.TrainTestSplit(data, 0.2);
// load training and testing data
var training = context.Data.CreateEnumerable<HouseBlockData>(partitions.TrainSet, reuseRowObject: false);
var testing = context.Data.CreateEnumerable<HouseBlockData>(partitions.TestSet, reuseRowObject: false);
// set up data arrays
var training_data = training.Select(v => v.GetFeatures()).ToArray();
var training_labels = training.Select(v => v.GetLabel()).ToArray();
var testing_data = testing.Select(v => v.GetFeatures()).ToArray();
var testing_labels = testing.Select(v => v.GetLabel()).ToArray();
// build features and labels
var features = NetUtil.Var(new int[] { 8 }, DataType.Float);
var labels = NetUtil.Var(new int[] { 1 }, DataType.Float);
// build the network
var network = features
.Dense(8, CNTKLib.ReLU)
.Dense(8, CNTKLib.ReLU)
.Dense(1)
.ToNetwork();
Console.WriteLine("Model architecture:");
Console.WriteLine(network.ToSummary());
// set up the loss function and the classification error function
var lossFunc = NetUtil.MeanSquaredError(network.Output, labels);
var errorFunc = NetUtil.MeanAbsoluteError(network.Output, labels);
// set up a trainer that uses the RMSProp algorithm
var learner = network.GetAdamLearner(
learningRateSchedule: (0.001, 1),
momentumSchedule: (0.9, 1),
unitGain: false);
// set up a trainer and an evaluator
var trainer = network.GetTrainer(learner, lossFunc, errorFunc);
var evaluator = network.GetEvaluator(errorFunc);
// train the model
Console.WriteLine("Epoch\tTrain\t\tTrain\tTest");
Console.WriteLine("\tLoss\t\tError\tError");
Console.WriteLine("--------------------------------------");
var maxEpochs = 50;
var batchSize = 16;
var loss = new double[maxEpochs];
var trainingError = new double[maxEpochs];
var testingError = new double[maxEpochs];
var batchCount = 0;
for (int epoch = 0; epoch < maxEpochs; epoch++)
{
// train one epoch on batches
loss[epoch] = 0.0;
trainingError[epoch] = 0.0;
batchCount = 0;
training_data.Index().Shuffle().Batch(batchSize, (indices, begin, end) =>
{
// get the current batch
var featureBatch = features.GetBatch(training_data, indices, begin, end);
var labelBatch = labels.GetBatch(training_labels, indices, begin, end);
// train the network on the batch
var result = trainer.TrainBatch(
new[] {
(features, featureBatch),
(labels, labelBatch)
},
false
);
loss[epoch] += result.Loss;
trainingError[epoch] += result.Evaluation;
batchCount++;
});
// show results
loss[epoch] /= batchCount;
trainingError[epoch] /= batchCount;
Console.Write($"{epoch}\t{loss[epoch]:F3}\t{trainingError[epoch]:F3}\t");
// test one epoch on batches
testingError[epoch] = 0.0;
batchCount = 0;
testing_data.Batch(batchSize, (data, begin, end) =>
{
// get the current batch for testing
var featureBatch = features.GetBatch(testing_data, begin, end);
var labelBatch = labels.GetBatch(testing_labels, begin, end);
// test the network on the batch
testingError[epoch] += evaluator.TestBatch(
new[] {
(features, featureBatch),
(labels, labelBatch)
}
);
batchCount++;
});
testingError[epoch] /= batchCount;
Console.WriteLine($"{testingError[epoch]:F3}");
}
// show final results
var finalError = testingError[maxEpochs-1];
Console.WriteLine();
Console.WriteLine($"Final test MAE: {finalError:0.00}");
// plot the error graph
var chart = Chart.Plot(
new []
{
new Graph.Scatter()
{
x = Enumerable.Range(0, maxEpochs).ToArray(),
y = trainingError,
name = "training",
mode = "lines+markers"
},
new Graph.Scatter()
{
x = Enumerable.Range(0, maxEpochs).ToArray(),
y = testingError,
name = "testing",
mode = "lines+markers"
}
}
);
chart.WithXTitle("Epoch");
chart.WithYTitle("Mean absolute error (MAE)");
chart.WithTitle("California House Training");
// save chart
File.WriteAllText("chart.html", chart.GetHtml());
}
}
}