analysis.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """
  16. This script computes bounds on the privacy cost of training the
  17. student model from noisy aggregation of labels predicted by teachers.
  18. It should be used only after training the student (and therefore the
  19. teachers as well). We however include the label files required to
  20. reproduce key results from our paper (https://arxiv.org/abs/1610.05755):
  21. the epsilon bounds for MNIST and SVHN students.
  22. The command that computes the epsilon bound associated
  23. with the training of the MNIST student model (100 label queries
  24. with a (1/20)*2=0.1 epsilon bound each) is:
  25. python analysis.py
  26. --counts_file=mnist_250_teachers_labels.npy
  27. --indices_file=mnist_250_teachers_100_indices_used_by_student.npy
  28. The command that computes the epsilon bound associated
  29. with the training of the SVHN student model (1000 label queries
  30. with a (1/20)*2=0.1 epsilon bound each) is:
  31. python analysis.py
  32. --counts_file=svhn_250_teachers_labels.npy
  33. --max_examples=1000
  34. --delta=1e-6
  35. """
  36. import os
  37. import math
  38. import numpy as np
  39. import tensorflow as tf
  40. from differential_privacy.multiple_teachers.input import maybe_download
  41. # These parameters can be changed to compute bounds for different failure rates
  42. # or different model predictions.
  43. tf.flags.DEFINE_integer("moments",8, "Number of moments")
  44. tf.flags.DEFINE_float("noise_eps", 0.1, "Eps value for each call to noisymax.")
  45. tf.flags.DEFINE_float("delta", 1e-5, "Target value of delta.")
  46. tf.flags.DEFINE_float("beta", 0.09, "Value of beta for smooth sensitivity")
  47. tf.flags.DEFINE_string("counts_file","","Numpy matrix with raw counts")
  48. tf.flags.DEFINE_string("indices_file","",
  49. "File containting a numpy matrix with indices used."
  50. "Optional. Use the first max_examples indices if this is not provided.")
  51. tf.flags.DEFINE_integer("max_examples",1000,
  52. "Number of examples to use. We will use the first"
  53. " max_examples many examples from the counts_file"
  54. " or indices_file to do the privacy cost estimate")
  55. tf.flags.DEFINE_float("too_small", 1e-10, "Small threshold to avoid log of 0")
  56. tf.flags.DEFINE_bool("input_is_counts", False, "False if labels, True if counts")
  57. FLAGS = tf.flags.FLAGS
  58. def compute_q_noisy_max(counts, noise_eps):
  59. """returns ~ Pr[outcome != winner].
  60. Args:
  61. counts: a list of scores
  62. noise_eps: privacy parameter for noisy_max
  63. Returns:
  64. q: the probability that outcome is different from true winner.
  65. """
  66. # For noisy max, we only get an upper bound.
  67. # Pr[ j beats i*] \leq (2+gap(j,i*))/ 4 exp(gap(j,i*)
  68. # proof at http://mathoverflow.net/questions/66763/
  69. # tight-bounds-on-probability-of-sum-of-laplace-random-variables
  70. winner = np.argmax(counts)
  71. counts_normalized = noise_eps * (counts - counts[winner])
  72. counts_rest = np.array(
  73. [counts_normalized[i] for i in xrange(len(counts)) if i != winner])
  74. q = 0.0
  75. for c in counts_rest:
  76. gap = -c
  77. q += (gap + 2.0) / (4.0 * math.exp(gap))
  78. return min(q, 1.0 - (1.0/len(counts)))
  79. def compute_q_noisy_max_approx(counts, noise_eps):
  80. """returns ~ Pr[outcome != winner].
  81. Args:
  82. counts: a list of scores
  83. noise_eps: privacy parameter for noisy_max
  84. Returns:
  85. q: the probability that outcome is different from true winner.
  86. """
  87. # For noisy max, we only get an upper bound.
  88. # Pr[ j beats i*] \leq (2+gap(j,i*))/ 4 exp(gap(j,i*)
  89. # proof at http://mathoverflow.net/questions/66763/
  90. # tight-bounds-on-probability-of-sum-of-laplace-random-variables
  91. # This code uses an approximation that is faster and easier
  92. # to get local sensitivity bound on.
  93. winner = np.argmax(counts)
  94. counts_normalized = noise_eps * (counts - counts[winner])
  95. counts_rest = np.array(
  96. [counts_normalized[i] for i in xrange(len(counts)) if i != winner])
  97. gap = -max(counts_rest)
  98. q = (len(counts) - 1) * (gap + 2.0) / (4.0 * math.exp(gap))
  99. return min(q, 1.0 - (1.0/len(counts)))
  100. def logmgf_exact(q, priv_eps, l):
  101. """Computes the logmgf value given q and privacy eps.
  102. The bound used is the min of three terms. The first term is from
  103. https://arxiv.org/pdf/1605.02065.pdf.
  104. The second term is based on the fact that when event has probability (1-q) for
  105. q close to zero, q can only change by exp(eps), which corresponds to a
  106. much smaller multiplicative change in (1-q)
  107. The third term comes directly from the privacy guarantee.
  108. Args:
  109. q: pr of non-optimal outcome
  110. priv_eps: eps parameter for DP
  111. l: moment to compute.
  112. Returns:
  113. Upper bound on logmgf
  114. """
  115. if q < 0.5:
  116. t_one = (1-q) * math.pow((1-q) / (1 - math.exp(priv_eps) * q), l)
  117. t_two = q * math.exp(priv_eps * l)
  118. t = t_one + t_two
  119. try:
  120. log_t = math.log(t)
  121. except ValueError:
  122. print "Got ValueError in math.log for values :" + str((q, priv_eps, l, t))
  123. log_t = priv_eps * l
  124. else:
  125. log_t = priv_eps * l
  126. return min(0.5 * priv_eps * priv_eps * l * (l + 1), log_t, priv_eps * l)
  127. def logmgf_from_counts(counts, noise_eps, l):
  128. """
  129. ReportNoisyMax mechanism with noise_eps with 2*noise_eps-DP
  130. in our setting where one count can go up by one and another
  131. can go down by 1.
  132. """
  133. q = compute_q_noisy_max(counts, noise_eps)
  134. return logmgf_exact(q, 2.0 * noise_eps, l)
  135. def sens_at_k(counts, noise_eps, l, k):
  136. """Return sensitivity at distane k.
  137. Args:
  138. counts: an array of scores
  139. noise_eps: noise parameter used
  140. l: moment whose sensitivity is being computed
  141. k: distance
  142. Returns:
  143. sensitivity: at distance k
  144. """
  145. counts_sorted = sorted(counts, reverse=True)
  146. if 0.5 * noise_eps * l > 1:
  147. print "l too large to compute sensitivity"
  148. return 0
  149. # Now we can assume that at k, gap remains positive
  150. # or we have reached the point where logmgf_exact is
  151. # determined by the first term and ind of q.
  152. if counts[0] < counts[1] + k:
  153. return 0
  154. counts_sorted[0] -= k
  155. counts_sorted[1] += k
  156. val = logmgf_from_counts(counts_sorted, noise_eps, l)
  157. counts_sorted[0] -= 1
  158. counts_sorted[1] += 1
  159. val_changed = logmgf_from_counts(counts_sorted, noise_eps, l)
  160. return val_changed - val
  161. def smoothed_sens(counts, noise_eps, l, beta):
  162. """Compute beta-smooth sensitivity.
  163. Args:
  164. counts: array of scors
  165. noise_eps: noise parameter
  166. l: moment of interest
  167. beta: smoothness parameter
  168. Returns:
  169. smooth_sensitivity: a beta smooth upper bound
  170. """
  171. k = 0
  172. smoothed_sensitivity = sens_at_k(counts, noise_eps, l, k)
  173. while k < max(counts):
  174. k += 1
  175. sensitivity_at_k = sens_at_k(counts, noise_eps, l, k)
  176. smoothed_sensitivity = max(
  177. smoothed_sensitivity,
  178. math.exp(-beta * k) * sensitivity_at_k)
  179. if sensitivity_at_k == 0.0:
  180. break
  181. return smoothed_sensitivity
  182. def main(unused_argv):
  183. ##################################################################
  184. # If we are reproducing results from paper https://arxiv.org/abs/1610.05755,
  185. # download the required binaries with label information.
  186. ##################################################################
  187. # Binaries for MNIST results
  188. paper_binaries_mnist = \
  189. ["https://github.com/npapernot/multiple-teachers-for-privacy/blob/master/mnist_250_teachers_labels.npy?raw=true",
  190. "https://github.com/npapernot/multiple-teachers-for-privacy/blob/master/mnist_250_teachers_100_indices_used_by_student.npy?raw=true"]
  191. if FLAGS.counts_file == "mnist_250_teachers_labels.npy" \
  192. or FLAGS.indices_file == "mnist_250_teachers_100_indices_used_by_student.npy":
  193. maybe_download(paper_binaries_mnist, os.getcwd())
  194. # Binaries for SVHN results
  195. paper_binaries_svhn = ["https://github.com/npapernot/multiple-teachers-for-privacy/blob/master/svhn_250_teachers_labels.npy?raw=true"]
  196. if FLAGS.counts_file == "svhn_250_teachers_labels.npy":
  197. maybe_download(paper_binaries_svhn, os.getcwd())
  198. input_mat = np.load(FLAGS.counts_file)
  199. if FLAGS.input_is_counts:
  200. counts_mat = input_mat
  201. else:
  202. # In this case, the input is the raw predictions. Transform
  203. num_teachers, n = input_mat.shape
  204. counts_mat = np.zeros((n, 10)).astype(np.int32)
  205. for i in range(n):
  206. for j in range(num_teachers):
  207. counts_mat[i, input_mat[j, i]] += 1
  208. n = counts_mat.shape[0]
  209. num_examples = min(n, FLAGS.max_examples)
  210. if not FLAGS.indices_file:
  211. indices = np.array(range(num_examples))
  212. else:
  213. index_list = np.load(FLAGS.indices_file)
  214. indices = index_list[:num_examples]
  215. l_list = 1.0 + np.array(xrange(FLAGS.moments))
  216. beta = FLAGS.beta
  217. total_log_mgf_nm = np.array([0.0 for _ in l_list])
  218. total_ss_nm = np.array([0.0 for _ in l_list])
  219. noise_eps = FLAGS.noise_eps
  220. for i in indices:
  221. total_log_mgf_nm += np.array(
  222. [logmgf_from_counts(counts_mat[i], noise_eps, l)
  223. for l in l_list])
  224. total_ss_nm += np.array(
  225. [smoothed_sens(counts_mat[i], noise_eps, l, beta)
  226. for l in l_list])
  227. delta = FLAGS.delta
  228. # We want delta = exp(alpha - eps l).
  229. # Solving gives eps = (alpha - ln (delta))/l
  230. eps_list_nm = (total_log_mgf_nm - math.log(delta)) / l_list
  231. print "Epsilons (Noisy Max): " + str(eps_list_nm)
  232. print "Smoothed sensitivities (Noisy Max): " + str(total_ss_nm / l_list)
  233. # If beta < eps / 2 ln (1/delta), then adding noise Lap(1) * 2 SS/eps
  234. # is eps,delta DP
  235. # Also if beta < eps / 2(gamma +1), then adding noise 2(gamma+1) SS eta / eps
  236. # where eta has density proportional to 1 / (1+|z|^gamma) is eps-DP
  237. # Both from Corolloary 2.4 in
  238. # http://www.cse.psu.edu/~ads22/pubs/NRS07/NRS07-full-draft-v1.pdf
  239. # Print the first one's scale
  240. ss_eps = 2.0 * beta * math.log(1/delta)
  241. ss_scale = 2.0 / ss_eps
  242. print "To get an " + str(ss_eps) + "-DP estimate of epsilon, "
  243. print "..add noise ~ " + str(ss_scale)
  244. print "... times " + str(total_ss_nm / l_list)
  245. print "Epsilon = " + str(min(eps_list_nm)) + "."
  246. if min(eps_list_nm) == eps_list_nm[-1]:
  247. print "Warning: May not have used enough values of l"
  248. # Data independent bound, as mechanism is
  249. # 2*noise_eps DP.
  250. data_ind_log_mgf = np.array([0.0 for _ in l_list])
  251. data_ind_log_mgf += num_examples * np.array(
  252. [logmgf_exact(1.0, 2.0 * noise_eps, l) for l in l_list])
  253. data_ind_eps_list = (data_ind_log_mgf - math.log(delta)) / l_list
  254. print "Data independent bound = " + str(min(data_ind_eps_list)) + "."
  255. return
  256. if __name__ == "__main__":
  257. tf.app.run()