本文整理匯總了Python中differential_privacy.dp_sgd.dp_optimizer.sanitizer.AmortizedGaussianSanitizer方法的典型用法代碼示例。如果您正苦於以下問題:Python sanitizer.AmortizedGaussianSanitizer方法的具體用法?Python sanitizer.AmortizedGaussianSanitizer怎麽用?Python sanitizer.AmortizedGaussianSanitizer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類differential_privacy.dp_sgd.dp_optimizer.sanitizer
的用法示例。
在下文中一共展示了sanitizer.AmortizedGaussianSanitizer方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: GAN_solvers
# 需要導入模塊: from differential_privacy.dp_sgd.dp_optimizer import sanitizer [as 別名]
# 或者: from differential_privacy.dp_sgd.dp_optimizer.sanitizer import AmortizedGaussianSanitizer [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 --- #