提交 04ad9b6b 编写于 作者: Y Yu Yang

Add Scope Parent & Local section

上级 acf0b75e
...@@ -17,11 +17,34 @@ ...@@ -17,11 +17,34 @@
* 每一个Scope析构的时候,会同时析构variable * 每一个Scope析构的时候,会同时析构variable
* 只能通过Scope创建Vairable。 * 只能通过Scope创建Vairable。
* 只能通过Scope获取Variable。 * 只能通过Scope获取Variable。
## Scope 可以被继承或者叫共享
解释下Scope如何被共享,如何查找Variable的算法。 ## Parent scope and local scope
* Scope永远从本地寻找Variable,找不到会从他的父亲Scope寻找Variable
* 嵌套深度不做要求。 Just like [scope](https://en.wikipedia.org/wiki/Scope_(computer_science)) in programming languages, `Scope` in the neural network also can be local. There are two attributes about local scope.
* We can create local variables in a local scope, and when that local scope are destroyed, all local variables should also be destroyed.
* Variables in a parent scope can be retrieved from that parent scope's local scope, i.e., when user get a variable from a scope, it will search this variable in current scope firstly. If there is no such variable in local scope, `scope` will keep searching from its parent, until the variable is found or there is no parent.
```cpp
class Scope {
public:
Scope(const std::shared_ptr<Scope>& scope): parent_(scope) {}
Variable* Get(const std::string& name) const {
Variable* var = GetVarLocally(name);
if (var != nullptr) {
return var;
} else if (parent_ != nullptr) {
return parent_->Get(name);
} else {
return nullptr;
}
}
private:
std::shared_ptr<Scope> parent_ {nullptr};
};
```
# 接口实现 # 接口实现
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册