0

I am implementing a custom keras layer. The call method in my class is as follows.

  def call(self, inputs, mask=None):
    if type(inputs) is not list or len(inputs) <= 1:
      raise Exception('Merge must be called on a list of tensors '
                      '(at least 2). Got: ' + str(inputs))
    e1 = inputs[0]
    e2 = inputs[1]
    f = K.transpose((K.batch_dot(e1, K.dot(e2, self.W), axes=1))) #Removing K.transpose also works, why?
    return f

I verfied and the code works but I am trying to find ways to better debug when implementing a custom layer in keras. Assuming e1 and e2 are (batch_size * d) and W is (d*d) How can I find the dimensions of each subpart of my expression? For eg. K.dot(e2, self.W), the result of batch_dot etc.

Apurv
  • 4,458
  • 2
  • 21
  • 31

1 Answers1

1

If you are using the theano backend, you can define Theano functions. (like François suggested)

E.g.

import theano
from keras import layers

input = layers.Input(params)
layer = YourLayer(params)
output = layer(input)

debug_fn = theano.function([input], output)
print(debug_fn(numpy_array))

If you want intermediate results I usually just return them temporarily, like this for example:

  def call(self, inputs, mask=None):
    if type(inputs) is not list or len(inputs) <= 1:
      raise Exception('Merge must be called on a list of tensors '
                      '(at least 2). Got: ' + str(inputs))
    e1 = inputs[0]
    e2 = inputs[1]
    f = K.transpose((K.batch_dot(e1, K.dot(e2, self.W), axes=1))) #Removing     K.transpose also works, why?
    return f, e1


import theano
from keras import layers

input = layers.Input(params)
layer = YourLayer(params)
output, e1 = layer(input)

debug_fn = theano.function([input], e1)
print(debug_fn(numpy_array))

I do not know if there are better practices, but it works quite well for me.

Tivaro
  • 177
  • 12
  • Unfortunately I don't. Seems that there should be [similar functionality](http://stackoverflow.com/questions/35366970/theano-function-equivalent-in-tensorflow) in tensorflow though! – Tivaro Oct 05 '16 at 16:33
  • Did you check out [this post](http://stackoverflow.com/questions/37221621/how-to-turn-entire-keras-model-into-theano-function?rq=1)? – Tivaro Oct 13 '16 at 00:38