unsupported operand type(s) for *: 'function' and 'float'
不清楚這個寫法是在哪裡出錯...基本上是依照範例寫的
得到的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]
回答列表
-
2019/12/08 上午 00:02張維元 (WeiYuan)贊同數:0不贊同數:1留言數:2
unsupported operand type(s) for *: 'function' and 'float' => 這個錯誤訊息是說 function 跟 float 沒有乘法的運算。 因為 focal_loss_fixed 是一個 function。