Skip to content
This repository was archived by the owner on Mar 12, 2020. It is now read-only.

Example Housing Regression Problem

Deepak Kumar Battini edited this page Nov 13, 2017 · 1 revision

Import the namespaces for this example

using SiaNet;
using SiaNet.Common;
using SiaNet.Model;
using SiaNet.Model.Layers;
using SiaNet.Model.Optimizers;

Select the device CPU or GPU

GlobalParameters.Device = CNTK.DeviceDescriptor.CPUDevice;

Configure logging (Optional step)

Logging.OnWriteLog += Logging_OnWriteLog;

Create new instance of the sequential model

Sequential model = new Sequential();

Configure the training callbacks

model.OnEpochEnd += Model_OnEpochEnd;
model.OnTrainingEnd += Model_OnTrainingEnd;

Load and split Boston housing dataset

DataFrame frame = new DataFrame();
Downloader.DownloadSample(SampleDataset.HousingRegression);
var samplePath = Downloader.GetSamplePath(SampleDataset.HousingRegression);
frame.LoadFromCsv(samplePath.Train);
var xy = frame.SplitXY(14, new[] { 1, 13 });
traintest = xy.SplitTrainTest(0.25);

Build the model by stacking various neural network layers

model = new Sequential();
model.Add(new Dense(13, 12, OptActivations.ReLU));
model.Add(new Dense(13, OptActivations.ReLU));
model.Add(new Dense(1));

Compile the model and train

model.Compile(OptOptimizers.Adam, OptLosses.MeanSquaredError, OptMetrics.MAE, Regulizers.RegL2(0.01));
model.Train(traintest.Train, 500, 32, traintest.Test);

Event Functions

private static void Model_OnTrainingEnd(Dictionary<string, List<double>> trainingResult)
{
   var mean = trainingResult[OptMetrics.MAE].Mean();
   var std = trainingResult[OptMetrics.MAE].Std();
   Console.WriteLine("Training completed. Mean: {0}, Std: {1}", mean, std);
}
private static void Model_OnEpochEnd(int epoch, uint samplesSeen, double loss, Dictionary<string, double> metrics)
{
   Console.WriteLine(string.Format("Epoch: {0}, Loss: {1}, Accuracy: {2}", epoch, loss, metrics["val_mae"]));
}