Introduction to the MXNet R package

We will use the package MXNet R to build our neural networks. It implements state-of-the-art deep learning algorithms and enables efficient GPU computing. We can work in our familiar R environment and at the same time harness the power of the GPUs (though access to GPU is available through the Python API now, we still need to wait for it to be available for R). It will be useful to give you a small overview about the basic building blocks of MXNet before we start using it for our time series predictions.

Refer to the https://github.com/apache/incubator-mxnet/tree/master/R package for more details on the MXNet R package.

In MXNet, NDArray is the basic operation unit. It's a vectorized operation unit for matrix and tensor computations. All operations on this operation unit can be run on either the CPU or GPUs. The most important point is that all these operations are parallel. It's the basic data structure for manipulating and playing around with data in MXNet. It supports a wide range of mathematical operations, allowing for the writing of machine learning programs in an imperative fashion. Let us look at some basic NDArray operations.

The following code demonstrates the creation of matrices:

> library(mxnet)
>
> zero.matrix <- mx.nd.zeros(c(3,3))
> zero.matrix
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
> ones.matrix <- mx.nd.ones(c(3,3))
> ones.matrix
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 1
[3,] 1 1 1

We have created two matrices of size 3 x 3, one filled with zeros and the other filled with ones.

The context defines where the code is executed, either in the CPU or GPU. Let us look at the context:

> mx.ctx.default()
$device
[1] "cpu"

$device_id
[1] 0

$device_typeid
[1] 1

attr(,"class")
[1] "MXContext"

As my machine does not include a GPU, the only context available is CPU.

To create an array in a specified context, we use the following:

zero.matrix.gup <- mx.nd.zeros(c(3,3), mx.gpu(0))

We pass the context while creating the arrays. Here, we have passed the GPU context, specifying that we need this 3 x 3 zero filled array to be created in the GPU.

Data can be copied between contexts/devices as shown in the following code:

zero.copy <- mx.nd.copyto(zero.matrix, mx.cpu())

We copy the data to the CPU by passing the GPU context.

Let us see some matrix operations and sampling:

> mx.nd.dot(ones.matrix, ones.matrix)
[,1] [,2] [,3]
[1,] 3 3 3
[2,] 3 3 3
[3,] 3 3 3
> mx.nd.elemwise.add(ones.matrix,ones.matrix)
[,1] [,2] [,3]
[1,] 2 2 2
[2,] 2 2 2
[3,] 2 2 2

The code demonstrates matrix multiplication and element-wise addition in the matrix.

Let us look at some sampling:

> mu <- mx.nd.array(c(0.0,2.5))
> sigma <- mx.nd.array(c(1,3))
> mx.nd.sample.normal(mu = mu, sigma = sigma)
[1] 0.5613724 0.4520042
>

The code generates data from a normal distribution for a given mean and standard deviation.

We previously saw the imperative operations on NDArrays. MXNet supports symbolic inferences through its symbolic API. In symbolic programming, the operations are not executed in a sequential manner. The first step is to define a computation graph. This graph contains placeholders for input and output. Compiling this graph provides a function. This function can be bound to the NDArrays and executed. The biggest advantage of using a symbolic graph is that functions can be optimized for execution before they are run.

More on the MXNet symbolic API can be found at https://mxnet.incubator.apache.org/tutorials/basic/symbol.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset