Tensorflow:深度学习梯度泄漏的第二阶导数学习

时间:2020-08-22 15:22:30

标签: python tensorflow keras deep-learning neural-network

我正在尝试从本文中重新创建结果:http://papers.nips.cc/paper/9617-deep-leakage-from-gradients.pdf

其要点是它试图使用模型权重和梯度来重新创建输入和输出训练数据(这对于分布式学习很重要)。这是他们的设置:

  1. 使用训练批次正常计算模型的梯度。
  2. 初始化形状与训练数据相同的随机训练数据,表示为假数据。
  3. 使用相同模型基于假数据计算假梯度。
  4. 计算真实梯度和假梯度之间的MSE。
  5. 通过虚假数据计算此MSE的梯度。
  6. 使用渐变更新伪造数据
  7. 重复3-6次几百次
  8. 此后,假数据应与输入数据非常相似

我在Tensorflow中努力实现的部分是步骤4和5。我不确定如何正确处理二阶导数。

这是我天真的做法:

# x_batch_train is the input data
# y_batch_train is the output data
# fakex is the fake input data
# fakey is the fake output data
fakex = tf.convert_to_tensor(np.random.random(x_batch_train.numpy().shape))
fakey = tf.convert_to_tensor(np.random.random(y_batch_train.numpy().shape))

with tf.GradientTape(persistent=True) as tape:
    logits = model(x_batch_train, training=True)
    # The normal loss using real data
    loss_value = loss_fn(y_batch_train, logits)

    fakeLogits = model(fakex, training=True)
    # The fake loss using fake data
    fake_loss_value = loss_fn(fakey, fakeLogits)

# The normal gradient
grads = tape.gradient(loss_value, model.trainable_weights)

# The fake gradient
fakegrads = tape.gradient(fake_loss_value, model.trainable_weights)

# The MSE of the normal gradient and the fake gradient
inputloss = tf.keras.losses.mean_squared_error(grads, fakegrads)

# The gradient wrt to the fake input data
inputGrad = tape.gradient(inputloss, fakex)
# The gradient wrt to the fake outputdata
outputGrad = tape.gradient(inputloss, fakey)

print(inputGrad)
print(outputGrad)

到目前为止,我尝试过的所有操作都导致输入grad和outputgrad的错误或返回None。如何使用tape.gradient进行这些二阶导数?假设可以/应该用tape.gradient完成。

谢谢!

0 个答案:

没有答案
相关问题