我想使用加权分布(概率)对数据进行采样
示例如下:
班级分布:
doc_distribution = {0: 40, 1: 18, 2: 8, 3: 598, ... , 9: 177}
我会以等概率的类来制作这批数据集。
total_dataset = 0
init_dist = []
for value in doc_distribution.values():
total_dataset += value
for value in doc_distribution.values():
init_dist.append(value / total_dataset)
target_dist = []
for value in doc_distribution.values():
target_dist.append(1 / len(doc_distribution))
然后,我制作导出模型input_fn
,tf.estimator
def input_fn(ngram_words, labels, opts):
dataset = tf.data.Dataset.from_tensor_slices((ngram_words, labels))
rej = tf.data.experimental.rejection_resample(class_func = lambda _, c : c, \
target_dist = target_dist, initial_dist = init_dist, seed = opts.seed)
dataset = dataset.shuffle(buffer_size = len(ngram_words) * 2, seed = opts.seed)
return dataset.batch(20)
最后,我可以得到如下结果rejection_resample
:
for next_elem in a:
k = next_elem[1]
break
dist = {}
for val in np.array(k):
if val in dist:
dist[val] += 1
else:
dist[val] = 1
print(dist)
结果是:{3: 33, 8: 14, 4: 17, 7: 5, 5: 10, 9: 12, 0: 6, 6: 3}
不知道为什么rejection_resample
效果不好,就是想平均抽取样本。我应该如何解决它?
有什么方法可以平均采样input_fn
吗tf.estimator
?