Home > database >  Can't use Keras' CosineSimilarity in Tensorflow2.7
Can't use Keras' CosineSimilarity in Tensorflow2.7

Time:09-10

I'm running TF 2.7 (I need it in that version). I'm trying to use the Keras.losses.CosineSimilarity loss function to train a Keras model (Stellargraph GCN model). But everytime a strange error stops the training process. I can confirm that it is related to using CosineSimilarity.

The error:=

TypeError: Expected int64 passed to parameter 'y' of op 'Maximum', got 1e-12 of type 'float' instead. Error: Expected int64, but got 1e-12 of type 'float'.

Here is the code :=

from tensorflow.keras import layers, optimizers, losses, metrics, Model
from tensorflow.keras.metrics import Precision
from tensorflow.keras.callbacks import EarlyStopping

model = Model(inputs=x_inp, outputs=predictions)
similarity = losses.CosineSimilarity()
model.compile(
    optimizer=optimizers.Adam(learning_rate = 0.001),
    loss = similarity,
    metrics= [Precision],
)

es_callback = EarlyStopping(monitor="val_F1", patience=50, restore_best_weights=True)

history = model.fit(
    train_gen,
    epochs=500,
    validation_data=val_gen,
    verbose=2,
    shuffle=False,  
    callbacks=[es_callback],
)

I can confirm that all of my training & validation data are of the integer type.

CodePudding user response:

It could be a confusing error message and you should actually cast your data to float32.

See here for an example of similar error.

In your case I think CosineSimilarity actually calculates the addition of the product of the norms (that is int32 here because your data are int32) and an epsilon (float32 seems to be equal to 1e-12) for denominator. It expects that epsilon to be a int32 to proceed the addition and raises this error. However in you case what you need is to change the data type to float32 instead.

  • Related