Neural network framework often provides symbolic API for users to write network topology conveniently. This doc manily focus on symbolic API in most popular neural network frameworks, and try to find out how to parse symbolic configuration to a portable file, such as protobuf or json.
### Mxnet
The core concept of symbolic API is `Symbol`. Mxnet implements `Symbol` class in C++, and export to Python using C-API. Please refer to the comments in Mxnet:
`Symbol` is help class used to represent the operator node in Graph.
`Symbol` acts as an interface for building graphs from different components like Variable, Functor and Group. `Symbol` is also exported to python front-end (while Graph is not) to enable quick test and deployment. Conceptually, symbol is the final operation of a graph and thus including all the information required (the graph) to evaluate its output value.
A simple network topology wrote by Symbol is as follows:
mlp = mx.symbol.SoftmaxOutput(data = fc3, name = 'softmax')
return mlp
```
Varible here is actually a Symbol. Every basic Symbol will correspond to one Node, and every Node has its own NodeAttr. There is a op field in NodeAttr class, when a Symbol represents Variable(often input data), the op field is null.
Symbol contains a data member, std::vector<NodeEntry> outputs, and NodeEntry cantains a poniter to Node. We can follow the Node pointer to get all the Graph.
The core concept of symbolic API is `Tensor`. Tensorflow defines `Tensor` in Python. Please refer to the comments in TensorFlow:
A `Tensor` is a symbolic handle to one of the outputs of an `Operation`. It does not hold the values of that operation's output, but instead provides a means of computing those values in a TensorFlow [Session](https://www.tensorflow.org/api_docs/python/tf/Session).
A simple example is as follows:
```python
# Build a dataflow graph.
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d)
# Construct a `Session` to execute the graph.
sess = tf.Session()
# Execute the graph and store the value that `e` represents in `result`.
result = sess.run(e)
```
The main method of `Tensor` is as follows:
```python
@property
def op(self):
"""The `Operation` that produces this tensor as an output."""
return self._op
@property
def dtype(self):
"""The `DType` of elements in this tensor."""
return self._dtype
@property
def graph(self):
"""The `Graph` that contains this tensor."""
return self._op.graph
@property
def name(self):
"""The string name of this tensor."""
if not self._op.name:
raise ValueError("Operation was not named: %s" % self._op)
"""The name of the device on which this tensor will be produced, or None."""
return self._op.device
```
Tensor can be taken as target to run by session. Tensor contains all the information of Graph, and tracks data dependency.
Here is a detailed example:
```
>>> import tensorflow as tf
>>> c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
>>> print c.graph
<tensorflow.python.framework.ops.Graph object at 0x10f256d50>
>>> d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
>>> print d.graph
<tensorflow.python.framework.ops.Graph object at 0x10f256d50>
>>> e = tf.matmul(c, d)
>>> print e.graph
<tensorflow.python.framework.ops.Graph object at 0x10f256d50>
```
### Dynet
The core concept of symbolic API is `Expression`, and Dynet defines `Expression` class in C++.
A simple example is as follows:
```cpp
ComputationGraph cg;
Expression W = parameter(cg, pW);
Expression in = input(cg, xs[i]);
Expression label = input(cg, ys[i]);
Expression pred = W * in;
Expression loss = square(pred - label);
```
The input data and parameter are also represented by Expression. Every basci Expression corresponds to a Node. And input data is also a Node.
Expression has a data member ComputationGraph, and ComputationGraph will be modified in users' configuring process. Expression can be a running target, beacuse Expression contains all dependency.
Actually, Symbol/Tensor/Expression in Mxnet/TensorFlow/Dynet are the same level concepts. We use a unified name Expression here, this level concept has following features:
- Users wirte topoloy with symbolic API, and all return value is Expression, including input data and parameter.
- Expression corresponds with a global Graph, and Expression can also be composed.
- Expression tracks all dependency and can be taken as a run target
<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/cluster/cluster_train_en.html#use-cluster-platforms-or-cluster-management-tools">Use cluster platforms or cluster management tools</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="survey-on-graph"></span><h1>Survey on Graph<aclass="headerlink"href="#survey-on-graph"title="Permalink to this headline">¶</a></h1>
<p>Neural network framework often provides symbolic API for users to write network topology conveniently. This doc manily focus on symbolic API in most popular neural network frameworks, and try to find out how to parse symbolic configuration to a portable file, such as protobuf or json.</p>
<divclass="section"id="mxnet">
<spanid="mxnet"></span><h2>Mxnet<aclass="headerlink"href="#mxnet"title="Permalink to this headline">¶</a></h2>
<p>The core concept of symbolic API is <codeclass="docutils literal"><spanclass="pre">Symbol</span></code>. Mxnet implements <codeclass="docutils literal"><spanclass="pre">Symbol</span></code> class in C++, and export to Python using C-API. Please refer to the comments in Mxnet:</p>
<p><codeclass="docutils literal"><spanclass="pre">Symbol</span></code> is help class used to represent the operator node in Graph.
<codeclass="docutils literal"><spanclass="pre">Symbol</span></code> acts as an interface for building graphs from different components like Variable, Functor and Group. <codeclass="docutils literal"><spanclass="pre">Symbol</span></code> is also exported to python front-end (while Graph is not) to enable quick test and deployment. Conceptually, symbol is the final operation of a graph and thus including all the information required (the graph) to evaluate its output value.</p>
<p>A simple network topology wrote by Symbol is as follows:</p>
<p>Varible here is actually a Symbol. Every basic Symbol will correspond to one Node, and every Node has its own NodeAttr. There is a op field in NodeAttr class, when a Symbol represents Variable(often input data), the op field is null.</p>
<p>Symbol contains a data member, std::vector<NodeEntry> outputs, and NodeEntry cantains a poniter to Node. We can follow the Node pointer to get all the Graph.</p>
<spanid="tensorflow"></span><h2>TensorFlow<aclass="headerlink"href="#tensorflow"title="Permalink to this headline">¶</a></h2>
<p>The core concept of symbolic API is <codeclass="docutils literal"><spanclass="pre">Tensor</span></code>. Tensorflow defines <codeclass="docutils literal"><spanclass="pre">Tensor</span></code> in Python. Please refer to the comments in TensorFlow:</p>
<p>A <codeclass="docutils literal"><spanclass="pre">Tensor</span></code> is a symbolic handle to one of the outputs of an <codeclass="docutils literal"><spanclass="pre">Operation</span></code>. It does not hold the values of that operation’s output, but instead provides a means of computing those values in a TensorFlow <aclass="reference external"href="https://www.tensorflow.org/api_docs/python/tf/Session">Session</a>.</p>
<p>A simple example is as follows:</p>
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="c1"># Build a dataflow graph.</span>
<spanclass="k">raise</span><spanclass="ne">ValueError</span><spanclass="p">(</span><spanclass="s2">"Operation was not named: </span><spanclass="si">%s</span><spanclass="s2">"</span><spanclass="o">%</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">_op</span><spanclass="p">)</span>
<spanclass="go"><tensorflow.python.framework.ops.Graph object at 0x10f256d50></span>
</pre></div>
</div>
</div>
<divclass="section"id="dynet">
<spanid="dynet"></span><h2>Dynet<aclass="headerlink"href="#dynet"title="Permalink to this headline">¶</a></h2>
<p>The core concept of symbolic API is <codeclass="docutils literal"><spanclass="pre">Expression</span></code>, and Dynet defines <codeclass="docutils literal"><spanclass="pre">Expression</span></code> class in C++.</p>
<p>The input data and parameter are also represented by Expression. Every basci Expression corresponds to a Node. And input data is also a Node.</p>
<p>Expression has a data member ComputationGraph, and ComputationGraph will be modified in users’ configuring process. Expression can be a running target, beacuse Expression contains all dependency.</p>
<spanid="conclusion"></span><h2>Conclusion<aclass="headerlink"href="#conclusion"title="Permalink to this headline">¶</a></h2>
<p>Actually, Symbol/Tensor/Expression in Mxnet/TensorFlow/Dynet are the same level concepts. We use a unified name Expression here, this level concept has following features:</p>
<ulclass="simple">
<li>Users wirte topoloy with symbolic API, and all return value is Expression, including input data and parameter.</li>
<li>Expression corresponds with a global Graph, and Expression can also be composed.</li>
<li>Expression tracks all dependency and can be taken as a run target</li>
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>.
Neural network framework often provides symbolic API for users to write network topology conveniently. This doc manily focus on symbolic API in most popular neural network frameworks, and try to find out how to parse symbolic configuration to a portable file, such as protobuf or json.
### Mxnet
The core concept of symbolic API is `Symbol`. Mxnet implements `Symbol` class in C++, and export to Python using C-API. Please refer to the comments in Mxnet:
`Symbol` is help class used to represent the operator node in Graph.
`Symbol` acts as an interface for building graphs from different components like Variable, Functor and Group. `Symbol` is also exported to python front-end (while Graph is not) to enable quick test and deployment. Conceptually, symbol is the final operation of a graph and thus including all the information required (the graph) to evaluate its output value.
A simple network topology wrote by Symbol is as follows:
mlp = mx.symbol.SoftmaxOutput(data = fc3, name = 'softmax')
return mlp
```
Varible here is actually a Symbol. Every basic Symbol will correspond to one Node, and every Node has its own NodeAttr. There is a op field in NodeAttr class, when a Symbol represents Variable(often input data), the op field is null.
Symbol contains a data member, std::vector<NodeEntry> outputs, and NodeEntry cantains a poniter to Node. We can follow the Node pointer to get all the Graph.
The core concept of symbolic API is `Tensor`. Tensorflow defines `Tensor` in Python. Please refer to the comments in TensorFlow:
A `Tensor` is a symbolic handle to one of the outputs of an `Operation`. It does not hold the values of that operation's output, but instead provides a means of computing those values in a TensorFlow [Session](https://www.tensorflow.org/api_docs/python/tf/Session).
A simple example is as follows:
```python
# Build a dataflow graph.
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d)
# Construct a `Session` to execute the graph.
sess = tf.Session()
# Execute the graph and store the value that `e` represents in `result`.
result = sess.run(e)
```
The main method of `Tensor` is as follows:
```python
@property
def op(self):
"""The `Operation` that produces this tensor as an output."""
return self._op
@property
def dtype(self):
"""The `DType` of elements in this tensor."""
return self._dtype
@property
def graph(self):
"""The `Graph` that contains this tensor."""
return self._op.graph
@property
def name(self):
"""The string name of this tensor."""
if not self._op.name:
raise ValueError("Operation was not named: %s" % self._op)
"""The name of the device on which this tensor will be produced, or None."""
return self._op.device
```
Tensor can be taken as target to run by session. Tensor contains all the information of Graph, and tracks data dependency.
Here is a detailed example:
```
>>> import tensorflow as tf
>>> c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
>>> print c.graph
<tensorflow.python.framework.ops.Graph object at 0x10f256d50>
>>> d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
>>> print d.graph
<tensorflow.python.framework.ops.Graph object at 0x10f256d50>
>>> e = tf.matmul(c, d)
>>> print e.graph
<tensorflow.python.framework.ops.Graph object at 0x10f256d50>
```
### Dynet
The core concept of symbolic API is `Expression`, and Dynet defines `Expression` class in C++.
A simple example is as follows:
```cpp
ComputationGraph cg;
Expression W = parameter(cg, pW);
Expression in = input(cg, xs[i]);
Expression label = input(cg, ys[i]);
Expression pred = W * in;
Expression loss = square(pred - label);
```
The input data and parameter are also represented by Expression. Every basci Expression corresponds to a Node. And input data is also a Node.
Expression has a data member ComputationGraph, and ComputationGraph will be modified in users' configuring process. Expression can be a running target, beacuse Expression contains all dependency.
Actually, Symbol/Tensor/Expression in Mxnet/TensorFlow/Dynet are the same level concepts. We use a unified name Expression here, this level concept has following features:
- Users wirte topoloy with symbolic API, and all return value is Expression, including input data and parameter.
- Expression corresponds with a global Graph, and Expression can also be composed.
- Expression tracks all dependency and can be taken as a run target
<spanid="survey-on-graph"></span><h1>Survey on Graph<aclass="headerlink"href="#survey-on-graph"title="永久链接至标题">¶</a></h1>
<p>Neural network framework often provides symbolic API for users to write network topology conveniently. This doc manily focus on symbolic API in most popular neural network frameworks, and try to find out how to parse symbolic configuration to a portable file, such as protobuf or json.</p>
<p>The core concept of symbolic API is <codeclass="docutils literal"><spanclass="pre">Symbol</span></code>. Mxnet implements <codeclass="docutils literal"><spanclass="pre">Symbol</span></code> class in C++, and export to Python using C-API. Please refer to the comments in Mxnet:</p>
<p><codeclass="docutils literal"><spanclass="pre">Symbol</span></code> is help class used to represent the operator node in Graph.
<codeclass="docutils literal"><spanclass="pre">Symbol</span></code> acts as an interface for building graphs from different components like Variable, Functor and Group. <codeclass="docutils literal"><spanclass="pre">Symbol</span></code> is also exported to python front-end (while Graph is not) to enable quick test and deployment. Conceptually, symbol is the final operation of a graph and thus including all the information required (the graph) to evaluate its output value.</p>
<p>A simple network topology wrote by Symbol is as follows:</p>
<p>Varible here is actually a Symbol. Every basic Symbol will correspond to one Node, and every Node has its own NodeAttr. There is a op field in NodeAttr class, when a Symbol represents Variable(often input data), the op field is null.</p>
<p>Symbol contains a data member, std::vector<NodeEntry> outputs, and NodeEntry cantains a poniter to Node. We can follow the Node pointer to get all the Graph.</p>
<p>The core concept of symbolic API is <codeclass="docutils literal"><spanclass="pre">Tensor</span></code>. Tensorflow defines <codeclass="docutils literal"><spanclass="pre">Tensor</span></code> in Python. Please refer to the comments in TensorFlow:</p>
<p>A <codeclass="docutils literal"><spanclass="pre">Tensor</span></code> is a symbolic handle to one of the outputs of an <codeclass="docutils literal"><spanclass="pre">Operation</span></code>. It does not hold the values of that operation’s output, but instead provides a means of computing those values in a TensorFlow <aclass="reference external"href="https://www.tensorflow.org/api_docs/python/tf/Session">Session</a>.</p>
<p>A simple example is as follows:</p>
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="c1"># Build a dataflow graph.</span>
<spanclass="k">raise</span><spanclass="ne">ValueError</span><spanclass="p">(</span><spanclass="s2">"Operation was not named: </span><spanclass="si">%s</span><spanclass="s2">"</span><spanclass="o">%</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">_op</span><spanclass="p">)</span>
<p>The core concept of symbolic API is <codeclass="docutils literal"><spanclass="pre">Expression</span></code>, and Dynet defines <codeclass="docutils literal"><spanclass="pre">Expression</span></code> class in C++.</p>
<p>The input data and parameter are also represented by Expression. Every basci Expression corresponds to a Node. And input data is also a Node.</p>
<p>Expression has a data member ComputationGraph, and ComputationGraph will be modified in users’ configuring process. Expression can be a running target, beacuse Expression contains all dependency.</p>
<p>Actually, Symbol/Tensor/Expression in Mxnet/TensorFlow/Dynet are the same level concepts. We use a unified name Expression here, this level concept has following features:</p>
<ulclass="simple">
<li>Users wirte topoloy with symbolic API, and all return value is Expression, including input data and parameter.</li>
<li>Expression corresponds with a global Graph, and Expression can also be composed.</li>
<li>Expression tracks all dependency and can be taken as a run target</li>
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>.