gctl_ai/examples/ex_mnist3.cpp
2024-09-10 20:15:33 +08:00

87 lines
3.3 KiB
C++

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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 <http://www.gnu.org/licenses/>.
*
* 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");
dnn my_nn("Ex-MNIST");
my_nn.load_network("data/saved_networks/mnist_m1.gctl.dnn");
my_nn.show_network();
my_nn.save_layer2text(0, "data/saved_networks/mnist_m1_layer1");
my_nn.save_layer2text(1, "data/saved_networks/mnist_m1_layer2");
/*
matrix<double> test_obs(784, 10000), test_lab(10, 10000, 0.0), predicts(10, 10000);
const std::vector<std::vector<double> > &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<double> &dt_lab2 = data.test_labels();
for (size_t i = 0; i < 10000; i++)
{
test_lab[dt_lab2[i]][i] = 1.0;
}
my_nn.predict(test_obs, predicts);
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);
}