My project involves using the same data. In my C++ code, it takes 17 seconds to train 100 data points, while in the JavaScript code from this specific project
https://github.com/CodingTrain/Toy-Neural-Network-JS, it only takes about 10 seconds to train 2400 data points. I really need some help figuring out what's wrong because I need to finish my undergraduate thesis.
I've already built two projects, and one of them (this one) is a neural network in C++ based on that JavaScript code (kind of), but it's still producing 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]);
}
}
}