autograd實現

curry dog發表於2020-10-30

思路:
步驟一:從某個葉節點開始,將其壓入隊尾
步驟二:彈出隊首節點作為當前節點。先求出當前節點對於各個父節點的梯度,父節點加上這個梯度之後出度也隨之減一,當某個父節點出度為零時,將其加入佇列中。重複步驟二。

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;
}

相關文章