Hacker News new | ask | show | jobs
by karjudev 1646 days ago
Can I define a deep neural network as an Awkward array of matrices of different sizes? It looks very promising to compute a backpropagation step without ping-ponging with the Python interpreter.
1 comments

I hadn't considered an Awkward array being used as a neural network, and I would expect specialized neural network frameworks to optimize it better: don't they fuse the matrices representing each layer into a single outside-of-Python computation? If they don't, I wonder why not.

Nevertheless, my ears perked up at "matrices of different sizes," since that was something I implemented a while back thinking it would find a use-case if it were well advertised.

You can matrix-multiply two arrays of matrices, in which all the matrices have different shapes, as long as matrix "i" in the first array is compatible with matrix "i" in the second array. Like this:

    import awkward1 as ak

    lefts = ak.Array([
        [[1, 2],
         [3, 4],
         [5, 6]],

        [[1, 2, 3, 4],
         [5, 6, 7, 8]],

        [[1],
         [2],
         [3],
         [4]],
    ])

    rights = ak.Array([
        [[7, 8, 9],
         [10, 11, 12]],

        [[8, 10],
         [11, 12],
         [13, 14],
         [15, 16]],

        [[5, 6, 7]],
    ])
Matrix-multiplying them results in this:

    >>> (lefts @ rights).tolist()
    [
        [[ 27,  30,  33],
         [ 61,  68,  75],
         [ 95, 106, 117]],

        [[129, 140],
         [317, 348]],

        [[ 5,  6,  7],
         [10, 12, 14],
         [15, 18, 21],
         [20, 24, 28]]
    ]
The left[0]'s 3×2 times right[0]'s 2×3 results in the output's 3×3, etc for i=1 and i=2. This is the kind of operation you wouldn't even be able to talk about if you didn't have jagged arrays.
I meant to add that autodiff is a feature we'll be working on this upcoming spring, by using JAX arrays as the buffers. Until we have autodiff, backpropagation won't be possible.