8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

TensorFlow 中的差分隐私错误

Dpedrinha 2月前

22 0

我正在使用 Jupyter Notebook 测试来自 TensorFlow 网站的示例代码。您可以在以下链接找到该代码:https://www.tensorflow.org/responsible_ai/privacy/tutorials/

使用 的示例代码 TensorFlow Jupyter Notebook 。您可以在以下链接中找到该代码:

https://www.tensorflow.org/responsible_ai/privacy/tutorials/classification_privacy

以下是代码以及 我的计算机上安装的 TensorFlowTensorFlow Privacy

!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'

我收到了上述错误,这不是我第一次在使用 TensorFlow Privacy 。但是, TensorFlow 网站显示了上述代码的以下输出:

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)

实现代码似乎很有挑战性 TensorFlow Privacy 。我想修复 TensorFlow 网站提供的参考代码中的错误并了解其原因,或者,如果无法修复,则找到 TensorFlow Privacy 的替代方法,以使用差异隐私实现 联邦 学习 (FL) .

注意: TensorFlow 测试了类似的代码 TensorFlow Privacy ,但类似的问题仍然存在。

帖子版权声明 1、本帖标题:TensorFlow 中的差分隐私错误
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Dpedrinha在本站《tensorflow》版块原创发布, 转载请注明出处!
最新回复 (0)
返回
作者最近主题: