Keras layer with int inputs cannot be built

Viewed 4072

I have a complex keras model in which one of the layers is a custom pretrained layer which expects "int32" as inputs. This model is implemented as a class that inherits from Model and it is implemented like this:

class MyModel(tf.keras.models.Model):

    def __init__(self, size, input_shape):
        super(MyModel, self).__init__()
        self.layer = My_Layer()
        self.build(input_shape)

    def call(self, inputs):
        return self.layer(inputs)

But when it reaches the self.build method, it throws the next error:

ValueError: You cannot build your model by calling `build` if your layers do not support float type inputs. Instead, in order to instantiate and build your model, `call` your model on real tensor data (of the correct dtype).

How can I fix it?

1 Answers

The exception is thrown when building a model with model.build.

model.build function build a model based on given input shape.

The error is raised because when we trying to build a model, it first calls a model with x argument depending on input shape type in the following code

if (isinstance(input_shape, list) and
    all(d is None or isinstance(d, int) for d in input_shape)):
  input_shape = tuple(input_shape)
if isinstance(input_shape, list):
  x = [base_layer_utils.generate_placeholders_from_shape(shape)
        for shape in input_shape]
elif isinstance(input_shape, dict):
  x = {
      k: base_layer_utils.generate_placeholders_from_shape(shape)
      for k, shape in input_shape.items()
  }
else:
  x = base_layer_utils.generate_placeholders_from_shape(input_shape)

x is a TensorFlow placeholder here. So when trying to call a model with x as an input it will pop a TypeError and the result except for block will work and give an error.

I assume your input shape is 16x16. Instead of using self.build([(16,16)]) this, call the model based on real tensor

inputs = tf.keras.Input(shape=(16,))
self.call(inputs)

Related