/******************************************************** * ██████╗ ██████╗████████╗██╗ * ██╔════╝ ██╔════╝╚══██╔══╝██║ * ██║ ███╗██║ ██║ ██║ * ██║ ██║██║ ██║ ██║ * ╚██████╔╝╚██████╗ ██║ ███████╗ * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ * Geophysical Computational Tools & Library (GCTL) * * Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn) * * GCTL is distributed under a dual licensing scheme. You can redistribute * it and/or modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation, either version 2 * of the License, or (at your option) any later version. You should have * received a copy of the GNU Lesser General Public License along with this * program. If not, see . * * If the terms and conditions of the LGPL v.2. would prevent you from using * the GCTL, please consider the option to obtain a commercial license for a * fee. These licenses are offered by the GCTL's original author. As a rule, * licenses are provided "as-is", unlimited in time for a one time fee. Please * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget * to include some description of your company and the realm of its activities. * Also add information on how to contact you by electronic and paper mail. ******************************************************/ #include "../data/MNIST/mnist_database.h" #include "../lib/dnn.h" using namespace gctl; int main(int argc, char const *argv[]) try { mnist_database data("data/MNIST"); matrix train_obs(784, 60000), train_lab(10, 60000, 0.0); matrix test_obs(784, 10000), test_lab(10, 10000, 0.0), predicts(10, 10000); const std::vector > &dt_obs = data.train_images(); for (size_t i = 0; i < 60000; i++) { for (size_t j = 0; j < 784; j++) { train_obs[j][i] = dt_obs[i][j]/255.0; } } const std::vector > &dt_obs2 = data.test_images(); for (size_t i = 0; i < 10000; i++) { for (size_t j = 0; j < 784; j++) { test_obs[j][i] = dt_obs2[i][j]/255.0; } } const std::vector &dt_lab = data.train_labels(); for (size_t i = 0; i < 60000; i++) { train_lab[dt_lab[i]][i] = 1.0; } const std::vector &dt_lab2 = data.test_labels(); for (size_t i = 0; i < 10000; i++) { test_lab[dt_lab2[i]][i] = 1.0; } dnn my_nn("Ex-MNIST"); my_nn.add_hind_layer(784, 800, FullyConnected, PReLU); my_nn.add_hind_layer(800, 10, FullyConnected, SoftMax); my_nn.add_output_layer(MultiClassEntropy); my_nn.add_train_set(train_obs, train_lab, 1000); my_nn.init_network(0.0, 0.1); //sgd_para my_para = my_nn.default_sgd_para(); //my_para.alpha = 0.01; //my_para.epsilon = 1e-5; //my_nn.train_network(my_para, gctl::ADAM); lgd_para my_para = my_nn.default_lgd_para(); my_para.flight_times = 1000; my_para.alpha = 0.08; my_para.beta = 1.8; my_nn.train_network(my_para); my_nn.predict(test_obs, predicts); my_nn.save_network("data/saved_networks/mnist_m1"); int wrong_predicts = 0; int test_id, pre_id; double test_scr, pre_scr; for (size_t j = 0; j < 10000; j++) { test_id = pre_id = 0; test_scr = pre_scr = 0; for (size_t i = 0; i < 10; i++) { if (test_lab[i][j] > test_scr) {test_scr = test_lab[i][j]; test_id = i;} if (predicts[i][j] > pre_scr) {pre_scr = predicts[i][j]; pre_id = i;} } if (test_id != pre_id) { wrong_predicts++; } } std::cout << "Correct Rate = " << (10000 - wrong_predicts)/100.0 << "%\n"; return 0; } catch (std::exception &e) { GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0); }