Tf numpy usage with Keras Functional API


Can we use tf.experimental.numpy with Keras Functional API?

When I create the following code:

inp1 = keras.layers.Input(shape=(1,), name=“inp1”)
inp2 = keras.layers.Input(shape=(1,), name=“inp2”)

out = tf.experimental.numpy.add(inp1, inp2)

m = keras.Model([inp1, inp2], out)

I get the following error:
Cannot interpret '<KerasTensor: shape=(None, 1) dtype=float32 (created by layer 'inp1')>' as a data type

Whereas when I use out = tf.math.add(inp1, inp2), there is no issue.

What could be the problem please?

PS: The use case that I have is to calculate the mean of a tensor ignoring all nan values. That’s why I was trying to use tf.experimental.numpy.nanmean which gave the same error.

Fadi Badine

1 Like

This works if you wrap it in a keras Lambda layer:

def add_numpy(vals):
  x, y = vals
  return tf.experimental.numpy.add(x, y)

inp1 = tf.keras.layers.Input(shape=(1,), name="inp1")
inp2 = tf.keras.layers.Input(shape=(1,), name="inp2")

out = tf.keras.layers.Lambda(add_numpy)((inp1, inp2))

m = tf.keras.Model([inp1, inp2], out)

m([tf.constant(1.), tf.constant(2.)])

AFAIK the result of calling a Keras functional layer is a KerasTensor which I think is just a placeholder tensor with certain type/shape information to propagate to other layers. The NumPy API probably doesn’t recognize that right now, so this workaround defers the NumPy calls so they work on actual objects they can recognize.


Thanks @Sean_Moriarity!