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 503 forks source link

Can we use custom layers? #113

Closed seranus closed 6 years ago

seranus commented 6 years ago

Is there a way to implement custom layers or if not a ETA on the feature?

linusmartensson commented 6 years ago

I tried, and it's quite easy, though absolutely not officially supported. Before you load your model, add elements to KerasJS.layers:

require("keras-js").layers["LayerName"]=(class LayerName ... {constructor(attrs){...} call(x){return x;}});

x is a tensor, attrs is some initialization parameters. Refer to other Layers for weight loading et al.

Most layers are fairly simple, so it should be easy to get the hang of, though it might be a bit fiddly to inherit from the base Layer straight up depending on where and how you're using it.

seranus commented 6 years ago

yeah, I managed to write a CPU version of it, stuck on gpu.

I have to reshape -> permute -> reshape.

I not sure do I have create the index map before every step or just once at the process start