Кастомная loss функция Keras

tf.Tensor(
[2.04 1.83 1.   5.   1.87 1.98 0.   0.   0.   1.   3.   1.   1.63 2.39
 1.   0.   0.   0.   9.   3.   1.74 2.17 1.   0.   0.   0.   4.   3.
 1.82 2.03 1.   0.   0.   0.   3.   2.   1.8  2.07 1.   0.   0.   0.
 1.   5.   1.87 1.98 0.   0.   0.   1.   3.   1.   1.63 2.39 1.   0.
 0.   0.   2.   0.   1.87 1.95 1.   0.   0.   0.   5.   6.   1.97 1.88
 0.   0.   1.   0.   2.   6.   1.78 2.11 0.   0.   0.   1.   1.   5.
 1.87 1.98 0.   0.   0.   1.   3.   1.   1.63 2.39 1.   0.   0.   0.
 9.   3.   1.74 2.17 1.   0.   0.   0.   4.   3.   1.82 2.03 1.   0.
 0.   0.   3.   2.   1.8  2.07 1.   0.   0.   0.   1.   5.   1.87 1.98
 0.   0.   0.   1.   3.   1.   1.63 2.39 1.   0.   0.   0.   9.   3.
 1.74 2.17 1.   0.   0.   0.   4.   3.   1.82 2.03 1.   0.   0.   0.
 3.   2.   1.8  2.07 1.   0.   0.   0.   1.   5.   1.87 1.98 0.   0.
 0.   1.   3.   1.   1.63 2.39 1.   0.   0.   0.   4.   3.   2.08 1.8
 0.   1.   0.   0.   3.   0.   1.95 1.89 0.   1.   0.   0.   4.   5.
 2.13 1.77 0.   0.   1.   0.   1.   5.   1.87 1.98 0.   0.   0.   1.
 3.   1.   1.63 2.39 1.   0.   0.   0.   9.   3.   1.74 2.17 1.   0.
 0.   0.   4.   3.   1.82 2.03 1.   0.   0.   0.   3.   2.   1.8  2.07
 1.   0.   0.   0.  ], shape=(242,), dtype=float32) 

Это подается на вход.

tf.Tensor([1.   2.04 1.83], shape=(3,), dtype=float32)

Это y_true.

def my_loss_fn(y_true, y_pred):
    x_1 = float()
    for trueValLine,predValLine in zip(y_true,y_pred):
        if trueValLine[0]>=0.5:
            if predValLine[0] == 1:
                x_1 += min(trueValLine[1],trueValLine[2])
            else:
                x_1 -= 1
        else:
            if predValLine[0] == 0:
                x_1 += max(trueValLine[1],trueValLine[2])
            else:
                x_1 -= 1
    return 1/x_1   

Функция потерь. Ошибка:

OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed: AutoGraph did convert this function. This might indicate
you are trying to use an unsupported feature.

Как это исправить?


Ответы (0 шт):