This module integrates the efficient libraries created by really smart folks into the rich javascript environment provided by nodejs.
At present this is in alpha mode. It's tested in real ML apps and holds up very well.
I need to make the install easier - sorry. Check the Docker file for linux prerequistes.
Of note:
- underlying data type is float (can we template this?)
- matrices are stored in column major order (for cuda compatibility)
- limited validation of inputs is present in this version (to be be improved)
- more non-blocking options ( e.g. svdp and pinvp )
- need anyone who can build on windows
- (polite) suggestions for matrix operations
- NVIDIA CUDA support - beta testing in progress (20kx20k mul() in 500ms!!!)
- caffe integration - let's get the caffe chaps' good work into nodejs :)
This library builds on the following. Great work chaps - so good it's worth stealing!
npm install lalg
BUT there are several prerequesites (see below) Promise to make this better, but I am still learning about software.
- nodejs of course ( tested on 7.2.0 )
- gyp - npm install -g node-gyp
- make
- python ( oh boy! - can't avoid python - even integrating C++ to javascript !!! )
- C++ compiler
- openblas
- lapacke
The Dockerfile shows the requirements in detail
docker run -i -t rcorbish/lalg
var lalg = require ( "lalg" ) ;
lalg.rand( 5 ) ;
Verify correct installation by: node node_modules/lalg/test/test.js
This is the C++ docs, which shows all the nodejs functions and a few examples. I'll keep this up to date as the unerlying code changes. NB. the javascript calls start with lowercase letters.
Create an uninitialized array (not too useful really)
var lalg = require('lalg');
var A = new lalg.Array( 10, 5 ) ;
Create an randomly initialized array
var lalg = require('lalg');
var A = lalg.rand( 10, 5 ) ;
Create two arrays one initialized to all 0.0, the other to all 1.0
var lalg = require('lalg');
var Zeros = lalg.zeros( 10, 5 ) ;
var Ones = lalg.ones( 10, 5 ) ;
Create the identity matrix ( identity is square, only need 1 arg )
var lalg = require('lalg');
var Identity5 = lalg.eye( 5 ) ;
Add a value to each element in the matrix Multiply each element in the matrix by a single value. NB add() also takes vectors and matrices as args too (see later).
var lalg = require('lalg');
var A = lalg.zeros( 5 ) ;
var T = A.add( 10 ) ; // now all elements in T are equal to 10
Multiply each element in the matrix by a single value. NB mul() also takes vectors and numbers (be careful with vectors, they must be compatible shapes)
var lalg = require('lalg');
var A = lalg.ones( 5 ) ;
var T = A.mul( 10 ) ; // now all elements in T are equal to 10
Multiply two matrices. This is the same call as the scalar version, but passing in a matrix or vector fires off the linear matrix multiplies
var lalg = require('lalg');
var A = lalg.rand( 5 ) ;
var B = lalg.rand( 5 ) ;
var T = A.mul( B ) ; // T = A x B - another 5x5 matrix
console..log( T ) ;
Other functions that may be useful:
- mean - calculate the mean of rows or columns
- sum - calculate the sum of elements in rows or columns
- norm - calculates the Euclidean norm of rows or columns
- inv - the matrix inverse
- pinv - the pseudo inverse, can calculate an inverse for non-square and singular matrices
- log - calculate the log of each element
- abs - absolute value of each element
- sqrt - the sqrt of each element
- svd - singular value decomp of a matrix - return U,S, Vt in once object
- pca - principal components analysis, reduces the dimension of a vector
- transpose - transpose a matyix
- dup - copy a matrix
Some functions of a matrix are provided to extract/add/move rows and columns
- rotate - rotate the columns in a matrix
- addColumn - adds a row vector to a matrix
- addRow - adds a column vector to a matrix
- removeRow - rfemoves a row vector from a matrix
- removeCoOlumn - removes a column from a matrix
- getRows - copies rows from a matrix to a new matrix
- getColumns - copies columns from a matrix to a new matrix
OK we'll try a more complex example. It showcases the non-blocking features of the library.
This implements the linear regression simple cals: theta = inv(X' X) X' y
where X' is a transpose operation ( thanks MATLAB )
This needs fast-csv npm install fast-csv
Click here for a sample. Linear regression as a first step.
Plot the results (in a spreadsheet for example) to see if we're accurate. Don't worry we'll do better with logistic regression.
This implements a solver to find the minimum value of a function. Given a matrix of features and a means to calculate a partial differential (gradients) finds the global minimum.
Click here for a sample.
See the docs for the requirements to define the gradient calculations.