2017-06-15 16 views
1

を構成するためにどのように私はここでの説明以下、CustomFuncと呼ばれるカスタム関数を作成しました:https://www.cntk.ai/pythondocs/extend.htmlカスタム関数CNTK

記事で示唆したように、私はそれを使用している場合は、それが動作:

model = cntk.user_function(CustomFunc(prev_node)) 

この作品このモデルは問題なく動作します。私の問題は、この関数をcntk.layers.Sequential呼び出しの内側とcntk.layers.Recurrence呼び出しの中で使いたいということです。これを行うために、私は何とか私の関数の構成を別のものと組み立てて、それをSequential呼び出しまたはRecurrence呼び出しの中に入れる必要があります。

customFunToUse = cntk.user_function(CustomFunc(cntk.placeholder(), otherInputs)) 
model = cntk.layers.Sequential([cntk.layers.Dense(100), 
           customFunToUse, 
           cntk.layers.Recurrence(
           customFunToUse >> cntk.layers.LSTM(100))]) 

しかし、これは動作しませんし、エラーのすべての種類が発生します:今、私はいくつかのプレースホルダを使用しているもの、すなわち私があるん時にはそれがセグメンテーション違反が他の同様のモデルで

"ValueError: Cannot create an NDArrayView using a view shape '[? x 10]' that has unknown dimensions for any of its axes." 
あり、です偶然に任意の量を考えると、それは同じ量と種類を返します。

他の回の代わりに

Evaluate: All nodes inside a recurrent loop must have a layout that is identical; mismatch found for nodes ... 

注意は私のカスタム関数は、入力寸法を変更していないこともあります。コードはこれです:

class CustomFun(UserFunction): 
    def __init__(self, *args, otherStuff, name='CustomFun'): 
     super(CustomFun, self).__init__(list(args), name=name) 
     self.otherStuff = otherStuff 

    def forward(self, arguments, outputs=None, keep_for_backward=None, device=None, as_numpy=True): 
     return None,[x/2 for x in arguments] 

    def backward(self, state, root_gradients, variables=None, as_numpy=True): 
     #it's not important right now, just a test... 
     return root_gradient 

    def infer_outputs(self): 
     #shape, type and dynamic axes of inputs are not changed by this function 

     outputVar = [output_variable(self.inputs[idx].shape, self.inputs[idx].dtype, 
      self.inputs[idx].dynamic_axes, name='out_quantLayer') for idx in range(len(self.inputs))] 
     return outputVar 

    def serialize(self): 
     return {'otherStuff': self.otherStuff} 

    @staticmethod 
    def deserialize(inputs, name, state): 
     return CustomFun(inputs, otherStuff=state['otherStuff'], name=name) 

答えて

1

正しい方法はこの def my_layer(x): @C.Function def apply(x): return cntk.user_function(CustomFunc(x)) return apply ような何かを書くことになります残念ながら、これはクラッシュに私のPythonインタプリタを引き起こしているようです。私はこれにgithub issue 2132を開いた。問題が解決したらこの回答を更新しようとします。

更新:私たちがキャッチしなかった小さなタイプミスがありました。 githubの問題ページに解決策があります。