public void TrainModel() { double w1 = 0.1; double w2 = 0.1; double bias = 0.0; // Conduct gradient descent for (int j = 0; j < numInput; ++j) { double yHat = DLHelper.CalcLinReg(inputX1[j], inputX2[j], w1, w2, bias); double diff = yHat - labelY[j]; // update w1, w2, and bias w1 += gradientDescentW(inputX1[j], diff); w2 += gradientDescentW(inputX2[j], diff); bias += gradientDescentB(diff); } trainedW1 = w1; trainedW2 = w2; trainedBias = bias; Console.WriteLine(string.Format("The trained W1 is {0:f4}", trainedW1)); Console.WriteLine(string.Format("The trained W2 is {0:f4}", trainedW2)); Console.WriteLine(string.Format("The trained bias is {0:f4}", trainedBias)); Console.WriteLine(); }
static void TestLinearRegression() { // Generate training data set. double w1 = 2.0; double w2 = -3.4; double bias = 4.2; Console.WriteLine(string.Format("The true W1 is {0:f4}", w1)); Console.WriteLine(string.Format("The true W2 is {0:f4}", w2)); Console.WriteLine(string.Format("The true bias is {0:f4}", bias)); Console.WriteLine(); int numberOfExample = 1000; DataGenerator dg = new DataGenerator(w1, w2, bias, numberOfExample); dg.GenerateInputAndLabel(); //string filePath = @"F:\Hanjin.Hu\Research\DeepLearning\DeepLearning\LinReg.csv"; //dg.OutputData(filePath); List <double> inputX1 = dg.GeneratedX1.ToList(); List <double> inputX2 = dg.GeneratedX2.ToList(); List <double> labelY = dg.GeneratedLabelY.ToList(); // Train the neural network. LinearRegressionNN lrnn = new LinearRegressionNN(inputX1, inputX2, labelY); lrnn.TrainModel(); // Generate testing data set. dg.Clear(); dg.GenerateInputAndLabel(); List <double> testX1 = dg.GeneratedX1.ToList(); List <double> testX2 = dg.GeneratedX2.ToList(); List <double> testY = dg.GeneratedLabelY.ToList(); // Test the model for (int i = 0; i < 5; ++i) { double predictedY = lrnn.Predict(testX1[i], testX2[i]); double linReg = DLHelper.CalcLinReg(testX1[i], testX2[i], w1, w2, bias); //Console.WriteLine(string.Format("The real house price is {0:f6}", testY[i])); Console.WriteLine(string.Format("The linear reg price is {0:f4}", linReg)); Console.WriteLine(string.Format("The predicted house price is {0:f4}", predictedY)); Console.WriteLine(); } }
protected void GenerateLabelY() { if (generatedX1.Count == 0) { GenerateX1(); } if (generatedX2.Count == 0) { GenerateX2(); } double mean = 0.0; double stdDev = 0.01; Normal normalDist = new Normal(mean, stdDev); for (int i = 0; i < numExamples; ++i) { double y = DLHelper.CalcLinReg(generatedX1[i], generatedX2[i], trueW1, trueW2, trueBias) + normalDist.Sample(); generatedLabelY.Add(y); } }