Я пытаюсь проверить выборку отклонения с помощью следующего кода.Вот результат, который я получил:
target_dist [0.5, 0.5]
initial distribution [0.8333333333333334, 0.16666666666666666]
result counts [1500, 600]
final dist 0.7142857142857143 0.2857142857142857
Окончательное распределение не отражает целевое распределение, которое я установил.
Есть идеи?
import tensorflow as tf
import numpy as np
# everything is based on tensorflow 2.0
tf.random.set_seed(2342)
def map2label(sample):
return tf.cast(tf.math.equal(sample, 2), tf.int32)
np_data = np.array([0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2])
target_dist = [0.5, 0.5]
init_dist = [(np_data.shape[0]-3)/np_data.shape[0], 3/np_data.shape[0]]
dataset = tf.data.Dataset.from_tensor_slices(np_data)
rej = tf.data.experimental.rejection_resample(map2label, target_dist, init_dist, 2342) # set seed explicitly
dataset = dataset.apply(rej)
bucket_counts = [0, 0]
for i in range(100):
for data in dataset:
class_id, data_content = data
bucket_counts[class_id.numpy()] += 1
print("This is your target_dist", target_dist, "This is your initial distribution", init_dist)
print("This is your result counts", bucket_counts,
"This is your final dist", bucket_counts[0] / np.sum(bucket_counts), bucket_counts[1] / np.sum(bucket_counts))