versions/0.11.0/api/perl/symbol.html (339 lines of code) (raw):

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <meta content="IE=edge" http-equiv="X-UA-Compatible"/> <meta content="width=device-width, initial-scale=1" name="viewport"/> <meta content="MXNet Perl Symbolic API" property="og:title"> <meta content="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/og-logo.png" property="og:image"> <meta content="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/og-logo.png" property="og:image:secure_url"> <meta content="MXNet Perl Symbolic API" property="og:description"/> <title>MXNet Perl Symbolic API — mxnet documentation</title> <link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/> <link href="../../_static/basic.css" rel="stylesheet" type="text/css"> <link href="../../_static/pygments.css" rel="stylesheet" type="text/css"> <link href="../../_static/mxnet.css" rel="stylesheet" type="text/css"/> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true, SOURCELINK_SUFFIX: '.txt' }; </script> <script src="https://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script> <script src="../../_static/underscore.js" type="text/javascript"></script> <script src="../../_static/searchtools_custom.js" type="text/javascript"></script> <script src="../../_static/doctools.js" type="text/javascript"></script> <script src="../../_static/selectlang.js" type="text/javascript"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script> <script type="text/javascript"> jQuery(function() { Search.loadIndex("/versions/0.11.0/searchindex.js"); Search.init();}); </script> <!-- --> <!-- <script type="text/javascript" src="../../_static/jquery.js"></script> --> <!-- --> <!-- <script type="text/javascript" src="../../_static/underscore.js"></script> --> <!-- --> <!-- <script type="text/javascript" src="../../_static/doctools.js"></script> --> <!-- --> <!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> --> <!-- --> <link href="../../genindex.html" rel="index" title="Index"> <link href="../../search.html" rel="search" title="Search"/> <link href="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/mxnet-icon.png" rel="icon" type="image/png"/> </link></link></link></meta></meta></meta></head> <body background="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/mxnet-background-compressed.jpeg" role="document"> <div class="content-block"><div class="navbar navbar-fixed-top"> <div class="container" id="navContainer"> <div class="innder" id="header-inner"> <h1 id="logo-wrap"> <a href="../../" id="logo"><img src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/mxnet_logo.png"/></a> </h1> <nav class="nav-bar" id="main-nav"> <a class="main-nav-link" href="/versions/0.11.0/get_started/install.html">Install</a> <span id="dropdown-menu-position-anchor"> <a aria-expanded="true" aria-haspopup="true" class="main-nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button">Gluon <span class="caret"></span></a> <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu"> <li><a class="main-nav-link" href="/versions/0.11.0/tutorials/gluon/gluon.html">About</a></li> <li><a class="main-nav-link" href="https://www.d2l.ai/">Dive into Deep Learning</a></li> <li><a class="main-nav-link" href="https://gluon-cv.mxnet.io">GluonCV Toolkit</a></li> <li><a class="main-nav-link" href="https://gluon-nlp.mxnet.io/">GluonNLP Toolkit</a></li> </ul> </span> <span id="dropdown-menu-position-anchor"> <a aria-expanded="true" aria-haspopup="true" class="main-nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button">API <span class="caret"></span></a> <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu"> <li><a class="main-nav-link" href="/versions/0.11.0/api/python/index.html">Python</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/api/c++/index.html">C++</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/api/julia/index.html">Julia</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/api/perl/index.html">Perl</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/api/r/index.html">R</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/api/scala/index.html">Scala</a></li> </ul> </span> <span id="dropdown-menu-position-anchor-docs"> <a aria-expanded="true" aria-haspopup="true" class="main-nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button">Docs <span class="caret"></span></a> <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu-docs"> <li><a class="main-nav-link" href="/versions/0.11.0/how_to/faq.html">FAQ</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/tutorials/index.html">Tutorials</a> <li><a class="main-nav-link" href="https://github.com/apache/incubator-mxnet/tree/v0.11.0/example">Examples</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/architecture/index.html">Architecture</a></li> <li><a class="main-nav-link" href="https://cwiki.apache.org/confluence/display/MXNET/Apache+MXNet+Home">Developer Wiki</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/model_zoo/index.html">Model Zoo</a></li> <li><a class="main-nav-link" href="https://github.com/onnx/onnx-mxnet">ONNX</a></li> </li></ul> </span> <span id="dropdown-menu-position-anchor-community"> <a aria-expanded="true" aria-haspopup="true" class="main-nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button">Community <span class="caret"></span></a> <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu-community"> <li><a class="main-nav-link" href="http://discuss.mxnet.io">Forum</a></li> <li><a class="main-nav-link" href="https://github.com/apache/incubator-mxnet/tree/v0.11.0">Github</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/community/contribute.html">Contribute</a></li> </ul> </span> <span id="dropdown-menu-position-anchor-version" style="position: relative"><a href="#" class="main-nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">0.11.0<span class="caret"></span></a><ul id="package-dropdown-menu" class="dropdown-menu"><li><a href="/">master</a></li><li><a href="/versions/1.7.0/">1.7.0</a></li><li><a href=/versions/1.6.0/>1.6.0</a></li><li><a href=/versions/1.5.0/>1.5.0</a></li><li><a href=/versions/1.4.1/>1.4.1</a></li><li><a href=/versions/1.3.1/>1.3.1</a></li><li><a href=/versions/1.2.1/>1.2.1</a></li><li><a href=/versions/1.1.0/>1.1.0</a></li><li><a href=/versions/1.0.0/>1.0.0</a></li><li><a href=/versions/0.12.1/>0.12.1</a></li><li><a href=/versions/0.11.0/>0.11.0</a></li></ul></span></nav> <script> function getRootPath(){ return "../../" } </script> <div class="burgerIcon dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button">☰</a> <ul class="dropdown-menu" id="burgerMenu"> <li><a href="/versions/0.11.0/get_started/install.html">Install</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/tutorials/index.html">Tutorials</a></li> <li class="dropdown-submenu dropdown"> <a aria-expanded="true" aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" tabindex="-1">Gluon</a> <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu"> <li><a class="main-nav-link" href="/versions/0.11.0/tutorials/gluon/gluon.html">About</a></li> <li><a class="main-nav-link" href="http://gluon.mxnet.io">The Straight Dope (Tutorials)</a></li> <li><a class="main-nav-link" href="https://gluon-cv.mxnet.io">GluonCV Toolkit</a></li> <li><a class="main-nav-link" href="https://gluon-nlp.mxnet.io/">GluonNLP Toolkit</a></li> </ul> </li> <li class="dropdown-submenu"> <a aria-expanded="true" aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" tabindex="-1">API</a> <ul class="dropdown-menu"> <li><a class="main-nav-link" href="/versions/0.11.0/api/python/index.html">Python</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/api/c++/index.html">C++</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/api/julia/index.html">Julia</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/api/perl/index.html">Perl</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/api/r/index.html">R</a></li> <li><a class="main-nav-link" href="/versions/0.11.0/api/scala/index.html">Scala</a></li> </ul> </li> <li class="dropdown-submenu"> <a aria-expanded="true" aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" tabindex="-1">Docs</a> <ul class="dropdown-menu"> <li><a href="/versions/0.11.0/how_to/faq.html" tabindex="-1">FAQ</a></li> <li><a href="/versions/0.11.0/tutorials/index.html" tabindex="-1">Tutorials</a></li> <li><a href="https://github.com/apache/incubator-mxnet/tree/v0.11.0/example" tabindex="-1">Examples</a></li> <li><a href="/versions/0.11.0/architecture/index.html" tabindex="-1">Architecture</a></li> <li><a href="https://cwiki.apache.org/confluence/display/MXNET/Apache+MXNet+Home" tabindex="-1">Developer Wiki</a></li> <li><a href="/versions/0.11.0/model_zoo/index.html" tabindex="-1">Gluon Model Zoo</a></li> <li><a href="https://github.com/onnx/onnx-mxnet" tabindex="-1">ONNX</a></li> </ul> </li> <li class="dropdown-submenu dropdown"> <a aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" role="button" tabindex="-1">Community</a> <ul class="dropdown-menu"> <li><a href="http://discuss.mxnet.io" tabindex="-1">Forum</a></li> <li><a href="https://github.com/apache/incubator-mxnet/tree/v0.11.0" tabindex="-1">Github</a></li> <li><a href="/versions/0.11.0/community/contribute.html" tabindex="-1">Contribute</a></li> </ul> </li> <li id="dropdown-menu-position-anchor-version-mobile" class="dropdown-submenu" style="position: relative"><a href="#" tabindex="-1">0.11.0</a><ul class="dropdown-menu"><li><a tabindex="-1" href=/>master</a></li><li><a tabindex="-1" href=/versions/1.6.0/>1.6.0</a></li><li><a tabindex="-1" href=/versions/1.5.0/>1.5.0</a></li><li><a tabindex="-1" href=/versions/1.4.1/>1.4.1</a></li><li><a tabindex="-1" href=/versions/1.3.1/>1.3.1</a></li><li><a tabindex="-1" href=/versions/1.2.1/>1.2.1</a></li><li><a tabindex="-1" href=/versions/1.1.0/>1.1.0</a></li><li><a tabindex="-1" href=/versions/1.0.0/>1.0.0</a></li><li><a tabindex="-1" href=/versions/0.12.1/>0.12.1</a></li><li><a tabindex="-1" href=/versions/0.11.0/>0.11.0</a></li></ul></li></ul> </div> <div class="plusIcon dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button"><span aria-hidden="true" class="glyphicon glyphicon-plus"></span></a> <ul class="dropdown-menu dropdown-menu-right" id="plusMenu"></ul> </div> <div id="search-input-wrap"> <form action="../../search.html" autocomplete="off" class="" method="get" role="search"> <div class="form-group inner-addon left-addon"> <i class="glyphicon glyphicon-search"></i> <input class="form-control" name="q" placeholder="Search" type="text"/> </div> <input name="check_keywords" type="hidden" value="yes"> <input name="area" type="hidden" value="default"/> </input></form> <div id="search-preview"></div> </div> <div id="searchIcon"> <span aria-hidden="true" class="glyphicon glyphicon-search"></span> </div> <!-- <div id="lang-select-wrap"> --> <!-- <label id="lang-select-label"> --> <!-- <\!-- <i class="fa fa-globe"></i> -\-> --> <!-- <span></span> --> <!-- </label> --> <!-- <select id="lang-select"> --> <!-- <option value="en">Eng</option> --> <!-- <option value="zh">中文</option> --> <!-- </select> --> <!-- </div> --> <!-- <a id="mobile-nav-toggle"> <span class="mobile-nav-toggle-bar"></span> <span class="mobile-nav-toggle-bar"></span> <span class="mobile-nav-toggle-bar"></span> </a> --> </div> </div> </div> <script type="text/javascript"> $('body').css('background', 'white'); </script> <div class="container"> <div class="row"> <div aria-label="main navigation" class="sphinxsidebar leftsidebar" role="navigation"> <div class="sphinxsidebarwrapper"> <ul> <li class="toctree-l1"><a class="reference internal" href="../python/index.html">Python Documents</a></li> <li class="toctree-l1"><a class="reference internal" href="../r/index.html">R Documents</a></li> <li class="toctree-l1"><a class="reference internal" href="../julia/index.html">Julia Documents</a></li> <li class="toctree-l1"><a class="reference internal" href="../c++/index.html">C++ Documents</a></li> <li class="toctree-l1"><a class="reference internal" href="../scala/index.html">Scala Documents</a></li> <li class="toctree-l1"><a class="reference internal" href="index.html">Perl Documents</a></li> <li class="toctree-l1"><a class="reference internal" href="../../how_to/index.html">HowTo Documents</a></li> <li class="toctree-l1"><a class="reference internal" href="../../architecture/index.html">System Documents</a></li> <li class="toctree-l1"><a class="reference internal" href="../../tutorials/index.html">Tutorials</a></li> </ul> </div> </div> <div class="content"> <div class="page-tracker"></div> <div class="section" id="mxnet-perl-symbolic-api"> <span id="mxnet-perl-symbolic-api"></span><h1>MXNet Perl Symbolic API<a class="headerlink" href="#mxnet-perl-symbolic-api" title="Permalink to this headline">¶</a></h1> <p>Topics:</p> <ul class="simple"> <li><a class="reference external" href="#how-to-compose-symbols">How to Compose Symbols</a> introduces operator overloading of symbols.</li> <li><a class="reference external" href="#symbol-attributes">Symbol Attributes</a> describes how to attach attributes to symbols.</li> <li><a class="reference external" href="#serialization">Serialization</a> explains how to save and load symbols.</li> <li><a class="reference external" href="#executing-symbols">Executing Symbols</a> explains how to evaluate the symbols with data.</li> <li><a class="reference external" href="#multiple-outputs">Multiple Outputs</a> explains how to configure multiple outputs.</li> </ul> <div class="section" id="how-to-compose-symbols"> <span id="how-to-compose-symbols"></span><h2>How to Compose Symbols<a class="headerlink" href="#how-to-compose-symbols" title="Permalink to this headline">¶</a></h2> <p>The symbolic API provides a way to configure computation graphs. You can configure the graphs either at the level of neural network layer operations or as fine-grained operations.</p> <p>The following example configures a two-layer neural network.</p> <div class="highlight-perl"><div class="highlight"><pre><span></span>pdl> use AI::MXNet qw(mx) pdl> $data = mx->symbold->Variable("data") pdl> $fc1 = mx->symbol->FullyConnected(data => $data, name => "fc1", num_hidden" -> 128) pdl> $act1 = mx->symbol->Activation(data => $fc1, name => "relu1", act_type => "relu") pdl> $fc2 = mx->symbol->FullyConnected(data => $act1, name => "fc2", num_hidden => 64) pdl> $net = mx->symbol->SoftmaxOutput(data => $fc2, name => "out") </pre></div> </div> <p>The basic arithmetic operators (plus, minus, div, multiplication) are overloaded for <em>element-wise operations</em> of symbols.</p> <p>The following example creates a computation graph that adds two inputs together.</p> <div class="highlight-perl"><div class="highlight"><pre><span></span><span class="n">pdl</span><span class="o">></span> <span class="k">use</span> <span class="nn">AI::MXNet</span> <span class="sx">qw(mx)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$a</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">symbol</span><span class="o">-></span><span class="n">Variable</span><span class="p">(</span><span class="s">"a"</span><span class="p">)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$b</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">symbol</span><span class="o">-></span><span class="n">Variable</span><span class="p">(</span><span class="s">"b"</span><span class="p">)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$c</span> <span class="o">=</span> <span class="nv">$a</span> <span class="o">+</span> <span class="nv">$b</span> </pre></div> </div> </div> <div class="section" id="symbol-attributes"> <span id="symbol-attributes"></span><h2>Symbol Attributes<a class="headerlink" href="#symbol-attributes" title="Permalink to this headline">¶</a></h2> <p>You can add an attribute to a symbol by providing an attribute hash when you create a symbol.</p> <div class="highlight-perl"><div class="highlight"><pre><span></span><span class="nv">$data</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">symbol</span><span class="o">-></span><span class="n">Variable</span><span class="p">(</span><span class="s">"data"</span><span class="p">,</span> <span class="n">attr</span> <span class="o">=></span> <span class="p">{</span> <span class="n">mood</span> <span class="o">=></span> <span class="s">"angry"</span> <span class="p">})</span> <span class="nv">$op</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">symbol</span><span class="o">-></span><span class="n">Convolution</span><span class="p">(</span><span class="n">data</span> <span class="o">=></span> <span class="nv">$data</span><span class="p">,</span> <span class="n">kernel</span> <span class="o">=></span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">num_filter</span> <span class="o">=></span> <span class="mi">1</span><span class="p">,</span> <span class="n">attr</span> <span class="o">=></span> <span class="p">{</span> <span class="n">mood</span> <span class="o">=></span> <span class="s">"so so"</span> <span class="p">})</span> </pre></div> </div> <p>For proper communication with the C++ backend, both the key and values of the attribute dictionary should be strings. To retrieve the attributes, use <code class="docutils literal"><span class="pre">->attr($key)</span></code>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span> $data->attr("mood") </pre></div> </div> <p>To attach attributes, you can use <code class="docutils literal"><span class="pre">AI::MXNet::AttrScope</span></code>. <code class="docutils literal"><span class="pre">AI::MXNet::AttrScopeAttrScope</span></code> automatically adds the specified attributes to all of the symbols created within that scope. The user can also inherit this object to change naming behavior. For example:</p> <div class="highlight-perl"><div class="highlight"><pre><span></span><span class="k">use</span> <span class="nn">AI::MXNet</span> <span class="sx">qw(mx)</span><span class="p">;</span> <span class="k">use</span> <span class="nn">Test::More</span> <span class="n">tests</span> <span class="o">=></span> <span class="mi">3</span><span class="p">;</span> <span class="k">my</span> <span class="p">(</span><span class="nv">$data</span><span class="p">,</span> <span class="nv">$gdata</span><span class="p">);</span> <span class="p">{</span> <span class="nb">local</span><span class="p">(</span><span class="nv">$</span><span class="nn">mx::</span><span class="nv">AttrScope</span><span class="p">)</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="n">AttrScope</span><span class="p">(</span><span class="n">group</span><span class="o">=></span><span class="mi">4</span><span class="p">,</span> <span class="n">data</span><span class="o">=></span><span class="s">'great'</span><span class="p">);</span> <span class="nv">$data</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">sym</span><span class="o">-></span><span class="n">Variable</span><span class="p">(</span><span class="s">"data"</span><span class="p">,</span> <span class="n">attr</span> <span class="o">=></span> <span class="p">{</span> <span class="n">dtype</span> <span class="o">=></span> <span class="s">"data"</span><span class="p">,</span> <span class="n">group</span> <span class="o">=></span> <span class="s">"1"</span> <span class="p">});</span> <span class="nv">$gdata</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">sym</span><span class="o">-></span><span class="n">Variable</span><span class="p">(</span><span class="s">"data2"</span><span class="p">);</span> <span class="p">}</span> <span class="n">ok</span><span class="p">(</span><span class="nv">$gdata</span><span class="o">-></span><span class="n">attr</span><span class="p">(</span><span class="s">"group"</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">);</span> <span class="n">ok</span><span class="p">(</span><span class="nv">$data</span><span class="o">-></span><span class="n">attr</span><span class="p">(</span><span class="s">"group"</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">);</span> <span class="k">my</span> <span class="nv">$exceedScopeData</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">sym</span><span class="o">-></span><span class="n">Variable</span><span class="p">(</span><span class="s">"data3"</span><span class="p">);</span> <span class="n">ok</span><span class="p">((</span><span class="ow">not</span> <span class="nb">defined</span> <span class="nv">$exceedScopeData</span><span class="o">-></span><span class="n">attr</span><span class="p">(</span><span class="s">"group"</span><span class="p">)),</span> <span class="s">"No group attr in global attr scope"</span><span class="p">);</span> </pre></div> </div> </div> <div class="section" id="serialization"> <span id="serialization"></span><h2>Serialization<a class="headerlink" href="#serialization" title="Permalink to this headline">¶</a></h2> <p>There are two ways to save and load the symbols. You can use the <code class="docutils literal"><span class="pre">mx->symbol->save</span></code> and <code class="docutils literal"><span class="pre">mxnet->symbol->load</span></code> functions to serialize the <code class="docutils literal"><span class="pre">AI::MXNet::Symbol</span></code> objects. The advantage of using <code class="docutils literal"><span class="pre">save</span></code> and <code class="docutils literal"><span class="pre">load</span></code> functions is that it is language agnostic and cloud friendly. The symbol is saved in JSON format. You can also get a JSON string directly using <code class="docutils literal"><span class="pre">$symbol->tojson</span></code>.</p> <p>The following example shows how to save a symbol to an S3 bucket, load it back, and compare two symbols using a JSON string.</p> <div class="highlight-perl"><div class="highlight"><pre><span></span><span class="n">pdl</span><span class="o">></span> <span class="k">use</span> <span class="nn">AI::MXNet</span> <span class="sx">qw(mx)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$a</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">sym</span><span class="o">-></span><span class="n">Variable</span><span class="p">(</span><span class="s">"a"</span><span class="p">)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$b</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">sym</span><span class="o">-></span><span class="n">Variable</span><span class="p">(</span><span class="s">"b"</span><span class="p">)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$c</span> <span class="o">=</span> <span class="nv">$a</span> <span class="o">+</span> <span class="nv">$b</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$c</span><span class="o">-></span><span class="n">save</span><span class="p">(</span><span class="s">"s3://my-bucket/symbol-c.json"</span><span class="p">)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$c2</span> <span class="o">=</span> <span class="nv">$c</span><span class="o">-></span><span class="n">load</span><span class="p">(</span><span class="s">"s3://my-bucket/symbol-c.json"</span><span class="p">)</span> <span class="n">pdl</span><span class="o">></span> <span class="n">ok</span><span class="p">(</span><span class="nv">$c</span><span class="o">-></span><span class="n">tojson</span> <span class="ow">eq</span> <span class="nv">$c2</span><span class="o">-></span><span class="n">tojson</span><span class="p">)</span> <span class="n">ok</span> <span class="mi">1</span> </pre></div> </div> </div> <div class="section" id="executing-symbols"> <span id="executing-symbols"></span><h2>Executing Symbols<a class="headerlink" href="#executing-symbols" title="Permalink to this headline">¶</a></h2> <p>After you have assembled a set of symbols into a computation graph, the MXNet engine can evaluate them. If you are training a neural network, this is typically handled by the high-level <a class="reference internal" href="module.html"><span class="doc">AI::MXNet::Module package</span></a> and the [<code class="docutils literal"><span class="pre">fit()</span></code>] function.</p> <p>For neural networks used in “feed-forward”, “prediction”, or “inference” mode (all terms for the same thing: running a trained network), the input arguments are the input data, and the weights of the neural network that were learned during training.</p> <p>To manually execute a set of symbols, you need to create an [<code class="docutils literal"><span class="pre">AI::MXNet::Executor</span></code>] object, which is typically constructed by calling the [<code class="docutils literal"><span class="pre">simple_bind(<parameters>)</span></code>] method on a AI::MXNet::Symbol.</p> </div> <div class="section" id="multiple-outputs"> <span id="multiple-outputs"></span><h2>Multiple Outputs<a class="headerlink" href="#multiple-outputs" title="Permalink to this headline">¶</a></h2> <p>To group the symbols together, use the <a class="reference external" href="#mxnet.symbol.Group">AI::MXNet::Symbol->Group</a> function.</p> <div class="highlight-perl"><div class="highlight"><pre><span></span><span class="n">pdl</span><span class="o">></span> <span class="k">use</span> <span class="nn">AI::MXNet</span> <span class="sx">qw(mx)</span> <span class="n">pdl</span><span class="o">></span> <span class="k">use</span> <span class="nn">Data::Dumper</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$data</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">sym</span><span class="o">-></span><span class="n">Variable</span><span class="p">(</span><span class="s">"data"</span><span class="p">)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$fc1</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">sym</span><span class="o">-></span><span class="n">FullyConnected</span><span class="p">(</span><span class="nv">$data</span><span class="p">,</span> <span class="n">name</span> <span class="o">=></span> <span class="s">"fc1"</span><span class="p">,</span> <span class="n">num_hidden</span> <span class="o">=></span> <span class="mi">128</span><span class="p">)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$act1</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">sym</span><span class="o">-></span><span class="n">Activation</span><span class="p">(</span><span class="nv">$fc1</span><span class="p">,</span> <span class="n">name</span> <span class="o">=></span> <span class="s">"relu1"</span><span class="p">,</span> <span class="n">act_type</span> <span class="o">=></span> <span class="s">"relu"</span><span class="p">)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$fc2</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">sym</span><span class="o">-></span><span class="n">FullyConnected</span><span class="p">(</span><span class="nv">$act1</span><span class="p">,</span> <span class="n">name</span> <span class="o">=></span> <span class="s">"fc2"</span><span class="p">,</span> <span class="n">num_hidden</span> <span class="o">=></span> <span class="mi">64</span><span class="p">)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$net</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">sym</span><span class="o">-></span><span class="n">SoftmaxOutput</span><span class="p">(</span><span class="nv">$fc2</span><span class="p">,</span> <span class="n">name</span> <span class="o">=></span> <span class="s">"softmax"</span><span class="p">)</span> <span class="n">pdl</span><span class="o">></span> <span class="nv">$group</span> <span class="o">=</span> <span class="nn">mx</span><span class="o">-></span><span class="nn">sym</span><span class="o">-></span><span class="n">Group</span><span class="p">([</span><span class="nv">$fc1</span><span class="p">,</span> <span class="nv">$net</span><span class="p">])</span> <span class="n">pdl</span><span class="o">></span> <span class="k">print</span> <span class="n">Dumper</span><span class="p">(</span><span class="nv">$group</span><span class="o">-></span><span class="n">list_outputs</span><span class="p">())</span> <span class="nv">$VAR1</span> <span class="o">=</span> <span class="p">[</span> <span class="s">'fc1_output'</span><span class="p">,</span> <span class="s">'softmax_output'</span> <span class="p">];</span> </pre></div> </div> <p>After you get the <code class="docutils literal"><span class="pre">Group</span></code>, you can bind on <code class="docutils literal"><span class="pre">group</span></code> instead. The resulting executor will have two outputs, one for fc1_output and one for softmax_output.</p> </div> </div> </div> </div> <div aria-label="main navigation" class="sphinxsidebar rightsidebar" role="navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../../index.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">MXNet Perl Symbolic API</a><ul> <li><a class="reference internal" href="#how-to-compose-symbols">How to Compose Symbols</a></li> <li><a class="reference internal" href="#symbol-attributes">Symbol Attributes</a></li> <li><a class="reference internal" href="#serialization">Serialization</a></li> <li><a class="reference internal" href="#executing-symbols">Executing Symbols</a></li> <li><a class="reference internal" href="#multiple-outputs">Multiple Outputs</a></li> </ul> </li> </ul> </div> </div> </div><div class="footer"> <div class="section-disclaimer"> <div class="container"> <div> <img height="60" src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/apache_incubator_logo.png"/> <p> Apache MXNet is an effort undergoing incubation at The Apache Software Foundation (ASF), <strong>sponsored by the <i>Apache Incubator</i></strong>. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF. </p> <p> "Copyright © 2017-2018, The Apache Software Foundation Apache MXNet, MXNet, Apache, the Apache feather, and the Apache MXNet project logo are either registered trademarks or trademarks of the Apache Software Foundation." </p> </div> </div> </div> </div> <!-- pagename != index --> </div> <script crossorigin="anonymous" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script src="../../_static/js/sidebar.js" type="text/javascript"></script> <script src="../../_static/js/search.js" type="text/javascript"></script> <script src="../../_static/js/navbar.js" type="text/javascript"></script> <script src="../../_static/js/clipboard.min.js" type="text/javascript"></script> <script src="../../_static/js/copycode.js" type="text/javascript"></script> <script src="../../_static/js/page.js" type="text/javascript"></script> <script src="../../_static/js/docversion.js" type="text/javascript"></script> <script type="text/javascript"> $('body').ready(function () { $('body').css('visibility', 'visible'); }); </script> </body> </html>