我正在尝试在 C++ 中进行反向模式自动微分。
我想出的想法是,对一个或两个其他变量进行操作的每个变量都将把梯度保存在一个向量中。
这是代码:
class Var {
private:
double value;
char character;
std::vector<std::pair<double, const Var*> > children;
public:
Var(const double& _value=0, const char& _character='_') : value(_value), character(_character) {};
void set_character(const char& character){ this->character = character; }
// computes the derivative of the current object with respect to 'var'
double gradient(Var* var) const{
if(this==var){
return 1.0;
}
double sum=0.0;
for(auto& pair : children){
// std::cout << "(" << this->character << " -> " << pair.second->character << ", " << this << " -> " << pair.second << ", weight=" << pair.first << ")" << std::endl;
sum += pair.first*pair.second->gradient(var);
}
return sum;
}
friend Var operator+(const Var& l, const Var& r){
Var result(l.value+r.value);
result.children.push_back(std::make_pair(1.0, &l));
result.children.push_back(std::make_pair(1.0, &r));
return result;
}
friend Var operator*(const Var& l, const Var& r){
Var result(l.value*r.value);
result.children.push_back(std::make_pair(r.value, &l));
result.children.push_back(std::make_pair(l.value, &r));
return result;
}
friend std::ostream& operator<<(std::ostream& os, const Var& var){
os << var.value;
return os;
}
};
我试图运行这样的代码:
int main(int argc, char const *argv[]) {
Var x(5,'x'), y(6,'y'), z(7,'z');
Var k = z + x*y;
k.set_character('k');
std::cout << "k = " << k << std::endl;
std::cout << "∂k/∂x = " << k.gradient(&x) << std::endl;
std::cout << "∂k/∂y = " << k.gradient(&y) << std::endl;
std::cout << "∂k/∂z = " << k.gradient(&z) << std::endl;
return 0;
}
应该构建的计算图如下:
x(5) y(6) z(7)
\ / /
∂w/∂x=y \ / ∂w/∂y=x /
\ / /
w=x*y /
\ / ∂k/∂z=1
\ /
∂k/∂w=1 \ /
\_________/
|
k=w+z
然后,例如,如果我想计算∂k/∂x
,我必须乘以沿边缘的梯度,并对每个边缘的结果求和。这是由 递归完成的double gradient(Var* var) const
。所以我有∂k/∂x = ∂k/∂w * ∂w/∂x + ∂k/∂z * ∂z/∂x
。
问题
如果我有像x*y
这里这样的中间计算,就会出现问题。此处未注释时std::cout
为输出:
k = 37
(k -> z, 0x7ffeb3345740 -> 0x7ffeb3345710, weight=1)
(k -> _, 0x7ffeb3345740 -> 0x7ffeb3345770, weight=1)
(_ -> x, 0x7ffeb3345770 -> 0x7ffeb33456b0, weight=0)
(_ -> y, 0x7ffeb3345770 -> 0x7ffeb33456e0, weight=5)
∂k/∂x = 0
(k -> z, 0x7ffeb3345740 -> 0x7ffeb3345710, weight=1)
(k -> _, 0x7ffeb3345740 -> 0x7ffeb3345770, weight=1)
(_ -> x, 0x7ffeb3345770 -> 0x7ffeb33456b0, weight=0)
(_ -> y, 0x7ffeb3345770 -> 0x7ffeb33456e0, weight=5)
∂k/∂y = 5
(k -> z, 0x7ffeb3345740 -> 0x7ffeb3345710, weight=1)
(k -> _, 0x7ffeb3345740 -> 0x7ffeb3345770, weight=1)
(_ -> x, 0x7ffeb3345770 -> 0x7ffeb33456b0, weight=0)
(_ -> y, 0x7ffeb3345770 -> 0x7ffeb33456e0, weight=5)
∂k/∂z = 1
它打印哪个变量连接到哪个变量,然后是它们的地址,以及连接的权重(应该是梯度)。
问题 weight=0
出在中间变量和中间变量之间x
,中间变量保存了x*y
(我w
在图中表示)的结果。我不知道为什么这个是零而不是连接到的另一个重量y
。
我注意到的另一件事是,如果您operator*
像这样切换线路:
result.children.push_back(std::make_pair(1.0, &r));
result.children.push_back(std::make_pair(1.0, &l));
然后是y
取消的连接。
提前感谢您的帮助。