From c36ce6d79955828ff12dc04f60aa7628a14d7db9 Mon Sep 17 00:00:00 2001 From: Michael Reneer Date: Thu, 27 Jan 2022 12:34:57 -0800 Subject: [PATCH] Normalize `mpmath` imports in TensorFlow Privacy to be more friendly with strict dependencies and lint. PiperOrigin-RevId: 424681602 --- .../privacy/analysis/rdp_accountant_test.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/tensorflow_privacy/privacy/analysis/rdp_accountant_test.py b/tensorflow_privacy/privacy/analysis/rdp_accountant_test.py index 01f3f3c..4432487 100644 --- a/tensorflow_privacy/privacy/analysis/rdp_accountant_test.py +++ b/tensorflow_privacy/privacy/analysis/rdp_accountant_test.py @@ -17,11 +17,7 @@ import math import sys from absl.testing import parameterized -from mpmath import exp -from mpmath import inf -from mpmath import log -from mpmath import npdf -from mpmath import quad +import mpmath import numpy as np import tensorflow as tf @@ -38,21 +34,21 @@ class TestGaussianMoments(tf.test.TestCase, parameterized.TestCase): def _log_float_mp(self, x): # Convert multi-precision input to float log space. if x >= sys.float_info.min: - return float(log(x)) + return float(mpmath.log(x)) else: return -np.inf - def _integral_mp(self, fn, bounds=(-inf, inf)): - integral, _ = quad(fn, bounds, error=True, maxdegree=8) + def _integral_mp(self, fn, bounds=(-mpmath.inf, mpmath.inf)): + integral, _ = mpmath.quad(fn, bounds, error=True, maxdegree=8) return integral def _distributions_mp(self, sigma, q): def _mu0(x): - return npdf(x, mu=0, sigma=sigma) + return mpmath.npdf(x, mu=0, sigma=sigma) def _mu1(x): - return npdf(x, mu=1, sigma=sigma) + return mpmath.npdf(x, mu=1, sigma=sigma) def _mu(x): return (1 - q) * _mu0(x) + q * _mu1(x) @@ -61,7 +57,7 @@ class TestGaussianMoments(tf.test.TestCase, parameterized.TestCase): def _mu1_over_mu0(self, x, sigma): # Closed-form expression for N(1, sigma^2) / N(0, sigma^2) at x. - return exp((2 * x - 1) / (2 * sigma**2)) + return mpmath.exp((2 * x - 1) / (2 * sigma**2)) def _mu_over_mu0(self, x, q, sigma): return (1 - q) + q * self._mu1_over_mu0(x, sigma)