tensorflow / addons

Useful extra functionality for TensorFlow 2.x maintained by SIG-addons
Apache License 2.0
1.69k stars 610 forks source link

RSquare does not work with generators #2873

Open github-abdellah-elyounsi opened 1 month ago

github-abdellah-elyounsi commented 1 month ago

System information

Describe the bug RSquare does not work with data generator it returns ValueError: Cannot convert a partially known TensorShape (None,) to a Tensor because y_true for some reason is None, None

If the code compiled without RSquare, mse for example everything works.

Code to reproduce the issue

I have the following code:


class DataGenerator(Sequence):
    def __init__(self, x_set, y_set, batch_size):
        self.x, self.y = x_set, y_set
        self.batch_size = batch_size

    def __len__(self):
        return int(np.ceil(len(self.x[0]) / float(self.batch_size)))

    def __getitem__(self, idx):
        low = idx * self.batch_size
        high = min(low + self.batch_size, len(self.x[0]))
        batch_x = [
            self.x[0].iloc[low : high],
            self.x[1].iloc[low : high],
        ]
        batch_y = self.y.iloc[low : high]
        return batch_x, batch_y

model a simple linear model compiled with metrics = Rsquare() and predicts a probability. Labels are 0 and 1 trained with cross entropy

train_gen = DataGenerator(inputs_train,y_train, batch_size)
val_gen = DataGenerator(inputs_val, y_val, batch_size)
history = model.fit(
        train_gen,
        validation_data=val_gen,
        epochs=1,
        shuffle=True,
        verbose=2,
    )

Other info / logs

This code works with all other metrics.

More logs: NFO:root:DataGenerator set completed 2024-07-11 13:01:33.025178: I tensorflow/core/common_runtime/executor.cc:1197] [/device:CPU:0] (DEBUG INFO) Executor start aborting (this does not indicate an error and you can ignore this message): INVALID_ARGUMENT: You must feed a value for placeholder tensor 'Placeholder/_0' with dtype int32 [[{{node Placeholder/_0}}]] Learning rate for epoch 1 is 1.00E-02 y_true (None, None) y_pred (None, 1) y_true: Tensor("Cast_2:0", shape=(None, None), dtype=float32) y_pred: Tensor("model/activation/Sigmoid:0", shape=(None, 1), dtype=float32)

Any idea why ? Thank you !