简体   繁体   中英

my c++ code is a lot slower than javascript code

i have a project which use the same data, which in my c++ code it need 17sec to train 100 data, meanwhile in javascript code from this project

https://github.com/CodingTrain/Toy-Neural-Network-JS it running only about 10sec to train 2400data please someone help me whats wrong, and i need to complete my project for my undergraduate thesis.

ive already build 2 project, which one of them(this) is the same neuralnetwork in c++ from that javascript code(kinda), but still giving the same results

NeuralNetwork::NeuralNetwork(int a,int b,int c)
{
    this->numInput = a;
    this->numHidden = b;
    this->numOutput = c;
    std::vector<double> vec(a, 0.1);
    for (int i = 0; i < b; ++i) {
        this->weightIH.push_back(vec);
    }
    std::vector<double> vec2(b, 0.1);
    for (int i = 0; i < c; ++i) {
        this->weightHO.push_back(vec2);
    }

}


NeuralNetwork::~NeuralNetwork()
{
}

std::vector<double> NeuralNetwork::tambahbias(std::vector<double> a) {
    int size = a.size();
    for (int i = 0; i < size; ++i) {
        a[i] = a[i] + 1;
    }

    return a;
}

std::vector<double> NeuralNetwork::activate(std::vector<double> a) {
    int size = a.size();
    for (int i = 0; i < size; ++i) {
        a[i] = a[i] / (1 + abs(a[i]));
    }
    return a;
}

std::vector<double> NeuralNetwork::derivation(std::vector<double> a) {
    int size = a.size();
    for (int i = 0; i < size; ++i) {
        a[i] = a[i] * (1 - a[i]);
    }
    return a;
}

std::vector<double> NeuralNetwork::hitungError(std::vector<double> a, std::vector<double> b) {
    int size = a.size();
    for (int i = 0; i < size; ++i) {
        a[i] = b[i] - a[i];
    }

    return a;
}


    void NeuralNetwork::train(std::vector<double> a, std::vector<double> target) {
        std::vector<double> hidden(numHidden);
        for (int i = 0; i < numHidden; ++i) {
            for (int j = 0; j < numInput; ++j) {
                hidden[i] += a[j] * weightIH[i][j];
            }
        }
        hidden = tambahbias(hidden);
        hidden = activate(hidden);
        std::vector<double> output(numOutput);
        for (int i = 0; i < numOutput; ++i) {
            for (int j = 0; j < numHidden; ++j) {
                output[i] += hidden[j] * weightHO[i][j];
            }
        }
        output = tambahbias(output);
        output = activate(output);
        std::vector<double> errorO(numOutput);
        errorO = hitungError(output, target);
        std::vector<double> gradO(numOutput);
        gradO = derivation(output);
        for (int i = 0; i < numOutput; ++i) {
            gradO[i] = gradO[i] * errorO[i] * 0.1;
        }
        for (int i = 0; i < numOutput; ++i) {
            for (int j = 0; j < numHidden; ++j) {
                weightHO[i][j] += (gradO[i] * hidden[j]);
            }
        }
        std::vector<double> gradH(numHidden);
        std::vector<double> derH(numHidden);
        derH = derivation(hidden);
        for (int i = 0; i < numHidden; ++i) {
            for (int j = 0; j < numOutput; ++j) {
                gradH[i] = gradO[j] * weightHO[j][i];
            }
            gradH[i] = gradH[i] * derH[i] * 0.1;
        }
        for (int i = 0; i < numHidden; ++i) {
            for (int j = 0; j < numInput; ++j) {
                weightIH[i][j] += (gradH[i] * a[j]);
            }
        }


    }

You're copying all your std::vector s into functions:

void NeuralNetwork::train(std::vector<double> a, std::vector<double> target) 

use references instead:

void NeuralNetwork::train(const std::vector<double>& a, const std::vector<double>& target)

Copying a vector is an O(n) operation in both space and time, using a reference is O(1) in both.

A const std::vector reference can't be modified, when you're copying the vector in and out again after modifying it:

std::vector<double> NeuralNetwork::derivation(std::vector<double> a)

use a non-const reference instead:

void NeuralNetwork::derivation(std::vector<double>& a)

事实证明我只是一个白痴,谁不知道调试/发布,让这个程序发布只是解决问题,谢谢大家的帮助

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM