Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

270
Vistas
Creating BLEU loss method on tensorflow gives "No gradient provided"

I need to build a custom loss method based on BLEU. I'm passing my LabelEncoder in the constructor to reverse labels and predictions and calculate the bleu distance.

Here is my Loss class

class CIMCodeSuccessiveLoss(Loss):

    def __init__(self, labelEncoder: LabelEncoder):
        super().__init__()
        self.le = labelEncoder

    def bleu_score(self, true_label, pred_label):
        cim_true_label = self.le.inverse_transform(true_label.numpy())
        cim_pred_label = self.le.inverse_transform(pred_label.numpy())
        bleu_scores = [sentence_bleu(list(one_true_label),
                                     list(one_pred_label),
                                     weights=(0.5, 0.25, 0.125, 0.125)) for one_true_label, one_pred_label in
                       zip(cim_true_label, cim_pred_label)]
        return np.float32(bleu_scores)

    def call(self, y_true, y_pred):
        labeled_y_pred = tf.cast(tf.argmax(y_pred, axis=-1), tf.int32)
        bleu = tf.py_function(self.bleu_score, (tf.reshape(y_true, [-1]), labeled_y_pred), tf.float32)
        return tf.reduce_sum(tf.square(1 - bleu))

The bleu_score method is calculating the correct scores and returns a NumPy array. when I try to return the squared sum, I get this error

raise ValueError(f"No gradients provided for any variable: {variable}.

I'm also providing the model:

inputs = tf.keras.Input(shape=(1,), dtype=tf.string)
x = vectorize_layer(inputs)
x = Embedding(vocab_size, embedding_dim, name="embedding")(x)
x = LSTM(units=32, name="lstm")(x)
outputs = Dense(classes_number, name="classification")(x)

model = tf.keras.Model(inputs=inputs, outputs=outputs, name="first_cim_classifier")

model.summary()


# we add early stopping for our model.
early_stopping = EarlyStopping(monitor='loss', patience=2)

model.compile(
    loss=CIMCodeSuccessiveLoss(le),
    optimizer=tf.keras.optimizers.Adam(),
    metrics=["accuracy", "crossentropy"],
    run_eagerly=True)

trained_model = model.fit(np.array(x_train), np.array(y_train), batch_size=64, epochs=10,
                          validation_data=(np.array(x_val), np.array(y_val)),
                          callbacks=[early_stopping])

Any help is appreciated. Thanks in advance.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

To calculate the loss function, you use the method 'tf.argmax(y_pred, axis=-1)',argmax is not differentiable and the automatic differentiation to calculate the gradients is not possible, you have to remove this method, for example (depending on your data) you can change the output layer to softmax and labels to one_hot.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda