transcranial / keras-js

Run Keras models in the browser, with GPU support using WebGL
https://transcranial.github.io/keras-js
MIT License
4.96k stars 501 forks source link

"shared_axes" option for PReLU #81

Open auduno opened 7 years ago

auduno commented 7 years ago

Hi,

it seems the option shared_axes for PReLU activations (https://keras.io/layers/advanced-activations/#prelu) is not supported in Keras-js. In my case I made a simple hack around this by modifying the code here:

import tile from 'ndarray-tile'

...

_call_cpu(x) {
    this.output = x
    let alpha_tiled;
    let tileAlphas = false;
    let tiling = [];

    for (let i = 0;i < x.tensor.shape.length;i++) {
        if (x.tensor.shape[i] != this.weights['alpha'].tensor.shape[i]) {
            tileAlphas = true;
            tiling.push(x.tensor.shape[i]);
        } else {
            tiling.push(1);
        }
    }
    if (tileAlphas) {
        alpha_tiled = tile(this.weights['alpha'].tensor, tiling);
    } else {
        alpha_tiled = this.weights['alpha'].tensor;
    }

    this._compute(this.output.tensor, alpha_tiled);
}

For _call_gpu I actually didn't need to change anything, as these work fine with the "shared axis"-dimensions set to one pixel (effectively sharing weights across that dimension).

I could try to do something slightly more general than this and submit a PR if you're interested. I noticed there seemed to be a lot of changes in the code recently, though, so wasn't sure whether there were already plans underway to modify the existing code or fix this at a later stage.

seranus commented 6 years ago

having the same problem.

@auduno in you code tile is not defined

Can't load weights if gpu is set

edit: nvm missed top line