Meet Clojure MXNet - NDArray
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 is the tensor data structure in MXNet. Let’s start of by creating one. First we need to require the
Now let’s create an all zero array of dimension 100 x 50
We can check the shape of this by using
There is also a quick way to create an ndarray of ones with the
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:
To convert it back to a vector format, we can use the
Now that we know how to create NDArrays, we can get to do something interesting like operations on them.
There are all the standard arithmetic operations:
1 2 3 4
Note that the original ndarrays are unchanged.
But, we can change that if we use the inplace operators:
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:
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.
When creating an
ndarray you can use a context argument to specify the device. To do this, we will need the help of the
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).
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.
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 very alpha state, so if you encounter any problems or have any other feedback, please log an issue so bugs and rough edges can be fixed :).