0

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.

1 Answer 1

1

You may need to specify the output shape of the Lambda layer z, the tf.py_function would give None as output shape which does not sit well with the Dense layer that follows it. You should try:

z = Lambda(my_lambda_func)(x)

z.set_shape(x.shape)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.