I am trying to create a custom layer to use in CNTK using the python interface. I am following this guide, but keep having a TypeError exception thrown right in the __init__
of my class. Note that I just copy-pasted the example in the linked guide.
import cntk as C
import numpy as np
class MySigmoid(UserFunction):
def __init__(self, arg, name='MySigmoid'):
super(MySigmoid, self).__init__([arg], name=name)
def forward(self, argument, device=None, outputs_to_retain=None):
sigmoid_x = 1 / (1 + np.exp(-argument))
return sigmoid_x, sigmoid_x
def backward(self, state, root_gradients):
sigmoid_x = state
return root_gradients * sigmoid_x * (1 - sigmoid_x)
def infer_outputs(self):
return [output_variable(self.inputs[0].shape, self.inputs[0].dtype,
self.inputs[0].dynamic_axes)]
@staticmethod
def deserialize(inputs, name, state):
return MySigmoid(inputs[0], name)
model = C.layers.Sequential(C.layers.Dense(10), C.user_function(layers_extensions.MySigmoid(3)))
And this is the error I get:
File "...\layers_extensions.py", line 30, in __init__
super(MySigmoid, self).__init__([arg], name=name)
File "c:\repos\cntk\bindings\python\cntk\ops\functions.py", line 1286, in __init__
super(UserFunction, self).__init__(inputs, name)
File "c:\repos\cntk\bindings\python\cntk\ops\functions.py", line 109, in __init__
super(Function, self).__init__(*args, **kwargs)
File "c:\repos\cntk\bindings\python\cntk\cntk_py.py", line 1698, in __init__
this = _cntk_py.new_Function(_self, *args)
TypeError: cannot convert list element to CNTK::Variable
I tried to google this error but nothing comes up. Can you help me?
For some reason, CNTK is passing the argument
parameter in the forward(...)
method as a list, even if it is a single parameter. I ended up making it work by taking the first from the list. You will find the working example here.