M
M
mIka012021-05-20 16:43:56
Neural networks
mIka01, 2021-05-20 16:43:56

How to dynamically add layers in a neural network?

Neural network. Vectors instead of arrays. How to make dynamic selection of vectors when we choose how many layers we need between start and end. For example, in the interface, indicate that I have 3 layers and these 3 layers themselves were created as a vector of vectors.

int main()
{
    setlocale(LC_ALL, "Russian");

    vector < vector < double >> no(10, vector<double>(2)); // нулевой слой        

    vector < vector < double >> n1(5, vector<double>(2)); // первый слой        

    vector < vector < double >> n2(4, vector<double>(2)); // второй слой        

    vector < vector < double >> n3(2, vector<double>(2)); // третий слой

    vector < vector < double >> idl(2, vector<double>(1)); // нужно получить

    static double k = 0.5;

    vector < vector < double >> w01(10, vector<double>(4));
    vector < vector < double >> w12(5, vector<double>(3));
    vector < vector < double >> w23(4, vector<double>(2));


    vector < vector < double >> test_questions =
    {
        { 1, 1, 1, 0, 0, 0, 0, 0, 0 },
        { 0, 1, 0, 0, 1, 0, 0, 1, 0 },
        { 1, 1, 1, 0, 0, 1, 0, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 1, 1, 1 },
        { 0, 0, 0, 1, 1, 1, 0, 0, 0 },
        { 0, 0, 1, 0, 0, 1, 0, 0, 1 },
        { 1, 0, 0, 1, 1, 1, 1, 0, 0 },
        { 0, 1, 0, 0, 1, 0, 1, 1, 1 },
        { 0, 0, 0, 0, 0, 0, 1, 1, 1 },
        { 1, 1, 1, 1, 0, 0, 1, 0, 0 },
        { 0, 1, 0, 1, 1, 1, 0, 1, 0 },
        { 0, 0, 1, 0, 0, 1, 1, 1, 1 },
        { 1, 0, 0, 1, 0, 0, 1, 0, 0 },
        { 1, 1, 1, 0, 1, 0, 0, 1, 0 },
        { 0, 0, 1, 1, 1, 1, 0, 0, 1 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    };
    vector < vector < double >> test_answers =
    {
        { 1, 0 },
        { 0, 1 },
        { 1, 1 },
        { 1, 1 },
        { 1, 0 },
        { 0, 1 },
        { 1, 1 },
        { 1, 1 },
        { 1, 0 },
        { 1, 1 },
        { 1, 1 },
        { 1, 1 },
        { 0, 1 },
        { 1, 1 },
        { 1, 1 },
        { 0, 0 },
    };

    no[9][0] = 1;
    n1[4][0] = 1;
    n2[3][0] = 1;

    communication neuro;
    neuro.fillW(w01); // рандом весов
    neuro.fillW(w12);
    neuro.fillW(w23);


    for (int i = 0; i < 100000; i++)
    {

        for (int j = 0; j < test_questions[0].size(); j++)
        {
            neuro.getTask(no, test_questions, test_answers, idl, j); // новый пример для нейроситей

            neuro.forWards(no, w01, n1); // проход по слою
            neuro.forWards(n1, w12, n2);
            neuro.forWards(n2, w23, n3);

            neuro.fixOutError(idl, n3); // нахождение отклонений
            neuro.findError(n2, w23, n3); // нахождение ошибок слоя
            neuro.findError(n1, w12, n2);

            neuro.backWards(n2, w23, n3, k); // изменение весов
            neuro.backWards(n1, w12, n2, k);
            neuro.backWards(no, w01, n1, k);
        }



    }

    cout << "===========================================================" << endl;

    while (true)
    {
        int i;
        cin >> i;

        neuro.getTask(no, test_questions, test_answers, idl, i);
        neuro.forWards(no, w01, n1);
        neuro.forWards(n1, w12, n2);
        neuro.forWards(n2, w23, n3);

        neuro.read(test_questions, test_answers, i);
        cout << endl;
        neuro.write(n3);
        cout << "===========================================================" << endl;
    }
    return 0;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question