예제 #1
0
def test_dp_model_with_pynative_mode():
    context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
    norm_bound = 1.0
    initial_noise_multiplier = 0.01
    network = Net()
    batch_size = 32
    batches = 128
    epochs = 1
    micro_batches = 2
    loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
    factory_opt = DPOptimizerClassFactory(micro_batches=micro_batches)
    factory_opt.set_mechanisms(
        'Gaussian',
        norm_bound=norm_bound,
        initial_noise_multiplier=initial_noise_multiplier)
    net_opt = factory_opt.create('Momentum')(network.trainable_params(),
                                             learning_rate=0.1,
                                             momentum=0.9)
    clip_mech = ClipMechanismsFactory().create('Gaussian',
                                               decay_policy='Linear',
                                               learning_rate=0.01,
                                               target_unclipped_quantile=0.9,
                                               fraction_stddev=0.01)
    model = DPModel(micro_batches=micro_batches,
                    norm_bound=norm_bound,
                    clip_mech=clip_mech,
                    noise_mech=None,
                    network=network,
                    loss_fn=loss,
                    optimizer=net_opt,
                    metrics=None)
    ms_ds = ds.GeneratorDataset(dataset_generator(batch_size, batches),
                                ['data', 'label'])
    model.train(epochs, ms_ds, dataset_sink_mode=False)
예제 #2
0
def test_dp_model_with_graph_mode():
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
    norm_bound = 1.0
    initial_noise_multiplier = 0.01
    network = Net()
    epochs = 1
    loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
    noise_mech = NoiseMechanismsFactory().create(
        'Gaussian',
        norm_bound=norm_bound,
        initial_noise_multiplier=initial_noise_multiplier)
    clip_mech = ClipMechanismsFactory().create('Gaussian',
                                               decay_policy='Linear',
                                               learning_rate=0.01,
                                               target_unclipped_quantile=0.9,
                                               fraction_stddev=0.01)
    net_opt = nn.Momentum(network.trainable_params(),
                          learning_rate=0.1,
                          momentum=0.9)
    model = DPModel(micro_batches=2,
                    clip_mech=clip_mech,
                    norm_bound=norm_bound,
                    noise_mech=noise_mech,
                    network=network,
                    loss_fn=loss,
                    optimizer=net_opt,
                    metrics=None)
    ms_ds = ds.GeneratorDataset(dataset_generator, ['data', 'label'])
    model.train(epochs, ms_ds, dataset_sink_mode=False)
예제 #3
0
def test_dp_model_with_graph_mode_ada_gaussian():
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
    norm_bound = 1.0
    initial_noise_multiplier = 0.01
    network = Net()
    batch_size = 32
    batches = 128
    epochs = 1
    alpha = 0.8
    loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
    noise_mech = NoiseMechanismsFactory().create(
        'AdaGaussian',
        norm_bound=norm_bound,
        initial_noise_multiplier=initial_noise_multiplier,
        noise_decay_rate=alpha,
        decay_policy='Exp')
    clip_mech = None
    net_opt = nn.Momentum(network.trainable_params(),
                          learning_rate=0.1,
                          momentum=0.9)
    model = DPModel(micro_batches=2,
                    clip_mech=clip_mech,
                    norm_bound=norm_bound,
                    noise_mech=noise_mech,
                    network=network,
                    loss_fn=loss,
                    optimizer=net_opt,
                    metrics=None)
    ms_ds = ds.GeneratorDataset(dataset_generator(batch_size, batches),
                                ['data', 'label'])
    model.train(epochs, ms_ds, dataset_sink_mode=False)
예제 #4
0
    net_opt = nn.Momentum(params=network.trainable_params(),
                          learning_rate=cfg.lr,
                          momentum=cfg.momentum)
    # Create a monitor for DP training. The function of the monitor is to
    # compute and print the privacy budget(eps and delta) while training.
    rdp_monitor = PrivacyMonitorFactory.create(
        'rdp',
        num_samples=60000,
        batch_size=cfg.batch_size,
        initial_noise_multiplier=cfg.initial_noise_multiplier,
        per_print_times=234)
    # Create the DP model for training.
    model = DPModel(micro_batches=cfg.micro_batches,
                    norm_bound=cfg.norm_bound,
                    noise_mech=noise_mech,
                    network=network,
                    loss_fn=net_loss,
                    optimizer=net_opt,
                    metrics={"Accuracy": Accuracy()})

    LOGGER.info(TAG, "============== Starting Training ==============")
    model.train(cfg['epoch_size'],
                ds_train,
                callbacks=[ckpoint_cb, LossMonitor(), rdp_monitor],
                dataset_sink_mode=cfg.dataset_sink_mode)

    LOGGER.info(TAG, "============== Starting Testing ==============")
    ckpt_file_name = 'trained_ckpt_file/checkpoint_lenet-5_234.ckpt'
    param_dict = load_checkpoint(ckpt_file_name)
    load_param_into_net(network, param_dict)
    ds_eval = generate_mnist_dataset(os.path.join(cfg.data_path, 'test'),