2016-10-03 2 views
0

私はカスタムケラス層を実装しています。 私のクラスの呼び出しメソッドは以下の通りです。ケラス中間表現の寸法の検索

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とe2を(batch_size * d)、Wを(d * d)と仮定すると、 私の式の各部分の次元はどのようにして求められますか? たとえば、バッチドットなどの結果であるK.dot(e2、self.W)

答えて

1

Theanoバックエンドを使用している場合は、Theano関数を定義できます。 (like François suggested

など。

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)) 

あなたは、私は通常は例えば次のように、一時的にそれらを返す中間結果をしたい場合:

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)) 

より良い慣行がある場合、私は知りませんが、それは私のために非常に適しています。

+0

テンソルフローの同様のトリックを知っていますか? – Apurv

+0

残念ながら私はしません。しかし、テンソルフローで[同様の機能](http://stackoverflow.com/questions/35366970/theano-function-equivalent-in-tensorflow)があるはずです! – Tivaro

+0

[this post](http://stackoverflow.com/questions/37221621/how-to-turn-entire-keras-model-into-theano-function?rq=1)をチェックしましたか? – Tivaro