Meet Clojure MXNet - NDArray
This is the beginning of a series of blog posts to get to know the Apache MXNet Deep Learning project and the new Clojure language binding clojure-package
MXNet is a first class, modern deep learning library that AWS has officially picked as its chosen library. It supports multiple languages on a first class basis and is incubating as an Apache project.
The motivation for creating a Clojure package is to be able to open the deep learning library to the Clojure ecosystem and build bridges for future development and innovation for the community. It provides all the needed tools including low level and high level apis, dynamic graphs, and things like GAN and natural language support.
So let’s get on with our introduction with one of the basic building blocks of MXNet, the NDArray
.
Meet NDArray
The NDArray
is the tensor data structure in MXNet. Let’s start of by creating one. First we need to require the ndarray
namespace:
1 2 |
|
Now let’s create an all zero array of dimension 100 x 50
1 2 |
|
We can check the shape of this by using shape-vec
1 2 |
|
There is also a quick way to create an ndarray of ones with the ones
function:
1
|
|
Ones and zeros are nice, but what an array with specific contents? There is an array
function for that. Specific the contents of the array first and the shape second:
1 2 |
|
To convert it back to a vector format, we can use the ->vec
function.
1 2 |
|
Now that we know how to create NDArrays, we can get to do something interesting like operations on them.
Operations
There are all the standard arithmetic operations:
1 2 3 4 |
|
Note that the original ndarrays are unchanged.
1 2 |
|
But, we can change that if we use the inplace operators:
1 2 |
|
There are many more operations, but just to give you a taste, we’ll take a look a the dot
product operation:
1 2 3 4 5 |
|
If you are curious about the other operators available in NDArray API check out the MXNet project documentation page
Now that we have ndarrays and can do calculations on them, we might want to save and load them.
Saving and Loading
You can save ndarrays with a name as a map like:
1
|
|
To load them, you just specify the filename and the map is returned.
1 2 3 |
|
One more cool thing, we can even due our operations on the cpu or gpu.
Multi-Device Support
When creating an ndarray
you can use a context argument to specify the device. To do this, we will need the help of the context
namespace.
1
|
|
By default, the ndarray
is created on the cpu context.
1 2 3 |
|
But we can specify the gpu instead, (if we have a gpu enabled build).
1
|
|
Note: Operations among different contexts are currently not allowed, but there is a copy-to
function that can help copy the content from one device to another and then continue on with the computation.
Wrap up
I hope you’ve enjoyed the brief introduction to the MXNet library, there is much more to explore in future posts. If you are interested in giving it a try, there are native jars for OSX cpu and Linux cpu/gpu available and the code for the ndarray tutorial can be found here
Please remember that the library is in a experimential state, so if you encounter any problems or have any other feedback, please log an issue so bugs and rough edges can be fixed :).