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

Function test_simple_autoencoder

dlib/test/dnn.cpp:4119–4191  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4117// ----------------------------------------------------------------------------------------
4118
4119 void test_simple_autoencoder()
4120 {
4121 print_spinner();
4122
4123 srand(1234);
4124
4125 const int output_width = 7;
4126 const int output_height = 7;
4127 const int num_samples = 100;
4128 ::std::vector<matrix<float>> x(num_samples);
4129
4130 matrix<float> tmp(output_width, output_height);
4131 for (int i = 0; i < num_samples; ++i)
4132 {
4133 const int model = i % 4;
4134
4135 for (int r = 0; r < output_height; ++r)
4136 for (int c = 0; c < output_width; ++c)
4137 switch (model) {
4138 case 0: tmp(r, c) = r / output_height; break;
4139 case 1: tmp(r, c) = c / output_width; break;
4140 case 2: tmp(r, c) = 1.0 - r / output_height; break;
4141 case 3: tmp(r, c) = 1.0 - c / output_width; break;
4142 default: DLIB_TEST_MSG(false, "Invalid model: " << model << " (should be between 0 and 3)");
4143 }
4144
4145 x[i] = tmp;
4146 }
4147
4148 using net_type = loss_mean_squared_per_pixel<
4149 cont<1,output_height,output_width,2,2,
4150 relu<con<4,output_height,output_width,2,2,
4151 input<matrix<float>>>>>>;
4152 net_type net;
4153
4154 const auto autoencoder_error = [&x, &net, &output_height, &output_width]()
4155 {
4156 const auto y = net(x);
4157 double error = 0.0;
4158 for (size_t i = 0; i < x.size(); ++i)
4159 for (int r = 0; r < output_height; ++r)
4160 for (int c = 0; c < output_width; ++c)
4161 error += fabs(y[i](r, c) - x[i](r, c));
4162
4163 return error / (x.size() * output_height * output_width);
4164 };
4165
4166 // The autoencoder can't be very good before it's been trained
4167 // (or at least the probability of the reconstruction error
4168 // being small should be super low; in fact, the error ought to
4169 // be much higher than 0.01, however since the initialization
4170 // is random, putting the limit below too high could make the
4171 // tests fail when other, unrelated tests are added into the
4172 // sequence)
4173 const double error_before = autoencoder_error();
4174 DLIB_TEST_MSG(error_before > 0.01, "Autoencoder error before training = " << error_before);
4175
4176 // Make sure there's an information bottleneck, as intended

Callers 1

run_testsMethod · 0.85

Calls 9

print_spinnerFunction · 0.85
tmpFunction · 0.85
set_learning_rateMethod · 0.80
set_max_num_epochsMethod · 0.80
sizeMethod · 0.45
nrMethod · 0.45
ncMethod · 0.45
kMethod · 0.45
trainMethod · 0.45

Tested by

no test coverage detected