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

Function test_simple_linear_regression_eil

dlib/test/dnn.cpp:3918–3961  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3916// ----------------------------------------------------------------------------------------
3917
3918 void test_simple_linear_regression_eil()
3919 {
3920 print_spinner();
3921 const int num_samples = 1000;
3922 ::std::vector<matrix<double>> x(num_samples);
3923 ::std::vector<float> y(num_samples);
3924 ::std::default_random_engine generator(16);
3925 ::std::normal_distribution<float> distribution(0,0.0001);
3926 const float true_intercept = 50.0;
3927 const float true_slope = 10.0;
3928 for ( int ii = 0; ii < num_samples; ++ii )
3929 {
3930 const double val = static_cast<double>(ii)/10;
3931 matrix<double> tmp(1,1);
3932 tmp = val;
3933 x[ii] = tmp;
3934 y[ii] = (true_intercept + true_slope*static_cast<float>(val) + distribution(generator));
3935 }
3936
3937 using net_type = loss_epsilon_insensitive<fc<1, input<matrix<double>>>>;
3938 net_type net(0.01);
3939 layer<1>(net).layer_details().set_bias_learning_rate_multiplier(300);
3940 sgd defsolver(0,0.9);
3941 dnn_trainer<net_type> trainer(net, defsolver);
3942 trainer.set_learning_rate(1e-5);
3943 trainer.set_min_learning_rate(1e-8);
3944 trainer.set_mini_batch_size(50);
3945 trainer.set_max_num_epochs(570);
3946 trainer.train(x, y);
3947
3948 const float slope = layer<1>(net).layer_details().get_weights().host()[0];
3949 const float slope_error = abs(true_slope - slope);
3950 const float intercept = layer<1>(net).layer_details().get_biases().host()[0];
3951 const float intercept_error = abs(true_intercept - intercept);
3952 const float eps_slope = 0.01, eps_intercept = 0.1;
3953
3954 dlog << LINFO << "slope_error: "<< slope_error;
3955 dlog << LINFO << "intercept_error: "<< intercept_error;
3956 DLIB_TEST_MSG(slope_error <= eps_slope,
3957 "Expected slope = " << true_slope << " Estimated slope = " << slope << " Error limit = " << eps_slope);
3958 DLIB_TEST_MSG(intercept_error <= eps_intercept,
3959 "Expected intercept = " << true_intercept << " Estimated intercept = " << intercept << " Error limit = " << eps_intercept);
3960
3961 }
3962
3963// ----------------------------------------------------------------------------------------
3964

Callers 1

run_testsMethod · 0.85

Calls 11

print_spinnerFunction · 0.85
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