PaddlePaddle divides the description of neural network computation graph into two stages: compile time and runtime.
PaddlePaddle use proto message to describe compile time graph for
1. Computation graph should be able to be saved to a file.
1. In distributed training, the graph will be serialized and send to multiple workers.
The computation graph is constructed by Data Node and Operation Node. The concept to represent them is in the table below.
| |compile time|runtime|
|---|---|---|
|Data|VarDesc(proto)|Variable(cpp)|
|Operation|OpDesc(proto)|Operator(cpp)|
## Definition of VarDesc
A VarDesc should have a name and value, in PaddlePaddle, the value will always be a tensor. Since we use LoDTensor most of the time. We add a LoDTesnorDesc to represent it.
```proto
message VarDesc {
required string name = 1;
optional LoDTensorDesc lod_tensor = 2;
}
```
## Definition of LodTensorDesc
```proto
enum DataType {
BOOL = 0;
INT16 = 1;
INT32 = 2;
INT64 = 3;
FP16 = 4;
FP32 = 5;
FP64 = 6;
}
message LoDTensorDesc {
required DataType data_type = 1;
repeated int32 dims = 2; // [UNK, 640, 480] is saved as [-1, 640, 480]
optional int32 lod_level = 3 [default=0];
}
```
## Definition of Variable in Python
In Python API, layer will take Variable as Input, and return Variable as Output. There should be a class `Variable` in python to help create and manage Variable.
```python
image = Variable(dims=[-1, 640, 480])
# fc1 and fc2 are both Variable
fc1 = layer.fc(input=image, output_size=10)
fc2 = layer.fc(input=fc1, output_size=20)
```
### what should class `Variable` Have
1. `name`.a name of string type is used to mark the value of the Variable.
1. `initializer`. Since our Tensor does not have value. we will always use some Operator to fullfill it when run. So we should have a initialize method to help add the init operator.
1. `operator`. Variable should record which operator produce itself. The reaon is:
- we use pd.eval(targets=[var1, var2]) to run the related ops to get the value of var1 and var2. var.op is used to trace the dependency of the current variable.
In PaddlePaddle, we use Block to describe Computation Graph, so in the code we will use Block but not Graph.
```python
import VarDesc
import LoDTensorDesc
import framework
def AddInitialOperator(variable, initializer):
# add an initialize Operator to block to init this Variable
<liclass="toctree-l2"><aclass="reference internal"href="../getstarted/build_and_install/index_en.html">Install and Build</a><ul>
<liclass="toctree-l3"><aclass="reference internal"href="../getstarted/build_and_install/docker_install_en.html">PaddlePaddle in Docker Containers</a></li>
<liclass="toctree-l3"><aclass="reference internal"href="../getstarted/build_and_install/build_from_source_en.html">Installing from Sources</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/usage/k8s/k8s_en.html">Paddle On Kubernetes</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/usage/k8s/k8s_aws_en.html">Distributed PaddlePaddle Training on AWS with Kubernetes</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/dev/build_en.html">Build PaddlePaddle from Source Code and Run Unit Test</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/dev/new_layer_en.html">Write New Layers</a></li>
<spanid="background"></span><h1>Background<aclass="headerlink"href="#background"title="Permalink to this headline">¶</a></h1>
<p>PaddlePaddle divides the description of neural network computation graph into two stages: compile time and runtime.</p>
<p>PaddlePaddle use proto message to describe compile time graph for</p>
<olclass="simple">
<li>Computation graph should be able to be saved to a file.</li>
<li>In distributed training, the graph will be serialized and send to multiple workers.</li>
</ol>
<p>The computation graph is constructed by Data Node and Operation Node. The concept to represent them is in the table below.</p>
<p>| |compile time|runtime|
|—|—|—|
|Data|VarDesc(proto)|Variable(cpp)|
|Operation|OpDesc(proto)|Operator(cpp)|</p>
</div>
<divclass="section"id="definition-of-vardesc">
<spanid="definition-of-vardesc"></span><h1>Definition of VarDesc<aclass="headerlink"href="#definition-of-vardesc"title="Permalink to this headline">¶</a></h1>
<p>A VarDesc should have a name and value, in PaddlePaddle, the value will always be a tensor. Since we use LoDTensor most of the time. We add a LoDTesnorDesc to represent it.</p>
<spanid="definition-of-lodtensordesc"></span><h1>Definition of LodTensorDesc<aclass="headerlink"href="#definition-of-lodtensordesc"title="Permalink to this headline">¶</a></h1>
<spanclass="k">repeated</span><spanclass="kt">int32</span><spanclass="na">dims</span><spanclass="o">=</span><spanclass="mi">2</span><spanclass="p">;</span><spanclass="c1">// [UNK, 640, 480] is saved as [-1, 640, 480]</span>
<spanid="definition-of-variable-in-python"></span><h1>Definition of Variable in Python<aclass="headerlink"href="#definition-of-variable-in-python"title="Permalink to this headline">¶</a></h1>
<p>In Python API, layer will take Variable as Input, and return Variable as Output. There should be a class <codeclass="docutils literal"><spanclass="pre">Variable</span></code> in python to help create and manage Variable.</p>
<spanid="what-should-class-variable-have"></span><h2>what should class <codeclass="docutils literal"><spanclass="pre">Variable</span></code> Have<aclass="headerlink"href="#what-should-class-variable-have"title="Permalink to this headline">¶</a></h2>
<olclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">name</span></code>.a name of string type is used to mark the value of the Variable.</li>
<li><codeclass="docutils literal"><spanclass="pre">initializer</span></code>. Since our Tensor does not have value. we will always use some Operator to fullfill it when run. So we should have a initialize method to help add the init operator.</li>
<li><codeclass="docutils literal"><spanclass="pre">operator</span></code>. Variable should record which operator produce itself. The reaon is:</li>
</ol>
<ulclass="simple">
<li>we use pd.eval(targets=[var1, var2]) to run the related ops to get the value of var1 and var2. var.op is used to trace the dependency of the current variable.</li>
</ul>
<p>In PaddlePaddle, we use Block to describe Computation Graph, so in the code we will use Block but not Graph.</p>
<spanclass="n">y</span><spanclass="o">=</span><spanclass="n">operator</span><spanclass="o">.</span><spanclass="n">fc</span><spanclass="p">(</span><spanclass="n">X</span><spanclass="p">,</span><spanclass="n">W</span><spanclass="p">,</span><spanclass="n">b</span><spanclass="p">,</span><spanclass="n">output</span><spanclass="o">=</span><spanclass="n">out</span><spanclass="p">)</span><spanclass="c1"># fc will put fc op input into out</span>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.
PaddlePaddle divides the description of neural network computation graph into two stages: compile time and runtime.
PaddlePaddle use proto message to describe compile time graph for
1. Computation graph should be able to be saved to a file.
1. In distributed training, the graph will be serialized and send to multiple workers.
The computation graph is constructed by Data Node and Operation Node. The concept to represent them is in the table below.
| |compile time|runtime|
|---|---|---|
|Data|VarDesc(proto)|Variable(cpp)|
|Operation|OpDesc(proto)|Operator(cpp)|
## Definition of VarDesc
A VarDesc should have a name and value, in PaddlePaddle, the value will always be a tensor. Since we use LoDTensor most of the time. We add a LoDTesnorDesc to represent it.
```proto
message VarDesc {
required string name = 1;
optional LoDTensorDesc lod_tensor = 2;
}
```
## Definition of LodTensorDesc
```proto
enum DataType {
BOOL = 0;
INT16 = 1;
INT32 = 2;
INT64 = 3;
FP16 = 4;
FP32 = 5;
FP64 = 6;
}
message LoDTensorDesc {
required DataType data_type = 1;
repeated int32 dims = 2; // [UNK, 640, 480] is saved as [-1, 640, 480]
optional int32 lod_level = 3 [default=0];
}
```
## Definition of Variable in Python
In Python API, layer will take Variable as Input, and return Variable as Output. There should be a class `Variable` in python to help create and manage Variable.
```python
image = Variable(dims=[-1, 640, 480])
# fc1 and fc2 are both Variable
fc1 = layer.fc(input=image, output_size=10)
fc2 = layer.fc(input=fc1, output_size=20)
```
### what should class `Variable` Have
1. `name`.a name of string type is used to mark the value of the Variable.
1. `initializer`. Since our Tensor does not have value. we will always use some Operator to fullfill it when run. So we should have a initialize method to help add the init operator.
1. `operator`. Variable should record which operator produce itself. The reaon is:
- we use pd.eval(targets=[var1, var2]) to run the related ops to get the value of var1 and var2. var.op is used to trace the dependency of the current variable.
In PaddlePaddle, we use Block to describe Computation Graph, so in the code we will use Block but not Graph.
```python
import VarDesc
import LoDTensorDesc
import framework
def AddInitialOperator(variable, initializer):
# add an initialize Operator to block to init this Variable
<p>PaddlePaddle divides the description of neural network computation graph into two stages: compile time and runtime.</p>
<p>PaddlePaddle use proto message to describe compile time graph for</p>
<olclass="simple">
<li>Computation graph should be able to be saved to a file.</li>
<li>In distributed training, the graph will be serialized and send to multiple workers.</li>
</ol>
<p>The computation graph is constructed by Data Node and Operation Node. The concept to represent them is in the table below.</p>
<p>| |compile time|runtime|
|—|—|—|
|Data|VarDesc(proto)|Variable(cpp)|
|Operation|OpDesc(proto)|Operator(cpp)|</p>
</div>
<divclass="section"id="definition-of-vardesc">
<spanid="definition-of-vardesc"></span><h1>Definition of VarDesc<aclass="headerlink"href="#definition-of-vardesc"title="永久链接至标题">¶</a></h1>
<p>A VarDesc should have a name and value, in PaddlePaddle, the value will always be a tensor. Since we use LoDTensor most of the time. We add a LoDTesnorDesc to represent it.</p>
<spanid="definition-of-lodtensordesc"></span><h1>Definition of LodTensorDesc<aclass="headerlink"href="#definition-of-lodtensordesc"title="永久链接至标题">¶</a></h1>
<spanclass="k">repeated</span><spanclass="kt">int32</span><spanclass="na">dims</span><spanclass="o">=</span><spanclass="mi">2</span><spanclass="p">;</span><spanclass="c1">// [UNK, 640, 480] is saved as [-1, 640, 480]</span>
<spanid="definition-of-variable-in-python"></span><h1>Definition of Variable in Python<aclass="headerlink"href="#definition-of-variable-in-python"title="永久链接至标题">¶</a></h1>
<p>In Python API, layer will take Variable as Input, and return Variable as Output. There should be a class <codeclass="docutils literal"><spanclass="pre">Variable</span></code> in python to help create and manage Variable.</p>
<spanid="what-should-class-variable-have"></span><h2>what should class <codeclass="docutils literal"><spanclass="pre">Variable</span></code> Have<aclass="headerlink"href="#what-should-class-variable-have"title="永久链接至标题">¶</a></h2>
<olclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">name</span></code>.a name of string type is used to mark the value of the Variable.</li>
<li><codeclass="docutils literal"><spanclass="pre">initializer</span></code>. Since our Tensor does not have value. we will always use some Operator to fullfill it when run. So we should have a initialize method to help add the init operator.</li>
<li><codeclass="docutils literal"><spanclass="pre">operator</span></code>. Variable should record which operator produce itself. The reaon is:</li>
</ol>
<ulclass="simple">
<li>we use pd.eval(targets=[var1, var2]) to run the related ops to get the value of var1 and var2. var.op is used to trace the dependency of the current variable.</li>
</ul>
<p>In PaddlePaddle, we use Block to describe Computation Graph, so in the code we will use Block but not Graph.</p>
<spanclass="n">y</span><spanclass="o">=</span><spanclass="n">operator</span><spanclass="o">.</span><spanclass="n">fc</span><spanclass="p">(</span><spanclass="n">X</span><spanclass="p">,</span><spanclass="n">W</span><spanclass="p">,</span><spanclass="n">b</span><spanclass="p">,</span><spanclass="n">output</span><spanclass="o">=</span><spanclass="n">out</span><spanclass="p">)</span><spanclass="c1"># fc will put fc op input into out</span>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.