versilov
Matrex - A blazing fast matrix library for Elixir/Erlang with C implementation using CBLAS (Machine Learning)
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foundation.
The name is Matrex and it’s super-fast (compared to pure Elixir implementations) matrix manipulation lib.
Critical code is written in C using CBLAS subroutines and linked as Erlang NIFs.
It’s about 50-5000 times faster, than pure Elixir.
In the repo you will find MathLab fmincg() ported to Elixir with the help of the library
and logistic regression MNIST digits recognition exercise from Andrew Ng’s ML course implemented in Elixir (15 times faster, than Octave implentation).
It can be used like this:
y = Matrex.load("y.mtx")
j =
y
|> Matrex.dot_tn(Matrex.apply(h, :log), -1)
|> Matrex.substract(
Matrex.dot_tn(
Matrex.substract(1, y),
Matrex.apply(Matrex.substract(1, h), :log)
)
)
|> Matrex.scalar()
|> (fn
NaN -> NaN
x -> x / m + regularization
end).()
Or like this:
import Matrex.Operators
h = sigmoid(x * theta)
l = ones(size(theta)) |> set(1, 1, 0.0)
j = (-t(y) * log(h) - t(1 - y) * log(1 - h) + lambda / 2 * t(l) * pow2(theta)) / m
I’ve also created a Jupyter notebook with logistic regression algorithm in Elixir built with the help of this library.
Please, check Matrex on GitHub,
take a look at Matrex hex docs,
and tell me what you think of it.
Most Liked
versilov
Compared Matrex performance to NumPy. Looks like we are going more or less on par, with Matrex being two times faster on element-wise operations and a bit slower on dot product.
2015 MacBook Pro, 2.2 GHz Core i7, 16 GB RAM
Operations are performed on 3000×3000 matrices filled with random numbers.
You can run benchmarks from the /bench folder with python numpy_bench.py and MIX_ENV=bench mix bench commands.
NumPy
benchmark iterations average time
np.divide(A, B) 30 15.43 ms/op
np.add(A, B) 100 14.62 ms/op
sigmoid(A) 50 93.28 ms/op
np.dot(A, B) 10 196.57 ms/op
Matrex
benchmark iterations average time
divide(A, B) 200 7.32 ms/op (~ 2× faster)
add(A, B) 200 7.71 ms/op (~ 2× faster)
sigmoid(A) 20 71.47 ms/op (23% faster)
dot(A, B) 10 213.31 ms/op (8% slower)
versilov
Thanks for all your praise!
As we move towards version 1.0, here come new features:
-
Heatmap of the matrix with Matrex.heatmap/3
This one is the coolest. Now you can monitor learning process and get insights from your matrices right inside terminal. Just have a look:
-
reshape/3 and concat/2. Fast and convenient matrix creation from enumerable of elements or other matrices.
Here’s how you can load MNIST digits dataset and show a subset of the data with one line of code:
-
Other minor additions: normalize/1, resize/2, min_finite/1, max_finite/1, to_row/1, to_columns/1, list_of_rows/2.
Would be glad to hear your feedback, bug reports and ideas.
versilov
Thank you!
Yes, I think what you’ve described is the second library we desperately need in Elixir to make it shine in machine learning.
Have a look at these two repos, cuda and neuro: sirin-tech · GitHub
They use kernels, defined in PTX ISA internal assembly language, to feed them into NVIDIA CUDA interface.
The only drawback is that they have zero documentation, as far as I know.
I plan to contact these guys and may be visit them in person to unite development efforts. I hope, these libs
need only good docs and some polishing, so we can get the next piece of Elixir ML puzzle faster:)
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance














