本文整理匯總了Python中differential_privacy.dp_sgd.per_example_gradients.per_example_gradients.PerExampleGradients方法的典型用法代碼示例。如果您正苦於以下問題:Python per_example_gradients.PerExampleGradients方法的具體用法?Python per_example_gradients.PerExampleGradients怎麽用?Python per_example_gradients.PerExampleGradients使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類differential_privacy.dp_sgd.per_example_gradients.per_example_gradients
的用法示例。
在下文中一共展示了per_example_gradients.PerExampleGradients方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: compute_sanitized_gradients
# 需要導入模塊: from differential_privacy.dp_sgd.per_example_gradients import per_example_gradients [as 別名]
# 或者: from differential_privacy.dp_sgd.per_example_gradients.per_example_gradients import PerExampleGradients [as 別名]
def compute_sanitized_gradients(self, loss, var_list=None,
add_noise=True):
"""Compute the sanitized gradients.
Args:
loss: the loss tensor.
var_list: the optional variables.
add_noise: if true, then add noise. Always clip.
Returns:
a pair of (list of sanitized gradients) and privacy spending accumulation
operations.
Raises:
TypeError: if var_list contains non-variable.
"""
self._assert_valid_dtypes([loss])
xs = [tf.convert_to_tensor(x) for x in var_list]
px_grads = per_example_gradients.PerExampleGradients(loss, xs)
sanitized_grads = []
for px_grad, v in zip(px_grads, var_list):
tensor_name = utils.GetTensorOpName(v)
sanitized_grad = self._sanitizer.sanitize(
px_grad, self._eps_delta, sigma=self._sigma,
tensor_name=tensor_name, add_noise=add_noise,
num_examples=self._batches_per_lot * tf.slice(
tf.shape(px_grad), [0], [1]))
sanitized_grads.append(sanitized_grad)
return sanitized_grads