SIGN IN SIGN UP
apache / mxnet UNCLAIMED

Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more

0 0 0 C++
# Module API
2016-04-24 21:24:07 -04:00
```eval_rst
.. currentmodule:: mxnet.module
```
2016-04-24 21:24:07 -04:00
## Overview
2016-04-24 21:24:07 -04:00
The module API, defined in the `module` (or simply `mod`) package, provides an
intermediate and high-level interface for performing computation with a
`Symbol`. One can roughly think a module is a machine which can execute a
program defined by a `Symbol`.
The `module.Module` accepts a `Symbol` as the input.
```python
>>> data = mx.sym.Variable('data')
>>> fc1 = mx.sym.FullyConnected(data, name='fc1', num_hidden=128)
>>> act1 = mx.sym.Activation(fc1, name='relu1', act_type="relu")
>>> fc2 = mx.sym.FullyConnected(act1, name='fc2', num_hidden=10)
>>> out = mx.sym.SoftmaxOutput(fc2, name = 'softmax')
>>> mod = mx.mod.Module(out) # create a module by given a Symbol
2016-04-24 21:24:07 -04:00
```
Assume there is a valid MXNet data iterator `nd_iter`. We can initialize the
module:
2016-04-24 21:24:07 -04:00
```python
>>> mod.bind(data_shapes=nd_iter.provide_data,
>>> label_shapes=nd_iter.provide_label) # create memory by given input shapes
>>> mod.init_params() # initial parameters with the default random initializer
2016-04-24 21:24:07 -04:00
```
Now the module is able to compute. We can call high-level API to train and
predict:
2016-04-24 21:24:07 -04:00
```python
>>> mod.fit(nd_iter, num_epoch=10, ...) # train
>>> mod.predict(new_nd_iter) # predict on new data
2016-04-24 21:24:07 -04:00
```
or use intermediate APIs to perform step-by-step computations
2016-04-24 21:24:07 -04:00
```python
>>> mod.forward(data_batch) # forward on the provided data batch
>>> mod.backward() # backward to calculate the gradients
>>> mod.update() # update parameters using the default optimizer
2016-04-24 21:24:07 -04:00
```
A detailed tutorial is available at
[Module - Neural network training and inference](http://mxnet.io/tutorials/basic/module.html).
2016-04-24 21:24:07 -04:00
The `module` package provides several modules:
2016-04-24 21:24:07 -04:00
```eval_rst
.. autosummary::
:nosignatures:
BaseModule
Module
SequentialModule
BucketingModule
PythonModule
PythonLossModule
2016-04-24 21:24:07 -04:00
```
We summarize the interface for each class in the following sections.
2016-04-24 21:24:07 -04:00
## The `BaseModule` class
The `BaseModule` is the base class for all other module classes. It defines the
interface each module class should provide.
2016-04-24 21:24:07 -04:00
### Initialize memory
2016-04-24 21:24:07 -04:00
```eval_rst
.. autosummary::
:nosignatures:
2016-04-24 21:24:07 -04:00
BaseModule.bind
```
2016-04-24 21:24:07 -04:00
### Get and set parameters
2016-04-24 21:24:07 -04:00
```eval_rst
.. autosummary::
:nosignatures:
BaseModule.init_params
BaseModule.set_params
BaseModule.get_params
BaseModule.save_params
BaseModule.load_params
2016-04-24 21:24:07 -04:00
```
### Train and predict
2016-04-24 21:24:07 -04:00
```eval_rst
.. autosummary::
:nosignatures:
BaseModule.fit
BaseModule.score
BaseModule.iter_predict
BaseModule.predict
2016-04-24 21:24:07 -04:00
```
### Forward and backward
2016-04-24 21:24:07 -04:00
```eval_rst
.. autosummary::
:nosignatures:
2016-04-24 21:24:07 -04:00
BaseModule.forward
BaseModule.backward
BaseModule.forward_backward
```
### Update parameters
2016-04-21 10:10:09 -04:00
```eval_rst
.. autosummary::
:nosignatures:
2016-04-21 10:10:09 -04:00
BaseModule.init_optimizer
BaseModule.update
BaseModule.update_metric
2016-04-21 10:10:09 -04:00
```
### Input and output
2016-04-21 10:10:09 -04:00
```eval_rst
.. autosummary::
:nosignatures:
BaseModule.data_names
BaseModule.output_names
BaseModule.data_shapes
BaseModule.label_shapes
BaseModule.output_shapes
BaseModule.get_outputs
BaseModule.get_input_grads
```
2016-04-21 10:10:09 -04:00
### Others
2016-04-21 10:10:09 -04:00
```eval_rst
.. autosummary::
:nosignatures:
BaseModule.get_states
BaseModule.set_states
BaseModule.install_monitor
BaseModule.symbol
2016-04-21 10:10:09 -04:00
```
## Other build-in modules
2016-04-21 10:10:09 -04:00
Besides the basic interface defined in `BaseModule`, each module class supports
additional functionality. We summarize them in this section.
2016-04-21 10:10:09 -04:00
### Class `Module`
2016-04-21 10:10:09 -04:00
```eval_rst
.. autosummary::
:nosignatures:
Module.load
Module.save_checkpoint
Module.reshape
Module.borrow_optimizer
Module.save_optimizer_states
Module.load_optimizer_states
2016-04-21 10:10:09 -04:00
```
### Class `BucketModule`
2016-04-21 10:10:09 -04:00
```eval_rst
.. autosummary::
:nosignatures:
2016-04-21 10:10:09 -04:00
BucketModule.switch_bucket
2016-04-21 10:10:09 -04:00
```
### Class `SequentialModule`
2016-04-21 10:10:09 -04:00
```eval_rst
.. autosummary::
:nosignatures:
2016-04-21 10:10:09 -04:00
SequentialModule.add
2016-04-21 10:10:09 -04:00
```
## API Reference
<script type="text/javascript" src='../../../_static/js/auto_module_index.js'></script>
2016-04-21 10:10:09 -04:00
```eval_rst
.. autoclass:: mxnet.module.BaseModule
:members:
.. autoclass:: mxnet.module.Module
:members:
.. autoclass:: mxnet.module.BucketingModule
:members:
.. autoclass:: mxnet.module.SequentialModule
:members:
.. autoclass:: mxnet.module.PythonModule
:members:
.. autoclass:: mxnet.module.PythonLossModule
:members:
2016-04-21 10:10:09 -04:00
```
<script>auto_index("api-reference");</script>