versilov

versilov

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.

Showing Posts 1 to 10

chrismcg

chrismcg

This is great to see, nice work!

grych

grych

Creator of Drab

Man, this is so great!

michalmuskala

michalmuskala

This looks really good!

I wonder about the decision of keeping everything in a binary with sizes in front vs having a C struct and a nif resource. Wouldn’t the later be easier to handle and possibly also slightly faster on some operations?

versilov

versilov OP

Michal,

I considered using NIF resource, but decided to stay with explicit binary for 3 reasons:

  1. I don’t want to add unnecessary NIFs, when Elixir gives the same speed or operation is not time-critical.
  2. Pattern matching for simple operations, like getting the size of the matirx.
  3. There is an idea to implement Elixir versions of all the NIFs, so they would
    work as fallback functions that work everywhere. E.g., when NIFs would fail to compile for some reasons. I don’t know if it’s worth the effort, but don’t want to cut off such possibilities.

And NIF resource leaves us only with , AFAIK?

%Matrex{} struct now works mostly for the purpose of implementing protocols and behaviours (Inspect, Enum and Access). It gives the opportunity to add more fields later, to distinguish matrices with
different element types and sizes, different matrix shapes and so on.

So, I believe, the current setup gives greater flexibility in future development.
But I did not dig deep into NIF resources, so, maybe I am missing something.

michalmuskala

michalmuskala

I guess you’re right.

Another approach I could see would be to have columns and rows directly in the struct. What do you think? This could allow users to do matrix.rows and matrix.cols or similar. You could possibly unpack those in the elixir layer and call the NIFs with 3 arguments instead of one.
I could imagine then the “data” field could be either a binary or a NIF resource depending on what’s more useful and the Elixir code would treat it as completely opaque.

versilov

versilov OP

Yeah, thought about it also, but postponed for two reasons:

  1. Did not want to hinder library release by rewriting the codebase I inherited from exlearn.
  2. It gives only more clean access to some properties (matrix.rows instead of matrix[:rows]),
    not the highest priority feature for now.

I definitely plan to experiment with this approach in the future to see how it would work in real life.

Thanks for the thorough feedback, I am amazed with the Elixir community:)

OvermindDL1

OvermindDL1

This does look very awesome!

Have you thought about adding a kernel builder interface? I.E. you’d build up a command queue then ‘submit’ it to the BLAS backend to perform it all as quickly as possible (some have JIT-style engines that can take such a command queue and run it as optimized assembly over the dataset, potentially even to other hardware optimized for the purpose).

versilov

versilov OP

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:)

PragTob

PragTob

This looks great, thank you very much for your work! :slight_smile:

versilov

versilov OP

Thanks for all your praise!

As we move towards version 1.0, here come new features:

Would be glad to hear your feedback, bug reports and ideas.

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews