1

I would like to implement an custom LSTM cell as in a keras layer. Actually this implementation exists in tensorflow, so I was wondering if it is possible to just wrap it as an keras layer and call it in the model.

I found official documentation too simplified to see how to build a custom RNN layer. There are similar questions here and here, but they seem unresolved.

Thanks in advance for your help !

yiyang
  • 75
  • 7

2 Answers2

0

From my understanding, you should just be able to initialize the cell in the init() of the class layer, and then inside the call method reference it with your input.

Ex:

class MySimpleLayer(Layer):
  def __init__(self, lstm_size):
    super(MySimpleLayer, self).__init__()
    self.lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)

  def call(self, batch, state):
    return self.lstm(batch, state)

layer = MySimpleLayer(lstm_size)
logits = layer(batch, state)

This implementation is as basic as it gets though, so you might need to look into the build() and compute_output_shape() methods for more complex use cases.

Naomi
  • 713
  • 1
  • 6
  • 17
  • I am sorry but `call` such defined does not go together with `Layer`; I got TypeError: call() missing 1 required positional argument: 'states' – yiyang Dec 19 '18 at 15:35
  • Call() definitely goes with the Layer class, as specified [here](https://keras.io/layers/writing-your-own-keras-layers/). It looks like an implementation error to me. Try passing [batch, state] as a single list input to call(). – Naomi Dec 20 '18 at 16:47
0

Now the documentation of tensorflow may have improved since the question is posted.

You may want to check this guide or this SO answer for reference.

carusyte
  • 1,549
  • 17
  • 21