deepgram / kur

Descriptive Deep Learning
Apache License 2.0
814 stars 107 forks source link

Including keras Conv2DTranspose #38

Closed josh-gree closed 7 years ago

josh-gree commented 7 years ago

Hi,

So is it a difficult endeavour to add new layers to the yml specification? I have had a brief look at the code for the dense layer and will have a go at trying to implement myself but any hints/pointers would be very helpful! Specifically I really would like to have access to transposed convolutions...https://keras.io/layers/convolutional/#conv2dtranspose perhaps they are already available?

Thanks

Josh

ajsyp commented 7 years ago

It's actually pretty simple! I'd recommend taking a look at how the convolution layer is implemented. There are a few steps:

  1. __init__(). Just set initial/default values for your layer's parameters.
  2. _parse(). This will get called when the Kurfile is being parsed, and all arguments to your layer are available in self.args. You can parse/validate arguments here, but do not call into any deep learning libraries (e.g., Keras), because this can have side-effects. The easiest thing is to require that self.args is a dictionary, and then you can parse out all parameters.
  3. _build(). This is where you yield each layer in turn. Exactly what gets yielded is backend-specific (for Keras, it is the layers themselves; for PyTorch, it is a function which will attach themodules). It is complicated by the fact that, as of Keras 2.0 (yesterday), the Keras API has changed. I want to continue supporting v2.0 as well as v1.x for a little longer, so you'll need to take a look at the v1.2.2 documentation to make sure that the layer properly. This should be really easy for your case, since Deconvolutions (same thing as ConvTranspose) were availabe in Keras 1.
  4. Then, make sure you import your layer in kur/containers/layers/__init__.py so that it will be seen by Kur.
  5. Write a unit test for it to make sure it works for all combinations of Keras/Theano, Keras/TensorFlow, PyTorch! It's pretty easy: take a look at the simple_model or embedded_model example in tests/conftest.py. This can be as simple as trying to compile a model that uses your layer (again, just grep around for the embedded example); it is very little work.
josh-gree commented 7 years ago

Thanks for the pointers have managed to implement it successfully. Only a few modifications needed from convolution layer...

Have not implemented for pytorch or keras v1 but if this would be something you would like as a pull request I could try and do so? I am however unable to find the keras v1 documentation, any ideas where it is?

ajsyp commented 7 years ago

Awesome! Did you feel it was pretty straightforward? If you have suggestions, please let me know.

I think that it would be great to see a pull request come out of this. The Keras v1 documentation is most easily generated by:

If you need support for this, let me know.

josh-gree commented 7 years ago

Yeah it was easy peasy! I will try and get it finished over the next few days...

ajsyp commented 7 years ago

In that case, I'll close the issue and then look forward to a pull request! If you have additional questions regarding implementation details, unit tests, PyTorch, etc., feel free to hit me up on our Gitter.