10-zin / cpp-micrograd Goto Github PK
View Code? Open in Web Editor NEWA c/c++ implementation of micrograd: a tiny autograd engine with neural net on top.
License: MIT License
A c/c++ implementation of micrograd: a tiny autograd engine with neural net on top.
License: MIT License
Following snippet highlights the issue:
int main() {
{
auto a = std::make_shared<Value>(1);
auto b = std::make_shared<Value>(2);
auto c = a + b;
}
// a, b and c are not destroyed ...
}
Issue seems to be with the captured variables of _backward
lambda:
std::shared_ptr<Value> Value::operator+(const std::shared_ptr<Value>& other) {
auto out_prev = std::unordered_set<std::shared_ptr<Value>>{shared_from_this(), other};
auto out = std::make_shared<Value>(data + other->data, out_prev, "+");
out->_backward = [this, other, out] { // We're capturing out, hence the lambda shares ownership of out!
grad += out->grad;
other->grad += out->grad;
};
return out;
}
This leads to a situation where the lambda inside out
shares ownership of out
. So, _backward
is not destroyed until until out
is destroyed and out
is not destroyed until _backward
is destroyed, hence nothing is destroyed.
To fix, the lambda should only take weak ownership.
std::shared_ptr<Value> Value::operator+(const std::shared_ptr<Value>& other) {
auto out_prev = std::unordered_set<std::shared_ptr<Value>>{shared_from_this(), other};
auto out = std::make_shared<Value>(data + other->data, out_prev, "+");
Value* weak_ref = out.get();
out->_backward = [this, other, weak_ref] {
grad += weak_ref->grad;
other->grad += weak_ref->grad;
};
return out;
}
more details are following:
std::shared_ptr value1 = std::make_shared(2.5);
std::shared_ptr value2 = std::make_shared(3.7);
std::shared_ptr value3 = std::make_shared(-3.0);
std::shared_ptr value4 = std::make_shared(1.7);
// Perform addition using the operator+
std::shared_ptr<Value> value1_2 = value1 + (value2 * 3.0); # error
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.