2017-10-30 7 views
0

私はTheanoで定義されたカスタム層を持っています。私はKerasモデルでそれらを使用したいと思います。これどうやってするの? Theanoでこれらの層(クラスとして定義されている)は特定の形式に従わなければなりませんか?KerasでカスタムのTheanoレイヤーを使用するには?

これのためのリソースが見つかりませんでした。誰かが私を導くことができれば、非常に役に立ちます。

答えて

0

ピュア操作:

これらの層は、純粋な操作であれば、あなたはkeras Lambda層を使用することができます。

アイデアは1つのテンソル(またはテンソルのリストを)取って関数を作成し、この関数内ですべての操作を行うことです。

def customFunc(x): 

    #tensor operations with the input tensor x 
    #you can use either keras.backend functions or theano functions  
    #paste the theano functions here 

    #you can also attempt to call the theano layer here, passing x as input 

    return result 

その後、あなたは、この関数からラムダ層を作成します。

model.add(Lambda(customFunc, output_shape=someShape)) 
をトレーニング可能な重みを持つ

レイヤー:

層がトレーニング可能な重みを持っている場合は、しかし、あなたが012を作成する必要があります。

それはあなたがbuild方法で重みを定義し、call方法で操作を実行するクラスです:

class MyLayer(Layer): 

    def __init__(self, yourOwnParameters, **kwargs): 
     self.yourOwnParameters = yourOwnParameters 
     super(MyLayer, self).__init__(**kwargs) 

    def build(self, input_shape): 
     # Create a trainable weight variable for this layer. 
     self.kernel = self.add_weight(name='kernel', 
            shape=someKernelShape, 
            initializer='uniform', 
            trainable=True) 

     #because of self.add_weight call: 
     #I'm not sure if you can use the theano layer unchanged 

     super(MyLayer, self).build(input_shape) # Be sure to call this somewhere! 

    def call(self, x): 

     #paste the theano operations here 
     return resultFromOperationsWith(x) 

    def compute_output_shape(self, input_shape): 
     return calculateSomeOutputShapeFromTheInputShape() 
関連する問題