Skip to content

model.predict() gives different result to model() when using Discretization layer #20981

@georyetti

Description

@georyetti

When using the Discretization layer in tensorflow backend you get a different output when calling model.predict() as opposed to the layer itself or using the models call method directly.

Keras version: 3.2.1
Tensorflow version: 2.16.1
Numpy version: 1.23.5

Reproducible code

import tensorflow as tf
import keras

layer = keras.layers.Discretization(
    bin_boundaries=[-0.5, 0, 0.1, 0.2, 3],
    name="bucket",
    output_mode="int",
)

x = tf.constant([[0.0, 0.15, 0.21, 0.3], [0.0, 0.17, 0.451, 7.8]])
inputs = keras.layers.Input(name="inp", dtype="float32", shape=(4,))

model_output = layer(inputs)
model = keras.models.Model(inputs=[inputs], outputs=[model_output])
layer(x)
<tf.Tensor: shape=(2, 4), dtype=int64, numpy=
array([[2, 3, 4, 4],
       [2, 3, 4, 5]])>
model(x)
<tf.Tensor: shape=(2, 4), dtype=int64, numpy=
array([[2, 3, 4, 4],
       [2, 3, 4, 5]])>
model.predict(x)
array([[2, 2, 2, 2],
       [2, 2, 2, 5]])

I've checked with later keras versions (3.4.0) and the issue occurs.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions