From a918e187f72925864ad422a8b8e9a6e4cdfe26bb Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 23 Jun 2024 12:34:44 -0700 Subject: [PATCH] and example --- examples/and.cpp | 70 +++++++++++++++++++++--------------------------- scalar.h | 14 ++++++---- 2 files changed, 39 insertions(+), 45 deletions(-) diff --git a/examples/and.cpp b/examples/and.cpp index 5ecd594..ef763e2 100644 --- a/examples/and.cpp +++ b/examples/and.cpp @@ -4,65 +4,55 @@ int main() { - // 4x2 dataset maxtrix shared pointer - std::shared_ptr> X[4][2]; - X[0][0] = Scalar::make(0); - X[0][1] = Scalar::make(0); - X[1][0] = Scalar::make(0); - X[1][1] = Scalar::make(1); - X[2][0] = Scalar::make(1); - X[2][1] = Scalar::make(0); - X[3][0] = Scalar::make(1); - X[3][1] = Scalar::make(1); - - // labels shared pointer - std::shared_ptr> Y[4]; - Y[0] = Scalar::make(0); - Y[1] = Scalar::make(0); - Y[2] = Scalar::make(0); - Y[3] = Scalar::make(1); - // weights shared pointer std::shared_ptr> w1 = Scalar::make(-.09); std::shared_ptr> w2 = Scalar::make(.02); std::shared_ptr> b = Scalar::make(0); + int num_epochs = 10000, num_samples = 4; - float learning_rate = 0.001; + float learning_rate = 0.0001; + auto& graph = ComputationalGraph::get_instance(); for (int i = 0; i < num_epochs; i++) { - std::shared_ptr> loss = std::make_shared>(0); + graph.clear(); + + std::shared_ptr> X[4][2]; + X[0][0] = Scalar::make(0); + X[0][1] = Scalar::make(0); + X[1][0] = Scalar::make(0); + X[1][1] = Scalar::make(1); + X[2][0] = Scalar::make(1); + X[2][1] = Scalar::make(0); + X[3][0] = Scalar::make(1); + X[3][1] = Scalar::make(1); + + // labels shared pointer + std::shared_ptr> Y[4]; + Y[0] = Scalar::make(0); + Y[1] = Scalar::make(0); + Y[2] = Scalar::make(0); + Y[3] = Scalar::make(1); + + std::shared_ptr> loss = std::make_shared>(0); for (int j = 0; j < num_samples; j++) { // forward - auto z = w1 * X[j][0]; - loss = z; - - break; + auto z = w1 * X[j][0] + w2 * X[j][1] + b; + auto a = sigmoid(z); + loss = cross_entropy(Y[j], a) + loss; } - std::cout << "Epoch: " << i << " Loss: " << loss->value << std::endl; loss->backward(); - - // print updating weights // update weights w1->value = w1->value - learning_rate * w1->grad; w2->value = w2->value - learning_rate * w2->grad; b->value = b->value - learning_rate * b->grad; - // print grads - // printing - - std::cout << "Printing grads:\n"; - - std::cout << "w1 grad: " << w1->grad << std::endl; - std::cout << "w2 grad: " << w2->grad << std::endl; - std::cout << "b grad: " << b->grad << std::endl; - - // reset gradients - w1->grad = 0; - w2->grad = 0; - b->grad = 0; + // print epoch loss + if (i % 1000 == 0) { + std::cout << "Epoch: " << i << " Loss: " << loss->value << std::endl; + } } return 0; diff --git a/scalar.h b/scalar.h index 8205770..4c8ccc5 100644 --- a/scalar.h +++ b/scalar.h @@ -28,13 +28,17 @@ class ComputationalGraph { return instance; } - void backward() { - for (auto node : nodes) { if (node->in_degrees == 0) { node->backward(); } } - } - void add_node(std::shared_ptr> node) { nodes.insert(node); } - void clear() { nodes.clear(); } + void clear() { + for (auto node : nodes) { + node->grad = 0; + node->in_degrees = 0; + node->children.clear(); + } + + nodes.clear(); + } };