0

我想使用加权分布(概率)对数据进行采样

示例如下:

班级分布: 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_fntf.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_fntf.estimator

4

1 回答 1

0

我们可以使用tf.data.experimental.sample_from_datasets代替rejection_resample.

unbatched_dataset = [(dataset.filter(lambda _, label: label == i)) for i in range(0, classify_num)]
weights = [1 / classify_num] * classify_num
balanced_ds = tf.data.experimental.sample_from_datasets(unbatched_dataset, weights, seed=opts.seed)
dataset = balanced_ds.shuffle(buffer_size = 1000, seed = opts.seed).repeat(opts.epochs)
于 2020-09-10T00:38:10.713 回答