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.
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.
It could be a confusing error message and you should actually cast your data to
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
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
Answered By – Valentin Goldité
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0