I am testing an example code from the TensorFlow website using Jupyter Notebook. You can find the code at the following link:
https://www.tensorflow.org/responsible_ai/privacy/tutorials/classification_privacy
Below is the code along with the versions of TensorFlow and TensorFlow Privacy installed on my computer.
!pip show tensorflow
Name: tensorflow
Version: 2.14.1
!pip show tensorflow_privacy
Name: tensorflow_privacy
Version: 0.9.0
import tensorflow as tf
tf.compat.v1.disable_v2_behavior()
import numpy as np
tf.get_logger().setLevel('ERROR')
WARNING:tensorflow:From /usr/local/lib/python3.10/dist-packages/tensorflow/python/compat/v2_compat.py:108: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
import tensorflow_privacy
from tensorflow_privacy.privacy.analysis import compute_dp_sgd_privacy
train, test = tf.keras.datasets.mnist.load_data()
train_data, train_labels = train
test_data, test_labels = test
train_data = np.array(train_data, dtype=np.float32) / 255
test_data = np.array(test_data, dtype=np.float32) / 255
train_data = train_data.reshape(train_data.shape[0], 28, 28, 1)
test_data = test_data.reshape(test_data.shape[0], 28, 28, 1)
train_labels = np.array(train_labels, dtype=np.int32)
test_labels = np.array(test_labels, dtype=np.int32)
train_labels = tf.keras.utils.to_categorical(train_labels, num_classes=10)
test_labels = tf.keras.utils.to_categorical(test_labels, num_classes=10)
assert train_data.min() == 0.
assert train_data.max() == 1.
assert test_data.min() == 0.
assert test_data.max() == 1.
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11490434/11490434 [==============================] - 2s 0us/step
epochs = 3
batch_size = 250
l2_norm_clip = 1.5
noise_multiplier = 1.3
num_microbatches = 250
learning_rate = 0.25
if batch_size % num_microbatches != 0:
raise ValueError('Batch size should be an integer multiple of the number of microbatches')
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(16, 8,
strides=2,
padding='same',
activation='relu',
input_shape=(28, 28, 1)),
tf.keras.layers.MaxPool2D(2, 1),
tf.keras.layers.Conv2D(32, 4,
strides=2,
padding='valid',
activation='relu'),
tf.keras.layers.MaxPool2D(2, 1),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(10)
])
optimizer = tensorflow_privacy.DPKerasSGDOptimizer(
l2_norm_clip=l2_norm_clip,
noise_multiplier=noise_multiplier,
num_microbatches=num_microbatches,
learning_rate=learning_rate)
loss = tf.keras.losses.CategoricalCrossentropy(
from_logits=True, reduction=tf.losses.Reduction.NONE)
model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
model.fit(train_data, train_labels,
epochs=epochs,
validation_data=(test_data, test_labels),
batch_size=batch_size)
Train on 60000 samples, validate on 10000 samples
Epoch 1/3
60000/60000 [==============================] - 204s 3ms/sample - loss: 0.8745 - acc: 0.7297 - val_loss: 0.3688 - val_acc: 0.8954
Epoch 2/3
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training_v1.py:2335: UserWarning: `Model.state_updates` will be removed in a future version. This property should not be used in TensorFlow 2.0, as `updates` are applied automatically.
updates = self.state_updates
60000/60000 [==============================] - 204s 3ms/sample - loss: 0.3743 - acc: 0.9021 - val_loss: 0.3241 - val_acc: 0.9227
Epoch 3/3
60000/60000 [==============================] - 203s 3ms/sample - loss: 0.3471 - acc: 0.9199 - val_loss: 0.3105 - val_acc: 0.9339
<keras.src.callbacks.History at 0x7ccded1f8850>
compute_dp_sgd_privacy.compute_dp_sgd_privacy(n=train_data.shape[0],
batch_size=batch_size,
noise_multiplier=noise_multiplier,
epochs=epochs,
delta=1e-5)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-13-abb2ef715897> in <cell line: 1>()
----> 1 compute_dp_sgd_privacy.compute_dp_sgd_privacy(n=train_data.shape[0],
2 batch_size=batch_size,
3 noise_multiplier=noise_multiplier,
4 epochs=epochs,
5 delta=1e-5)
AttributeError: module 'tensorflow_privacy.privacy.analysis.compute_dp_sgd_privacy' has no attribute 'compute_dp_sgd_privacy'
I received the following error, which is not the first time I’ve encountered such issues when using TensorFlow Privacy.
However, the TensorFlow website shows the following output for the code above:
DP-SGD with sampling rate = 0.417% and noise_multiplier = 1.3 iterated over 720 steps satisfies differential privacy with eps = 0.563 and delta = 1e-05.
The optimal RDP order is 18.0.
(0.5631726490328062, 18.0)
Given this situation, it seems challenging to implement code using TensorFlow Privacy.
I would like to either fix the error in the reference code provided by the TensorFlow website and understand its cause or, if fixing it is not possible, find an alternative approach to TensorFlow Privacy for implementing Federated Learning (FL) with Differential Privacy.
Note: I have also tested similar code with other versions of TensorFlow and TensorFlow Privacy, but similar issues persist.