类别不均衡以及自定义loss函数

2019-02-28  本文已影响0人  夕宝爸爸

计算标签权重

def calculating_class_weights(y_true):
    from sklearn.utils.class_weight import compute_class_weight
    number_dim = np.shape(y_true)[1]
    weights = np.empty([number_dim, 2])
    for i in range(number_dim):
        weights[i] = compute_class_weight('balanced', [0.,1.], y_true[:, i])
    return weights
y_true = np.array([[0,1,1,1,0,1],[0,0,0,1,1,1],[1,0,0,0,1,0],[1,1,0,0,1,0]])
print(y_true)
print(weights)
[[0 1 1 1 0 1]
 [0 0 0 1 1 1]
 [1 0 0 0 1 0]
 [1 1 0 0 1 0]]
[[1.         1.        ]
 [1.         1.        ]
 [0.66666667 2.        ]
 [1.         1.        ]
 [2.         0.66666667]
 [1.         1.        ]]

构建定制化的loss函数

import tensorflow as tf
from keras import backend as K
def get_weighted_loss(weights):
    def weighted_loss(y_true, y_pred):
        return K.mean((weights[:,0]**(1-y_true))*(weights[:,1]**(y_true))*K.binary_crossentropy(tf.convert_to_tensor(y_true,np.float32), tf.convert_to_tensor(y_pred,np.float32)), axis=-1)
    return weighted_loss
model.compile(optimizer=Adam(), loss=get_weighted_loss(class_weights))

这里有一点注意,需要将numpy array转换为tensor,转换的方式为:

import tensorflow as tf
import numpy as np

data = [[1,2,3],[4,5,6]]
data_np = np.asarray(data, np.float32)

data_tf = tf.convert_to_tensor(data_np, np.float32)

否则将会报错

AttributeError: 'numpy.dtype' object has no attribute 'base_dtype'
上一篇 下一篇

猜你喜欢

热点阅读