Tensorflow在不平衡数据上训练
例如,对于一个二分类任务,有N个训练数据,label为0和1,即train_labels.shape = (N,1),
counts = np.bincount(train_label[:, 0])
print('训练数据中正样本占: {} ({:.2f}% of total)'.format(counts[1], 100 * float(counts[1]) / len(train_labels)))
计算训练阶段每个标签的权重:
weight_for_0 = 1. / counts[0]
weight_for_1 = 1. / counts[1]
定义模型,训练时指定每个样本的权重。