commit 88df2413d569e10c0d1f7dbef0d410c21417def7
parent 70983f35d5e1f16bbbb24e8bd782ec07b485b389
Author: David Freifeld <freifeld.david@gmail.com>
Date: Wed, 24 Jun 2020 23:25:39 -0700
Attempts at backprop fixes
Diffstat:
2 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/bpnn.cpp b/bpnn.cpp
@@ -11,10 +11,10 @@ Layer::Layer(float* vals, int batch_sz, int nodes)
(*contents)((int)i / nodes,i%nodes) = vals[i];
(*dZ)((int)i / nodes,i%nodes) = 0;
}
- bias = new Eigen::MatrixXd (1, nodes);
+ bias = new Eigen::MatrixXd (batch_sz, nodes);
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < batch_sz; j++) {
- (*bias)(j,i) = 0.001;
+ (*bias)(j, i) = 0.001;
}
}
}
@@ -28,9 +28,11 @@ Layer::Layer(int batch_sz, int nodes)
(*contents)((int)i / nodes,i%nodes) = 0;
(*dZ)((int)i / nodes,i%nodes) = 0;
}
- bias = new Eigen::MatrixXd (1, nodes);
+ bias = new Eigen::MatrixXd (batch_sz, nodes);
for (int i = 0; i < nodes; i++) {
- (*bias)(0,i) = 0.001;
+ for (int j = 0; j < batch_sz; j++) {
+ (*bias)(j, i) = 0.001;
+ }
}
dZ = new Eigen::MatrixXd (batch_sz, nodes);
}
@@ -155,21 +157,31 @@ float Network::accuracy()
void Network::backpropagate()
{
+ printf("Entering?\n");
std::vector<Eigen::MatrixXd> gradients;
std::vector<Eigen::MatrixXd> deltas;
+ // std::cout << *labels << "\n" << *layers[length-1].contents << "\n\n\n";
Eigen::MatrixXd error = ((*layers[length-1].contents) - (*labels));
+ // std::cout << error << "\n" << *layers[length-1].dZ << "\n\n\n";
gradients.push_back(error.cwiseProduct(*layers[length-1].dZ));
deltas.push_back((*layers[length-2].contents).transpose() * gradients[0]);
int counter = 1;
for (int i = length-2; i >= 1; i--) {
gradients.push_back((gradients[counter-1] * layers[i].weights->transpose()));
+ std::cout << gradients[counter] << "\n" << *layers[i].dZ << "\n?\n\n";
deltas.push_back((gradients[counter].cwiseProduct(*layers[i].dZ)));
+ printf("Test?\n");
counter++;
}
for (int i = 1; i < gradients.size(); i++) {
Eigen::MatrixXd gradient = gradients[i];
- *layers[length-2-i].weights -= learning_rate * (deltas[i] * *layers[i-1].contents).transpose());
- *layers[length-2-i].bias -= learning_rate * (deltas[i]);
+ printf("Test2?\n");
+ std::cout << deltas[i] << "\n\n" << layers[i-1].contents->transpose() << " " << i-1 << " " <<length-2-i <<"\n";
+ *layers[length-2-i].weights -= learning_rate * (layers[i-1].contents->transpose()*deltas[i]);
+ printf("Test2.5?\n");
+ std::cout << deltas[i] << "\n\n" << *layers[length-2-i].bias << "(layer "<< length-2-i << " cuz " << length << " - 2 - " << i << ")\n";
+ // *layers[length-2-i].bias -= learning_rate * (deltas[i]);
+ printf("Test3?\n");
}
}
diff --git a/example.cpp b/example.cpp
@@ -6,9 +6,10 @@ int main()
Network net ("./extra.txt", 4, 1, 1, 5, 10, 1);
net.set_activation(0, "linear");
net.set_activation(1, "sigmoid");
- net.set_activation(2, "resig");
+ net.set_activation(2, "sigmoid");
+ net.set_activation(3, "resig");
// net.list_net();
- net.train(50);
+ net.train(1);
// net.list_net();
//char line[1024];
//net.stream->getline(line, 1024);