當前位置: 首頁>>代碼示例>>Python>>正文


Python accountant.GaussianMomentsAccountant方法代碼示例

本文整理匯總了Python中differential_privacy.privacy_accountant.tf.accountant.GaussianMomentsAccountant方法的典型用法代碼示例。如果您正苦於以下問題:Python accountant.GaussianMomentsAccountant方法的具體用法?Python accountant.GaussianMomentsAccountant怎麽用?Python accountant.GaussianMomentsAccountant使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在differential_privacy.privacy_accountant.tf.accountant的用法示例。


在下文中一共展示了accountant.GaussianMomentsAccountant方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: GAN_solvers

# 需要導入模塊: from differential_privacy.privacy_accountant.tf import accountant [as 別名]
# 或者: from differential_privacy.privacy_accountant.tf.accountant import GaussianMomentsAccountant [as 別名]
def GAN_solvers(D_loss, G_loss, learning_rate, batch_size, total_examples, 
        l2norm_bound, batches_per_lot, sigma, dp=False):
    """
    Optimizers
    """
    discriminator_vars = [v for v in tf.trainable_variables() if v.name.startswith('discriminator')]
    generator_vars = [v for v in tf.trainable_variables() if v.name.startswith('generator')]
    if dp:
        print('Using differentially private SGD to train discriminator!')
        eps = tf.placeholder(tf.float32)
        delta = tf.placeholder(tf.float32)
        priv_accountant = accountant.GaussianMomentsAccountant(total_examples)
        clip = True
        l2norm_bound = l2norm_bound/batch_size
        batches_per_lot = 1
        gaussian_sanitizer = sanitizer.AmortizedGaussianSanitizer(
                priv_accountant,
                [l2norm_bound, clip])
       
        # the trick is that we need to calculate the gradient with respect to
        # each example in the batch, during the DP SGD step
        D_solver = dp_optimizer.DPGradientDescentOptimizer(learning_rate,
                [eps, delta],
                sanitizer=gaussian_sanitizer,
                sigma=sigma,
                batches_per_lot=batches_per_lot).minimize(D_loss, var_list=discriminator_vars)
    else:
        D_loss_mean_over_batch = tf.reduce_mean(D_loss)
        D_solver = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(D_loss_mean_over_batch, var_list=discriminator_vars)
        priv_accountant = None
    G_loss_mean_over_batch = tf.reduce_mean(G_loss)
    G_solver = tf.train.AdamOptimizer().minimize(G_loss_mean_over_batch, var_list=generator_vars)
    return D_solver, G_solver, priv_accountant

# --- to do with the model --- # 
開發者ID:ratschlab,項目名稱:RGAN,代碼行數:37,代碼來源:model.py


注:本文中的differential_privacy.privacy_accountant.tf.accountant.GaussianMomentsAccountant方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。