autograd实现

思路:
步骤一:从某个叶节点开始,将其压入队尾
步骤二:弹出队首节点作为当前节点。先求出当前节点对于各个父节点的梯度,父节点加上这个梯度之后出度也随之减一,当某个父节点出度为零时,将其加入队列中。重复步骤二。

std::vector <std::vector <double>> nn::graph::compute_gradients (nn::var::iterator tar, const std::vector <nn::var::iterator> &var_list) {
	var *target = tar.reference;
	auto outdegrees = find_outdegrees (target);
	std::unordered_map <var*, std::vector <double>> gradients;
	std::queue <var*> q;

	gradients[target] = {1.0};
	q.push (target);

	while (q.size()) {
		auto node = q.front ();
		q.pop ();

		if (node -> operation == nullptr)
			continue;
		auto input_gradients = node -> operation -> grad (gradients[node]);


		for (size_t i = 0; i < input_gradients.size(); i++) {
			var *in_node = (node -> inputs)[i];
			add_to_vector (gradients[in_node], input_gradients[i]);
			outdegrees[in_node]--;
			if (outdegrees[in_node] == 0)
				q.push (in_node);
		}
	}
	std::vector <std::vector <double>> result;


	for (auto &i : var_list)
		result.push_back (gradients[i.reference]);
	clear ();
	return result;
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页