MCPcopy Create free account
hub / github.com/davisking/dlib / test_simple_linear_regression

Function test_simple_linear_regression

dlib/test/dnn.cpp:3874–3914  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3872// ----------------------------------------------------------------------------------------
3873
3874 void test_simple_linear_regression()
3875 {
3876 const int num_samples = 1000;
3877 ::std::vector<matrix<double>> x(num_samples);
3878 ::std::vector<float> y(num_samples);
3879 ::std::default_random_engine generator(16);
3880 ::std::normal_distribution<float> distribution(0,0.1);
3881 const float true_intercept = 50.0;
3882 const float true_slope = 10.0;
3883 for ( int ii = 0; ii < num_samples; ++ii )
3884 {
3885 const double val = static_cast<double>(ii)/10;
3886 matrix<double> tmp(1,1);
3887 tmp = val;
3888 x[ii] = tmp;
3889 y[ii] = (true_intercept + true_slope*static_cast<float>(val) + distribution(generator));
3890 }
3891
3892 using net_type = loss_mean_squared<fc<1, input<matrix<double>>>>;
3893 net_type net;
3894 layer<1>(net).layer_details().set_bias_learning_rate_multiplier(300);
3895 sgd defsolver(0,0.9);
3896 dnn_trainer<net_type> trainer(net, defsolver);
3897 trainer.set_learning_rate(1e-5);
3898 trainer.set_min_learning_rate(1e-6);
3899 trainer.set_mini_batch_size(50);
3900 trainer.set_max_num_epochs(170);
3901 trainer.train(x, y);
3902
3903 const float slope = layer<1>(net).layer_details().get_weights().host()[0];
3904 const float slope_error = abs(true_slope - slope);
3905 const float intercept = layer<1>(net).layer_details().get_biases().host()[0];
3906 const float intercept_error = abs(true_intercept - intercept);
3907 const float eps_slope = 0.05, eps_intercept = 0.1;
3908
3909 DLIB_TEST_MSG(slope_error <= eps_slope,
3910 "Expected slope = " << true_slope << " Estimated slope = " << slope << " Error limit = " << eps_slope);
3911 DLIB_TEST_MSG(intercept_error <= eps_intercept,
3912 "Expected intercept = " << true_intercept << " Estimated intercept = " << intercept << " Error limit = " << eps_intercept);
3913
3914 }
3915
3916// ----------------------------------------------------------------------------------------
3917

Callers 1

run_testsMethod · 0.85

Calls 10

absFunction · 0.85
set_learning_rateMethod · 0.80
set_min_learning_rateMethod · 0.80
set_mini_batch_sizeMethod · 0.80
set_max_num_epochsMethod · 0.80
trainMethod · 0.45
hostMethod · 0.45
get_weightsMethod · 0.45
get_biasesMethod · 0.45

Tested by

no test coverage detected