pythontensorflowmachine-learningkeras

Using tf.keras.metrics.R2Score results in an error in Tensorflow


I'm making a regression model with Tensorflow, but when I use tf.keras.metrics.R2Score() as a metric, it fails with ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: <tf.Tensor: shape=(), dtype=float32, numpy=0.0> after the first epoch. (But works fine up until then) However, if I use a different metric (tf.keras.metrics.RootMeanSquaredError(), it works fine.

import pandas as pd

weather_states = pd.read_sql("SELECT stations.id, stations.capacity_kw, start, wind_speed_10m, wind_direction_10m, wind_speed_80m, wind_direction_80m, wind_speed_180m, wind_direction_180m FROM stations INNER JOIN weather_states ON stations.id = weather_states.station WHERE weather_states.source = 'openmeteo_forecast/history/best' AND stations.source = 'wind'", db_client)
 
grid_states = pd.read_sql("SELECT start, wind FROM grid_states", db_client)
 
def create_x_y(df: tuple[Any, pd.DataFrame]):
    start = df[1]["start"].iloc[0]
    res = df[1].sort_values("id").drop(["id", "start"], axis=1)
    temp_wind = grid_states.loc[grid_states["start"] == start]["wind"].to_list()
    wind_kw = temp_wind if len(temp_wind) >= 1 else None
    res_flat_df = pd.DataFrame(res.to_numpy().reshape((1, -1)))
    res_flat_df["wind_kw"] = wind_kw
    return res_flat_df
 
data = pd.concat(map(create_x_y, weather_states.groupby("start"))).dropna()
from sklearn.model_selection import train_test_split
 
 
data = data.astype("float32")
train, test = train, test = train_test_split(data.dropna(), test_size=0.2)
 
train_y = train.pop("wind_kw")
train_x = train
 
 
test_y = test.pop("wind_kw")
test_x = test
 
norm = tf.keras.layers.Normalization()
norm.adapt(train_x)
 
model = tf.keras.Sequential([
    norm,
    tf.keras.layers.Dense(16, activation="linear"),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Dense(1, activation="linear"),
])
 
 
model.compile(
    optimizer=tf.keras.optimizers.legacy.Adam(0.001),
    metrics=[tf.keras.metrics.R2Score(dtype=tf.float32)],
    loss=tf.keras.losses.MeanSquaredError(),
)
 
model.fit(train_x, train_y, epochs=7, batch_size=2)
 
tf.keras.models.save_model(model, 'wind.keras')

print(data.describe())

              0            1            2            3            4  ...          241          242          243          244      wind_kw
count    1896.0  1896.000000  1896.000000  1896.000000  1896.000000  ...  1896.000000  1896.000000  1896.000000  1896.000000  1896.000000
mean   144000.0     4.315717   189.610759     5.791377   193.830169  ...     3.881292   145.420359     4.572205   143.642405  1292.576958
std         0.0     2.482439   113.178764     2.926497   113.685887  ...     2.612259    93.293471     2.775681    94.721086   611.333721
min    144000.0     0.100000     1.000000     0.100000     1.000000  ...     0.100000     2.000000     0.000000     1.000000    34.263000
25%    144000.0     2.110000    88.000000     3.487500    90.000000  ...     1.900000    67.000000     2.500000    63.000000   793.109500
50%    144000.0     4.110000   199.000000     5.500000   231.000000  ...     3.075000   137.000000     3.940000   135.000000  1251.590000
75%    144000.0     6.220000   291.000000     7.882500   294.000000  ...     5.502500   205.000000     6.082500   205.000000  1761.926750
max    144000.0    11.670000   360.000000    15.210000   360.000000  ...    14.460000   360.000000    16.980000   360.000000  3008.125000
print(type(data))
#<class 'pandas.core.frame.DataFrame'>
print(data.dtypes)
#0          float32
#1          float32
#2          float32
#3          float32
#4          float32
#            ...   
#241        float32
#242        float32
#243        float32
#244        float32
#wind_kw    float32
#Length: 246, dtype: object
print(data.shape)
#(1896, 246)

I can't seem to find any information online about this error when using R2Score- any ideas as to what could be the issue?


Solution

  • It is a bug of TensorFlow 2.13. Use TensorFlow 2.15 or higher.