diff --git a/csrc/layers/convolutional_layer.h b/csrc/layers/convolutional_layer.h index fe59342..90eb544 100644 --- a/csrc/layers/convolutional_layer.h +++ b/csrc/layers/convolutional_layer.h @@ -118,21 +118,21 @@ class Conv2D : public Layer { for (int oh = 0; oh < output_height; ++oh) { for (int ow = 0; ow < output_width; ++ow) { // Compute the dot product of the kernel and the input patch - double result = 0.0; + std::shared_ptr result = std::make_shared(0.0); for (int ic = 0; ic < in_channels; ++ic) { for (int kh = 0; kh < kernel_size; ++kh) { for (int kw = 0; kw < kernel_size; ++kw) { int ih = oh * stride + kh - padding; int iw = ow * stride + kw - padding; if (ih >= 0 && ih < height && iw >= 0 && iw < width) { - result += input->get({ic, ih, iw})->data * - weights->get({oc, ic, kh, kw})->data; + result = result->add(input->get({ic, ih, iw})->mul( + weights->get({oc, ic, kh, kw}))); } } } } - result += bias->get(oc)->data; // Add bias - output->set({oc, oh, ow}, std::make_shared(result)); + result = result->add(bias->get(oc)); // Add bias + output->set({oc, oh, ow}, result); } } } diff --git a/csrc/main.cc b/csrc/main.cc index 29c6e26..881df99 100644 --- a/csrc/main.cc +++ b/csrc/main.cc @@ -117,8 +117,8 @@ PYBIND11_MODULE(_core, m) { .def_readonly("minIdx", &Tensor::minIdx) .def_readonly("vals", &Tensor::v) .def("normalize_idx", &Tensor::normalize_idx) - .def("backward", &Tensor::backward) .def("zero_grad", &Tensor::zero_grad) + .def("reshape", &Tensor::reshape) .def("__add__", &Tensor::add) .def("__truediv__", &Tensor::div) .def("matmul", &Tensor::matmul) diff --git a/csrc/tensor.h b/csrc/tensor.h index 41c356f..9a570af 100644 --- a/csrc/tensor.h +++ b/csrc/tensor.h @@ -23,6 +23,12 @@ class Tensor : public std::enable_shared_from_this { this->compute_stride(); } + ~Tensor(){ + this->strides.clear(); + this->shape.clear(); + this->v.clear(); + } + void compute_stride() { this->strides.clear(); this->strides.resize(this->shape.size()); @@ -39,6 +45,24 @@ class Tensor : public std::enable_shared_from_this { this->maxIdx--; // 1 less } + void reshape(std::vector new_shape){ + int total_ele = 1; + for(auto &e:new_shape){ + total_ele*=e; + } + if(total_ele!=this->maxIdx +1 ){ + throw std::runtime_error("New shape must be able to contain (" + + std::to_string(this->maxIdx+1) + + "), but new shape can handle: " + + std::to_string(total_ele) + + " elements." + ); + } + + this->shape = new_shape; + this->compute_stride(); + } + std::string tensor_shape_str() { std::string shape_str = "("; for (auto& e : this->shape) { @@ -113,12 +137,6 @@ class Tensor : public std::enable_shared_from_this { } } - void backward() { - for (auto& e : this->v) { - e->backward(); - } - } - void remove_redundant_rows(std::shared_ptr t) { // remove redundant rows(1) std::vector new_shape = {}; diff --git a/csrc/value.cc b/csrc/value.cc index 51de32b..71bcefe 100644 --- a/csrc/value.cc +++ b/csrc/value.cc @@ -46,6 +46,8 @@ void Value::backward() { for (int i = int(topo_list.size()) - 1; i >= 0; i--) { // std::cout << "i: " << i << "; node: " << topo_list[i]->printMe() << "\n"; topo_list[i]->executeBackWardMethod(); + topo_list[i]->clearBackwardMethod(); + topo_list[i]->_prev.clear(); } } diff --git a/csrc/value.h b/csrc/value.h index 7c0f0ca..b15c527 100644 --- a/csrc/value.h +++ b/csrc/value.h @@ -32,19 +32,28 @@ class Value : public std::enable_shared_from_this { Value(double data) : data(data) {} Value(double data, std::unordered_set> _prev, char _op) : data(data), _prev(std::move(_prev)), _op(_op) {} + + ~Value(){ + this->_prev.clear(); + this->clearBackwardMethod(); + } // Setter to assign a new function void setBackWardMethod(std::function func) { - backward_ = func; + this->backward_ = func; } - // Method to execute the private method + // Method to execute the private method backward_ void executeBackWardMethod() { - if (backward_) { - backward_(); + if (this->backward_) { + this->backward_(); } } + void clearBackwardMethod() { + this->backward_ = nullptr; + } + void backward(); std::string printMe() {