Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions tensorflow_privacy/privacy/optimizers/dp_optimizer_keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,11 @@ def _compute_gradients(self, loss, var_list, grad_loss=None, tape=None):
else:
num_microbatches = self._num_microbatches
microbatch_losses = tf.reduce_mean(
tf.reshape(loss, [num_microbatches, -1]), axis=1)
tf.reshape(
loss,
[num_microbatches,
tf.shape(loss)[0] / num_microbatches]),
axis=1)

if callable(var_list):
var_list = var_list()
Expand All @@ -250,7 +254,11 @@ def _compute_gradients(self, loss, var_list, grad_loss=None, tape=None):
else:
num_microbatches = self._num_microbatches
microbatch_losses = tf.reduce_mean(
tf.reshape(loss, [num_microbatches, -1]), axis=1)
tf.reshape(
loss,
[num_microbatches,
tf.shape(loss)[0] / num_microbatches]),
axis=1)

var_list = tf.nest.flatten(var_list)

Expand Down Expand Up @@ -294,7 +302,10 @@ def get_gradients(self, loss, params):
# This code mostly follows the logic in the original DPOptimizerClass
# in dp_optimizer.py, except that this returns only the gradients,
# not the gradients and variables.
microbatch_losses = tf.reshape(loss, [self._num_microbatches, -1])
microbatch_losses = tf.reshape(
loss,
[self._num_microbatches,
tf.shape(loss)[0] / self._num_microbatches])
sample_params = (
self._dp_sum_query.derive_sample_params(self._global_state))

Expand Down