コード例 #1
0
        predictor_type='Linear',
        respects_MC=False)
    accuracy_s_rf[i] = evaluations.evaluate_private_representations(
        network.encoder,
        trainset,
        testset,
        device,
        verbose=True,
        predictor_type='RandomForest',
        respects_MC=False)
    X, Y = testset.data, testset.hidden
    Z, Z_mean = network.encoder(
        torch.FloatTensor(X).to(device),
        torch.FloatTensor(Y).to(device))
    mine_network = mine.MINE(1,
                             representation_dim,
                             hidden_size=100,
                             moving_average_rate=0.1).to(device)
    print('MINE calculations...')
    IYZ[i] = mine_network.train(Y,
                                Z.detach().cpu().numpy(),
                                batch_size=batch_size_mine,
                                n_iterations=int(5e4),
                                n_verbose=-1,
                                n_window=100,
                                save_progress=-1)
    print(f'I(Y;Z): {IYZ[i]}')

np.save(logsdir + 'betas', betas)
np.save(logsdir + 'IYZ', IYZ)
np.save(logsdir + 'accuracy_s_lin', accuracy_s_lin)
np.save(logsdir + 'accuracy_s_rf', accuracy_s_rf)
コード例 #2
0
ファイル: run.py プロジェクト: burklight/MINE-PyTorch
    rhos = np.linspace(-0.95, 0.95, n_rhos)

    for i, rho in enumerate(rhos):

        print(f'Computing MI for rho {rho:.2f}')

        # Compute the real MI
        cov = np.eye(2 * d)
        cov[d:2 * d, 0:d] = rho * np.eye(d)
        cov[0:d, d:2 * d] = rho * np.eye(d)
        MI_real[i] = -0.5 * np.log(np.linalg.det(cov)) / np.log(2)

        # Compute the estimation of the MI
        dataset = utils.BiVariateGaussianDatasetForMI(d, rho, 5000)
        mine_network = mine.MINE(d, d, hidden_size=100).to(device)
        MI_mine[i] = mine_network.train(dataset,
                                        batch_size=args.batch_size,
                                        n_iterations=args.n_iterations,
                                        n_verbose=args.n_verbose,
                                        n_window=args.n_window,
                                        save_progress=args.save_progress)

        print(
            f'Rho {rho:.2f}: Real ({MI_real[i]:.3f}) - Estimated ({MI_mine[i]:.3f})'
        )

    plt.plot(rhos, MI_real, 'black', label='Real MI')
    plt.plot(rhos, MI_mine, 'orange', label='Estimated MI')
    plt.legend()
    plt.savefig(f'../figures/{d}-dimensional_gaussian_mi.png')