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

Function test_multioutput_linear_regression

dlib/test/dnn.cpp:4056–4115  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4054// ----------------------------------------------------------------------------------------
4055
4056 void test_multioutput_linear_regression()
4057 {
4058 const int num_outputs = 2;
4059 const int num_samples = 1000;
4060 ::std::vector<matrix<double>> x(num_samples);
4061 ::std::vector<matrix<float>> y(num_samples);
4062 ::std::default_random_engine generator(16);
4063 ::std::normal_distribution<float> distribution(0,0.1);
4064 ::std::normal_distribution<float> slope_distribution(10,5);
4065 ::std::normal_distribution<float> intercept_distribution(50,10);
4066 ::std::vector<float> true_intercepts(num_outputs);
4067 ::std::vector<float> true_slopes(num_outputs);
4068 for ( int jj = 0; jj < num_outputs; ++jj )
4069 {
4070 true_slopes[jj] = slope_distribution(generator);
4071 true_intercepts[jj] = intercept_distribution(generator);
4072 }
4073 matrix<float> ytmp(num_outputs, 1);
4074 for ( int ii = 0; ii < num_samples; ++ii )
4075 {
4076 const double val = static_cast<double>(ii)/10;
4077 matrix<double> tmp(1,1);
4078 tmp = val;
4079 x[ii] = tmp;
4080 for ( int jj = 0; jj < num_outputs; ++jj )
4081 ytmp(jj, 0) = (true_intercepts[jj] + true_slopes[jj]*static_cast<float>(val) + distribution(generator));
4082
4083 y[ii] = ytmp;
4084 }
4085
4086 using net_type = loss_mean_squared_multioutput<fc<num_outputs, input<matrix<double>>>>;
4087 net_type net;
4088 layer<1>(net).layer_details().set_bias_learning_rate_multiplier(900);
4089 sgd defsolver(0,0.9);
4090 dnn_trainer<net_type> trainer(net, defsolver);
4091 trainer.set_learning_rate(1e-5);
4092 trainer.set_min_learning_rate(1e-6);
4093 trainer.set_mini_batch_size(50);
4094 trainer.set_max_num_epochs(170);
4095 trainer.train(x, y);
4096
4097 float slope_error = 0.0;
4098 float intercept_error = 0.0;
4099 const float eps_slope = 0.05, eps_intercept = 0.1;
4100
4101 for ( int jj = 0; jj < num_outputs; ++jj )
4102 {
4103 slope_error += abs(layer<1>(net).layer_details().get_weights().host()[jj] - true_slopes[jj]);
4104 intercept_error += abs(layer<1>(net).layer_details().get_biases().host()[jj] - true_intercepts[jj]);
4105 }
4106
4107 slope_error /= float(num_outputs);
4108 intercept_error /= float(num_outputs);
4109
4110 DLIB_TEST_MSG(slope_error <= eps_slope,
4111 "Average absolute slope error = " << slope_error << " Error limit = " << eps_slope);
4112 DLIB_TEST_MSG(intercept_error <= eps_intercept,
4113 "Average absolute intercept error = " << intercept_error << " Error limit = " << eps_intercept);

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