New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eleMap Function to Matrix.prototype #8

Merged
merged 2 commits into from Jan 16, 2016

Conversation

Projects
None yet
2 participants
@samjabrahams
Collaborator

samjabrahams commented Oct 28, 2015

eleMap overview

eleMap is allows finer control over the transformations done to a matrix. Like map, eleMap takes in a callback function. However, in addition to the current element's value, eleMap also passes in its row and column position to the callback function. An eleMap callback declaration might look like this:

var cb = function(v, row, col) {
    switch (row) {
        case 0:
            // Perform transformation on first row
        case 1:
            // Perform transformation on second row
        default:
            // Perform transformation on all other rows
    }
}

Then we could apply this callback to a matrix, either in-place or returning a new matrix.

// In-place transformation
m.eleMap_(cb);

// Return a new matrix
var m2 = m.eleMap(cb);

eleMap functions identically to map when row and col are omitted.

Changes to existing files

  • This pull makes changes to three files. A build command has been added to src/mathtransforms.js to create both eleMap() and eleMap_().
  • In build/buildMacros.coffee, a new operation function, eleMathOp is added, as well as an additional case under module.exports to use it.
    • This is a somewhat ineloquent solution. Ideally, eleMap would be able to use the existing mathOp function. However, due to macroComps being split on commas (line 29 in new file, line 23 in old file), and the desired replacement for 'EXPR' is transformFn(thisData[row][col], row, col), it was deemed acceptable.
    • Theoretically, macroComps could be split on ', ' (note the space), and all the functions would work. However, not interfering with existing code was a goal of this merge.
  • Several tests were added to test/_commonTests.js

Tests

Six total tests were added in test/_commonTests.js. Three tests are conducted for both in-place and regular transformations- one testing row transformations, one testing column transformations, and one testing element-by-element transformations. These run alongside the other tests when the gulp command is used in the terminal.

Some applications

  • Applying different activation functions to different layers in a neural network
  • Zeroing out specific rows, columns or elements based on position
  • Basis of future convenience selector/application functions (something like matrix.row(3).apply(function());

samjabrahams added some commits Oct 27, 2015

Add eleMap function to Matrix.prototype
eleMap applies a transformation to each element in a matrix, given its
initial value, its row position, and its columnal position. This
transformation is given to eleMap in the form of a callback function
with parameters v, row, and col.

Typical use cases would be to perform different transformations to
different rows and/or columns. For example, multiply the first row by 2,
the second by 3, and the rest by four. Here is a callback that would
achieve this:

var callback = function(v, row, col) {
    switch (row) {
        case 0: return v * 2;
        case 1: return v * 3;
        default: return v * 4;
    }
}

We would then pass the callback into eleMap as you would into map.

matrix2 = matrix.eleMap(callback);
Add tests for eleMap to test/_commonTests.js
The tests included perform checks on row-by-row transformations,
column-by-column transformations, and element-by-element transformations
with eleMap. Additionally, it tests both the default and in-place
implementations of eleMap.
@hiddentao

This comment has been minimized.

Owner

hiddentao commented Jan 16, 2016

@samjabrahams This is really great work! I'll merge it in first and then can figure out how to refactor it to be better.

@hiddentao hiddentao merged commit fbcbdf7 into hiddentao:master Jan 16, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment