Finding dimensions of a keras intermediate expression
<p>I am implementing a custom keras layer.
The call method in my class is as follows.</p>

<pre><code>  def call(self, inputs, mask=None):
    if type(inputs) is not list or len(inputs) &lt;= 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
</code></pre>

<p>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.</p>

<p>If you are using the theano backend, you can define Theano functions. (<a href="https://github.com/fchollet/keras/issues/41" rel="nofollow">like FranÃ§ois suggested</a>)</p>

<p>E.g. </p>

<pre class="lang-py prettyprint-override"><code>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))
</code></pre>

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

<pre class="lang-py prettyprint-override"><code>  def call(self, inputs, mask=None):
    if type(inputs) is not list or len(inputs) &lt;= 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))
</code></pre>

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

python keras keras-layer