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]);
}
}
}