Ejemplo n.º 1
0
    def rsample(self, sample_shape=torch.Size([])):
        a_sampler = D.OneHotCategorical(probs=torch.ones(len(self.a_domain)))

        probs = {}
        x_a_dists = {}
        for a in self.a_domain:
            probs[a] = {}
            for x in self.x_support:
                probs[a][x] = math.exp(self.log_prob(x, a))

            normalise = sum(probs[a].values())
            for x in self.x_support:
                probs[a][x] = probs[a][x] / normalise

            x_a_dists[a] = EmpiricalDistribution(None, domain=self.x_support, probs=probs[a])
            self.test = EmpiricalDistribution(None, domain=self.x_support, probs=probs[a])

        a_vals = a_sampler.sample_n(sample_shape.numel())

        a_counts = torch.sum(a_vals, axis=0)

        x_samples = []
        a_samples = []
        for a_c, a_vals in zip(a_counts, self.a_domain):
            a_c = int(a_c)
            x_samples.append(x_a_dists[a_vals].sample_n(a_c))
            a_samples.append(torch.Tensor([a_vals] * a_c))

        x_samples = torch.cat(x_samples).view(*sample_shape, -1)
        a_samples = torch.cat(a_samples).view(*sample_shape, -1)

        return x_samples, a_samples
Ejemplo n.º 2
0
    test_a = torch.Tensor(test_df.loc[:, sensitive].values)
    test_a = torch.cat([test_a, 1 - test_a], axis=1)
    test_sample = list(zip(test_x, test_a))

    #%%
    # Fair Density setup
    x_support = all_binaries(DOMAIN, threshold=[1, 2])
    a_domain = [tuple(int(v) for v in t) for t in torch.eye(train_a.shape[1])]

    train_x_cond_a = []
    for i in range(train_a.shape[1]):
        a_mask = train_a[:, i] > 0

        train_x_cond_a.append(
            EmpiricalDistribution(train_x[a_mask],
                                  domain=x_support,
                                  baseline=0))
        #train_x_cond_a.append(EmpiricalDistribution([], domain=x_support, baseline=1))

    save_dir = 'dt_compas_small_{}_{}/fold_{}'.format(SENSITIVE_ATTRIBUTE, TAU,
                                                      f_idx)
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    #%%
    boost = BoostDensityDT(TAU, train_sample, test_sample, train_x_cond_a,
                           x_support, a_domain)

    #%%
    boost.init_boost(num_iter=50, max_depth=8)
Ejemplo n.º 3
0
##        nn.Linear(20, 20),
#        nn.ReLU(),
#        nn.Linear(20, 1),
        nn.Hardtanh(min_val=-math.log(2), max_val=math.log(2))
    )

    #%%
    # Fair Density setup
    x_support = all_binaries(DOMAIN, threshold=[1,2])
    a_domain = [tuple(int(v) for v in t) for t in torch.eye(train_a.shape[1])]

    train_x_cond_a = []
    for i in range(train_a.shape[1]):
        a_mask = train_a[:, i] > 0

        train_x_cond_a.append(EmpiricalDistribution(train_x[a_mask], domain=x_support, baseline=1))
        #train_x_cond_a.append(EmpiricalDistribution([], domain=x_support, baseline=1))

    #%%
    boost = BoostDensity(TAU, train_sample, test_sample, train_x_cond_a, x_support, a_domain, model)

    #%%
    boost.init_boost(optimiser_gen=optim.Adam, batch_size=128)

    #%%
    start = time.time()
    boost.boost()
    final = time.time()

    results[f_idx] = {
        'fold': f_idx,