2.6. Documentation

Due to constraints on the length of this book, we cannot possibly introduce every single MXNet function and class (and you probably would no want us to). The API documentation and additional tutorials and examples provide plenty of documentation beyond the book. In this section we provide you some guidance to exploring the MXNet API.

2.6.1. Finding all the functions and classes in the module

In order to know which functions and classes can be called in a module, we invoke the dir function. For instance, we can query all properties in the nd.random module as follows:

from mxnet import nd
['NDArray', '_Null', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_internal', '_random_helper', 'current_context', 'exponential', 'exponential_like', 'gamma', 'gamma_like', 'generalized_negative_binomial', 'generalized_negative_binomial_like', 'multinomial', 'negative_binomial', 'negative_binomial_like', 'normal', 'normal_like', 'numeric_types', 'poisson', 'poisson_like', 'randint', 'randn', 'shuffle', 'uniform', 'uniform_like']

Generally, we can ignore functions that start and end with __ (special objects in Python) or functions that start with a single _(usually internal functions). Based on the remaining function/attribute names, we might hazard a guess that this module offers various methods for generating random numbers, including sampling from the uniform distribution (uniform), normal distribution (normal), and Poisson distribution (poisson).

2.6.2. Finding the usage of specific functions and classes

For more specific instructions on how to use a given function or class, we can invoke the help function. As an example, let’s explore the usage instructions for NDArray’s ones_like function.

Help on function ones_like:

ones_like(data=None, out=None, name=None, **kwargs)
    Return an array of ones with the same shape and type
    as the input array.


      x = [[ 0.,  0.,  0.],
           [ 0.,  0.,  0.]]

      ones_like(x) = [[ 1.,  1.,  1.],
                      [ 1.,  1.,  1.]]

    data : NDArray
        The input

    out : NDArray, optional
        The output NDArray to hold the result.

    out : NDArray or list of NDArrays
        The output of this function.

From the documentation, we can see that the ones_like function creates a new array with the same shape as the supplied NDArray and all elements set to 1. Whenever possible, you should run a quick test to confirm your interpretation:

x = nd.array([[0, 0, 0], [2, 2, 2]])
y = x.ones_like()
[[1. 1. 1.]
 [1. 1. 1.]]
<NDArray 2x3 @cpu(0)>

In the Jupyter notebook, we can use ? to display the document in another window. For example, nd.random.uniform? will create content that is almost identical to help(nd.random.uniform), displaying it in a new browser window. In addition, if we use two question marks, e.g. nd.random.uniform??, the code implementing the function will also be displayed.

2.6.3. API Documentation

For further details on the API details check the MXNet website at http://mxnet.apache.org/. You can find the details under the appropriate headings (also for programming languages other than Python).

2.6.4. Exercise

Look up ones_like and autograd in the API documentation.

2.6.5. Scan the QR Code to Discuss