1
$\begingroup$

tensorflow_addons is dead:

$ python3
>>> from tensorflow_addons.metrics import RSquare
2024-07-26 18:50:12.659714: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-07-26 18:50:12.661619: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-07-26 18:50:12.673466: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-07-26 18:50:12.717156: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-07-26 18:50:12.789897: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-07-26 18:50:12.811768: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-07-26 18:50:12.859591: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-07-26 18:50:18.850295: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
my_virtualenv_dir/lib/python3.11/site-packages/tensorflow_addons/utils/tfa_eol_msg.py:23: UserWarning: 

TensorFlow Addons (TFA) has ended development and introduction of new features.
TFA has entered a minimal maintenance and release mode until a planned end of life in May 2024.
Please modify downstream libraries to take dependencies from other repositories in our TensorFlow community (e.g. Keras, Keras-CV, and Keras-NLP). 

For more information see: https://github.com/tensorflow/addons/issues/2807 

  warnings.warn(
my_virtualenv_dir/lib/python3.11/site-packages/tensorflow_addons/utils/ensure_tf_install.py:53: UserWarning: Tensorflow Addons supports using Python ops for all Tensorflow versions above or equal to 2.13.0 and strictly below 2.16.0 (nightly versions are not supported). 
 The versions of TensorFlow you are currently using is 2.17.0 and is not supported. 
Some things might work, some things might not.
If you were to encounter a bug, do not file an issue.
If you want to make sure you're using a tested and supported configuration, either change the TensorFlow version or the TensorFlow Addons's version. 
You can find the compatibility matrix in TensorFlow Addon's readme:
https://github.com/tensorflow/addons
  warnings.warn(
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "my_virtualenv_dir/lib/python3.11/site-packages/tensorflow_addons/__init__.py", line 23, in <module>
    from tensorflow_addons import activations
  File "my_virtualenv_dir/lib/python3.11/site-packages/tensorflow_addons/activations/__init__.py", line 17, in <module>
    from tensorflow_addons.activations.gelu import gelu
  File "my_virtualenv_dir/lib/python3.11/site-packages/tensorflow_addons/activations/gelu.py", line 19, in <module>
    from tensorflow_addons.utils.types import TensorLike
  File "my_virtualenv_dir/lib/python3.11/site-packages/tensorflow_addons/utils/types.py", line 29, in <module>
    from keras.src.engine import keras_tensor
ModuleNotFoundError: No module named 'keras.src.engine'

Given that the above ends in an error, what to use instead of RSquare() in

from tensorflow_addons.metrics import RSquare
model.compile(loss=keras.losses.MeanSquaredError,optimizer=keras.optimizers.Adam(),metrics=[RSquare()])

?

$\endgroup$
4
  • 1
    $\begingroup$ Have you checked the R2Score class from tensorflow? $\endgroup$
    – Oxbowerce
    Commented Jul 26 at 17:06
  • 1
    $\begingroup$ @Oxbowerce Though I saw this class, I'm not sure whether it's a drop-in replacement. The interface seems different, and I get strange output when I plug it into my code. The manual tensorflow.org/api_docs/python/tf/keras/metrics/R2Score doesn't give any direct hint on whether this class can be meaningfully used inside model.compile(metric=[…]). $\endgroup$
    – AlMa1r
    Commented Jul 26 at 17:28
  • 1
    $\begingroup$ What sort of output are you getting? Based on the documentation it inherits from the Metric class, which can be used with the compile API (see the second code snippet on the documentation page). $\endgroup$
    – Oxbowerce
    Commented Jul 26 at 17:58
  • 1
    $\begingroup$ @Oxbowerce Concerning your "What sort of output are you getting?": Something which makes me think that always a constant is returned. It doesn't make any sense. If you really think the new class fits, please feel free to provide a minimal working example. $\endgroup$
    – AlMa1r
    Commented Jul 26 at 18:01

1 Answer 1

2
$\begingroup$

It seems as though the R2Score metric might be what you are looking for. As it also derives from the Metric class it should be a drop in replacement and can be used with the compile API. See the following example:

from tensorflow import keras
import numpy as np

model = keras.Sequential()
model.add(keras.layers.Dense(10, activation="relu"))
model.add(keras.layers.Dense(1))

model.compile(
    optimizer="adam",
    loss=keras.losses.MeanSquaredError(),
    metrics=[keras.metrics.R2Score()]
)

data = np.random.random((1000, 5))
labels = data.sum(axis=1)

model.fit(data, labels, epochs=10)

Which gives the following training metrics:

Epoch 1/10
32/32 [==============================] - 1s 2ms/step - loss: 6.9616 - r2_score: -15.4681
Epoch 2/10
32/32 [==============================] - 0s 2ms/step - loss: 5.6880 - r2_score: -12.4553
Epoch 3/10
32/32 [==============================] - 0s 2ms/step - loss: 4.3526 - r2_score: -9.2965
Epoch 4/10
32/32 [==============================] - 0s 2ms/step - loss: 2.9772 - r2_score: -6.0427
Epoch 5/10
32/32 [==============================] - 0s 2ms/step - loss: 1.7269 - r2_score: -3.0851
Epoch 6/10
32/32 [==============================] - 0s 2ms/step - loss: 0.8163 - r2_score: -0.9311
Epoch 7/10
32/32 [==============================] - 0s 3ms/step - loss: 0.3177 - r2_score: 0.2484
Epoch 8/10
32/32 [==============================] - 0s 3ms/step - loss: 0.1258 - r2_score: 0.7023
Epoch 9/10
32/32 [==============================] - 0s 2ms/step - loss: 0.0741 - r2_score: 0.8247
Epoch 10/10
32/32 [==============================] - 0s 2ms/step - loss: 0.0644 - r2_score: 0.8477

The final R2 score of 0.8477 seems to be correct given the following plot comparing the predicted values with the actual values:

enter image description here

$\endgroup$
4
  • $\begingroup$ Thx! How have you plot it ? $\endgroup$
    – AlMa1r
    Commented Jul 27 at 8:51
  • $\begingroup$ As for the MeanSquaredError, I'm unsure what to use instead. Ideally, I'd use 1-R2, but I don't find a built-it loss function for it. $\endgroup$
    – AlMa1r
    Commented Jul 27 at 8:52
  • $\begingroup$ I just used matplotlib to plot the predictions against the labels. Why not use mean squared error as your loss function? R-squared is a reformulation of the MSE, so minimizing the MSE is equal to maximizing the R-squared. $\endgroup$
    – Oxbowerce
    Commented Jul 27 at 9:53
  • $\begingroup$ Your code gives me an erroron tensorflow 2.13: "ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: <tf.Tensor: shape=(), dtype=float32, numpy=0.0>". As reported by another user on Stack Overflow. $\endgroup$ Commented Oct 18 at 12:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.