Wrap general python function in tensorflow
My goal is to create a custom layer in keras with a weight matrix that is larger than the input (output size is the same), while additional parameters are used to handle the function (weight %%%). The function is NOT written in tensorflow and it would be quite tedious to do so, so I tried using tensorflow py_func. However, NN aborts with "ValueError: None values not supported".
The master code for my own layer is Dense with a few tweaks. W - matrix of weights with dimension [input_dim + n, output_dim]. Suppose there is a generic function "func" with n parameters, and it should be evaluated on (W% *% Input).
def func(self,output):
do something
return something
We now wrap this function into a call:
def call(self, x, mask=None):
output = K.dot(x, self.W[self.n:,:])
if self.use_bias:
output = K.bias_add(output, self.bias)
if self.activation is not None:
output = tf.reshape(tf.py_func(self.func,[output],tf.float32),(-1,self.units))
output = self.activation(output)
return output
The "No" error occurs in self.activation. py_func works flawlessly when using its own session, but it looks like this nesting in the Keras layer won't do the expected result. It also fails if func(x)=x*x
.
source to share
No one has answered this question yet
Check out similar questions: