logo
Loading...

unsupported operand type(s) for *: 'function' and 'float' - Cupoy

不清楚這個寫法是在哪裡出錯...基本上是依照範例寫的得到的error message為: unsup...

ml100-3,ml100-3-d89

unsupported operand type(s) for *: 'function' and 'float'

2019/12/07 08:50 AM
機器學習新手論壇
Leo Siu
觀看數:0
回答數:1
收藏數:0
ml100-3
ml100-3-d89

不清楚這個寫法是在哪裡出錯...基本上是依照範例寫的

得到的error message為: unsupported operand type(s) for *: 'function' and 'float'

請各位專家指教,謝謝!


import tensorflow as tf

import keras.backend as K


"""Code Here

撰寫一個 loss function, 使其可以結合 focal loss 與 crossentropy loss

"""

def combined_loss(gamma=2., alpha=4.):

    """Define the customized loss."""

    gamma = float(gamma)

    alpha = float(alpha)

    def focal_loss_fixed(y_true, y_pred):

        """Focal loss for multi-classification

        FL(p_t)=-alpha(1-p_t)^{gamma}ln(p_t)

        """

        epsilon = 1e-8

        y_true = tf.convert_to_tensor(y_true, tf.float32)

        y_pred = tf.convert_to_tensor(y_pred, tf.float32)


        model_out = tf.add(y_pred, epsilon)

        ce = tf.multiply(y_true, -tf.log(model_out))

        weight = tf.multiply(y_true, tf.pow(tf.subtract(1., model_out), gamma))

        fl = tf.multiply(alpha, tf.multiply(weight, ce))

        reduced_fl = tf.reduce_max(fl, axis=1)

        return tf.reduce_mean(reduced_fl)

    return focal_loss_fixed*(1-ce_w)+keras.losses.categorical_crossentropy(y_true, y_pred)*ce_w

    

ce_weights_list = [0., 0.3, 0.5, 0.7, 1]