2021-10-30

Keras - MulOp type mismatch

I get the following error while trying to compile my keras model. I've researched stackoverflow and the problem was posted several times. I tried the fixes suggested there, but nothing worked. Maybe someone can spot the problem.

TypeError: in user code:

C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_math_ops.py:6245 mul
"Mul", x=x, y=y, name=name)
C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py:558 _apply_op_helper
inferred_from[input_arg.type_attr]))

TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type float64 of argument 'x'.

This is my code:

from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

num_classes = len(label_encoder.classes_)

def make_model(input_shape):
    bias = tf.cast(np.log(pos/neg),tf.float64)
    output_bias = tf.keras.initializers.Constant(bias)
    
    input_layer = keras.layers.Input(input_shape)

    conv1 = keras.layers.Conv1D(filters=64, kernel_size=3, padding="same")(input_layer)
    conv1 = keras.layers.BatchNormalization()(conv1)
    conv1 = keras.layers.ReLU()(conv1)

    conv2 = keras.layers.Conv1D(filters=64, kernel_size=3, padding="same")(conv1)
    conv2 = keras.layers.BatchNormalization()(conv2)
    conv2 = keras.layers.ReLU()(conv2)

    conv3 = keras.layers.Conv1D(filters=64, kernel_size=3, padding="same")(conv2)
    conv3 = keras.layers.BatchNormalization()(conv3)
    conv3 = keras.layers.ReLU()(conv3)

    gap = keras.layers.GlobalAveragePooling1D()(conv3)

    output_layer = keras.layers.Dense(1, activation="sigmoid", bias_initializer=output_bias)(gap)

    return keras.models.Model(inputs=input_layer, outputs=output_layer)

model = make_model(input_shape=X_train.shape[1:])
epochs = 40
batch_size = 50

METRICS = [
      keras.metrics.TruePositives(name='tp'),
      keras.metrics.FalsePositives(name='fp'),
      keras.metrics.TrueNegatives(name='tn'),
      keras.metrics.FalseNegatives(name='fn'), 
      keras.metrics.BinaryAccuracy(name='accuracy'),
      keras.metrics.Precision(name='precision'),
      keras.metrics.Recall(name='recall'),
      keras.metrics.AUC(name='auc'),
      keras.metrics.AUC(name='prc', curve='PR'), # precision-recall curve
]

callbacks = [
    keras.callbacks.ModelCheckpoint(
        "best_model.h5", save_best_only=True, monitor="val_prc"
    ),
    keras.callbacks.ReduceLROnPlateau(
        monitor="val_prc", factor=0.5, patience=50, min_lr=0.0001
    ),
    keras.callbacks.EarlyStopping(monitor="val_prc", patience=20, verbose=1, restore_best_weights=True,mode="max"),
]
model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=METRICS,
)
history = model.fit(
    X_train,
    y_train,
    batch_size=batch_size,
    epochs=epochs,
    callbacks=callbacks,
    validation_split=0.2,
    verbose=1,
)

Also explicit type casting of the input data doesn't help:

X_train = tf.cast(X_train,tf.float64)
X_test = tf.cast(X_test,tf.float64)
y_train = tf.cast(y_train,tf.float64)
y_test = tf.cast(y_test,tf.float64)


from Recent Questions - Stack Overflow https://ift.tt/3Cu9BWY
https://ift.tt/eA8V8J

No comments:

Post a Comment