I would like to wrap numpy codes using tf.py_fucn in a customized lambda layer using keras.
Notice: only for simplicity I'm showing a simple np.power function.
Here is what I've done
def my_func(x):
return np.power(x, 2)
def my_lambda_func(x):
return tf.py_function(my_func, [x], tf.float32)
def model():
inp = Input(shape=(2,))
x = Dense(128)(inp)
x = Dense(128)(x)
z = Lambda(my_lambda_func)(x)
output = Dense(1)(z)
model = Model(inputs=inp, outputs=output)
return model
model = model ()
model.compile(optimizer='adam', loss='mse')
Then I get this error
ValueError Traceback (most recent call last) in () 21 22 ---> 23 model = model () 24 model.compile(optimizer='adam', loss='mse')
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py in build(self, input_shape)
1179 last_dim = tensor_shape.dimension_value(input_shape[-1])
1180 if last_dim is None:
-> 1181 raise ValueError('The last dimension of the inputs to Dense '
1182 'should be defined. Found None.')
1183 self.input_spec = InputSpec(min_ndim=2, axes={-1: last_dim})
ValueError: The last dimension of the inputs to Dense should be defined. Found None.