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

Function test_linear

dlib/test/dnn.cpp:4194–4246  ·  view source on GitHub ↗

----------------------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

4192
4193// ----------------------------------------------------------------------------------------
4194 void test_linear()
4195 {
4196 print_spinner();
4197
4198 // Define the network
4199 using net_type = tag2<linear_no_bias<6, tag1<input<matrix<float>>>>>;
4200 net_type net;
4201
4202 // Input tensor
4203 const int n_samples = 3, k = 1;
4204 std::vector<matrix<float>> x(n_samples);
4205 matrix<float> xtmp(2, 4);
4206 xtmp = 1.0f, 2.0f, 3.0f, 4.0f,
4207 5.0f, 6.0f, 7.0f, 8.0f;
4208 x[0] = xtmp;
4209 xtmp = 9.0f, 10.0f, 11.0f, 12.0f,
4210 13.0f, 14.0f, 15.0f, 16.0f;
4211 x[1] = xtmp;
4212 xtmp = 17.0f, 18.0f, 19.0f, 20.0f,
4213 21.0f, 22.0f, 23.0f, 24.0f;
4214 x[2] = xtmp;
4215
4216 // Convert input matrix to tensor
4217 resizable_tensor input_tensor;
4218 net.to_tensor(&x[0], &x[0] + n_samples, input_tensor);
4219 net.forward(input_tensor);
4220
4221 // Get the internal linear weights
4222 matrix<float> w = mat(layer<tag2>(net).subnet().layer_details().get_weights());
4223
4224 // Theoretical calculation of the output
4225 std::vector<matrix<float>> expected_outputs(n_samples);
4226 for (int i = 0; i < n_samples; ++i) {
4227 matrix<float> input_matrix = x[i];
4228 expected_outputs[i] = input_matrix * w;
4229 }
4230
4231 // Compare output tensor with expected output
4232 auto& net_output = layer<tag2>(net).get_output();
4233
4234 // Display results
4235 for (int i = 0; i < n_samples; ++i) {
4236 matrix<float> output_sample;
4237 output_sample.set_size(2, 6);
4238 for (long r = 0; r < output_sample.nr(); ++r) {
4239 for (long c = 0; c < output_sample.nc(); ++c) {
4240 output_sample(r, c) = net_output.host()[tensor_index(net_output, i, 0, r, c)];
4241 }
4242 }
4243 DLIB_TEST_MSG(max(abs(output_sample - expected_outputs[i])) < 1e-5,
4244 "linear layer - sample " + std::to_string(i));
4245 }
4246 }
4247
4248// ----------------------------------------------------------------------------------------
4249

Callers 1

run_testsMethod · 0.85

Calls 12

print_spinnerFunction · 0.85
tensor_indexFunction · 0.85
absFunction · 0.85
matFunction · 0.50
maxFunction · 0.50
to_tensorMethod · 0.45
forwardMethod · 0.45
get_weightsMethod · 0.45
set_sizeMethod · 0.45
nrMethod · 0.45
ncMethod · 0.45
hostMethod · 0.45

Tested by

no test coverage detected