forked from 626_privacy/tensorflow_privacy
Fixing new pylint errors.
This commit is contained in:
parent
92f97ae32c
commit
71c4a11eb9
5 changed files with 61 additions and 63 deletions
|
@ -56,7 +56,7 @@ class StrongConvexMixin:
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
class_weight: the class weights as scalar or 1d tensor, where its
|
class_weight: the class weights as scalar or 1d tensor, where its
|
||||||
dimensionality is equal to the number of outputs.
|
dimensionality is equal to the number of outputs.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Beta
|
Beta
|
||||||
|
@ -115,7 +115,7 @@ class StrongConvexHuber(losses.Loss, StrongConvexMixin):
|
||||||
C: Penalty parameter C of the loss term
|
C: Penalty parameter C of the loss term
|
||||||
radius_constant: constant defining the length of the radius
|
radius_constant: constant defining the length of the radius
|
||||||
delta: delta value in huber loss. When to switch from quadratic to
|
delta: delta value in huber loss. When to switch from quadratic to
|
||||||
absolute deviation.
|
absolute deviation.
|
||||||
reduction: reduction type to use. See super class
|
reduction: reduction type to use. See super class
|
||||||
name: Name of the loss instance
|
name: Name of the loss instance
|
||||||
dtype: tf datatype to use for tensor conversions.
|
dtype: tf datatype to use for tensor conversions.
|
||||||
|
|
|
@ -180,20 +180,20 @@ class BoltonModel(Model): # pylint: disable=abstract-method
|
||||||
n_samples=None,
|
n_samples=None,
|
||||||
steps_per_epoch=None,
|
steps_per_epoch=None,
|
||||||
**kwargs): # pylint: disable=arguments-differ
|
**kwargs): # pylint: disable=arguments-differ
|
||||||
"""Fit with a generator..
|
"""Fit with a generator.
|
||||||
|
|
||||||
This method is the same as fit except for when the passed dataset
|
This method is the same as fit except for when the passed dataset
|
||||||
is a generator. See super method and fit for more details.
|
is a generator. See super method and fit for more details.
|
||||||
|
|
||||||
Args:
|
|
||||||
n_samples: number of individual samples in x
|
|
||||||
noise_distribution: the distribution to get noise from.
|
|
||||||
epsilon: privacy parameter, which trades off utility and privacy. See
|
|
||||||
Bolton paper for more description.
|
|
||||||
class_weight: the class weights to be used. Can be a scalar or 1D tensor
|
|
||||||
whose dim == n_classes.
|
|
||||||
|
|
||||||
See the super method for descriptions on the rest of the arguments.
|
Args:
|
||||||
|
n_samples: number of individual samples in x
|
||||||
|
noise_distribution: the distribution to get noise from.
|
||||||
|
epsilon: privacy parameter, which trades off utility and privacy. See
|
||||||
|
Bolton paper for more description.
|
||||||
|
class_weight: the class weights to be used. Can be a scalar or 1D tensor
|
||||||
|
whose dim == n_classes.
|
||||||
|
|
||||||
|
See the super method for descriptions on the rest of the arguments.
|
||||||
"""
|
"""
|
||||||
if class_weight is None:
|
if class_weight is None:
|
||||||
class_weight = self.calculate_class_weights(class_weight)
|
class_weight = self.calculate_class_weights(class_weight)
|
||||||
|
@ -227,14 +227,14 @@ class BoltonModel(Model): # pylint: disable=abstract-method
|
||||||
num_classes=None):
|
num_classes=None):
|
||||||
"""Calculates class weighting to be used in training.
|
"""Calculates class weighting to be used in training.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
class_weights: str specifying type, array giving weights, or None.
|
class_weights: str specifying type, array giving weights, or None.
|
||||||
class_counts: If class_weights is not None, then an array of
|
class_counts: If class_weights is not None, then an array of
|
||||||
the number of samples for each class
|
the number of samples for each class
|
||||||
num_classes: If class_weights is not None, then the number of
|
num_classes: If class_weights is not None, then the number of
|
||||||
classes.
|
classes.
|
||||||
Returns:
|
Returns:
|
||||||
class_weights as 1D tensor, to be passed to model's fit method.
|
class_weights as 1D tensor, to be passed to model's fit method.
|
||||||
"""
|
"""
|
||||||
# Value checking
|
# Value checking
|
||||||
class_keys = ['balanced']
|
class_keys = ['balanced']
|
||||||
|
|
|
@ -43,7 +43,7 @@ class TestLoss(losses.Loss, StrongConvexMixin):
|
||||||
|
|
||||||
W is a convex set that forms the hypothesis space.
|
W is a convex set that forms the hypothesis space.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
radius
|
radius
|
||||||
"""
|
"""
|
||||||
return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
|
return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
|
||||||
|
@ -70,7 +70,7 @@ class TestLoss(losses.Loss, StrongConvexMixin):
|
||||||
Args:
|
Args:
|
||||||
class_weight: class weights used
|
class_weight: class weights used
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
L
|
L
|
||||||
"""
|
"""
|
||||||
return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
|
return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
|
||||||
|
@ -175,12 +175,12 @@ class InitTests(keras_parameterized.TestCase):
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
def test_compile(self, n_outputs, loss, optimizer):
|
def test_compile(self, n_outputs, loss, optimizer):
|
||||||
"""test compilation of BoltonModel.
|
"""Test compilation of BoltonModel.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
n_outputs: number of output neurons
|
n_outputs: number of output neurons
|
||||||
loss: instantiated TestLoss instance
|
loss: instantiated TestLoss instance
|
||||||
optimizer: instanced TestOptimizer instance
|
optimizer: instantiated TestOptimizer instance
|
||||||
"""
|
"""
|
||||||
# test compilation of valid tf.optimizer and tf.loss
|
# test compilation of valid tf.optimizer and tf.loss
|
||||||
with self.cached_session():
|
with self.cached_session():
|
||||||
|
@ -206,7 +206,7 @@ class InitTests(keras_parameterized.TestCase):
|
||||||
Args:
|
Args:
|
||||||
n_outputs: number of output neurons
|
n_outputs: number of output neurons
|
||||||
loss: instantiated TestLoss instance
|
loss: instantiated TestLoss instance
|
||||||
optimizer: instanced TestOptimizer instance
|
optimizer: instantiated TestOptimizer instance
|
||||||
"""
|
"""
|
||||||
# test compilaton of invalid tf.optimizer and non instantiated loss.
|
# test compilaton of invalid tf.optimizer and non instantiated loss.
|
||||||
with self.cached_session():
|
with self.cached_session():
|
||||||
|
@ -262,17 +262,17 @@ def _do_fit(n_samples,
|
||||||
"""Instantiate necessary components for fitting and perform a model fit.
|
"""Instantiate necessary components for fitting and perform a model fit.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
n_samples: number of samples in dataset
|
n_samples: number of samples in dataset
|
||||||
input_dim: the sample dimensionality
|
input_dim: the sample dimensionality
|
||||||
n_outputs: number of output neurons
|
n_outputs: number of output neurons
|
||||||
epsilon: privacy parameter
|
epsilon: privacy parameter
|
||||||
generator: True to create a generator, False to use an iterator
|
generator: True to create a generator, False to use an iterator
|
||||||
batch_size: batch_size to use
|
batch_size: batch_size to use
|
||||||
reset_n_samples: True to set _samples to None prior to fitting.
|
reset_n_samples: True to set _samples to None prior to fitting.
|
||||||
False does nothing
|
False does nothing
|
||||||
optimizer: instance of TestOptimizer
|
optimizer: instance of TestOptimizer
|
||||||
loss: instance of TestLoss
|
loss: instance of TestLoss
|
||||||
distribution: distribution to get noise from.
|
distribution: distribution to get noise from.
|
||||||
|
|
||||||
Returns: BoltonModel instsance
|
Returns: BoltonModel instsance
|
||||||
"""
|
"""
|
||||||
|
@ -329,8 +329,8 @@ class FitTests(keras_parameterized.TestCase):
|
||||||
"""Tests fitting of BoltonModel.
|
"""Tests fitting of BoltonModel.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
generator: True for generator test, False for iterator test.
|
generator: True for generator test, False for iterator test.
|
||||||
reset_n_samples: True to reset the n_samples to None, False does nothing
|
reset_n_samples: True to reset the n_samples to None, False does nothing
|
||||||
"""
|
"""
|
||||||
loss = TestLoss(1, 1, 1)
|
loss = TestLoss(1, 1, 1)
|
||||||
optimizer = Bolton(TestOptimizer(), loss)
|
optimizer = Bolton(TestOptimizer(), loss)
|
||||||
|
@ -398,10 +398,10 @@ class FitTests(keras_parameterized.TestCase):
|
||||||
"""Tests fitting with invalid parameters, which should raise an error.
|
"""Tests fitting with invalid parameters, which should raise an error.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
generator: True to test with generator, False is iterator
|
generator: True to test with generator, False is iterator
|
||||||
reset_n_samples: True to reset the n_samples param to None prior to
|
reset_n_samples: True to reset the n_samples param to None prior to
|
||||||
passing it to fit
|
passing it to fit
|
||||||
distribution: distribution to get noise from.
|
distribution: distribution to get noise from.
|
||||||
"""
|
"""
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
loss = TestLoss(1, 1, 1)
|
loss = TestLoss(1, 1, 1)
|
||||||
|
@ -505,21 +505,20 @@ class FitTests(keras_parameterized.TestCase):
|
||||||
'num_classes': 2,
|
'num_classes': 2,
|
||||||
'err_msg': 'Detected array length:'},
|
'err_msg': 'Detected array length:'},
|
||||||
])
|
])
|
||||||
|
|
||||||
def test_class_errors(self,
|
def test_class_errors(self,
|
||||||
class_weights,
|
class_weights,
|
||||||
class_counts,
|
class_counts,
|
||||||
num_classes,
|
num_classes,
|
||||||
err_msg):
|
err_msg):
|
||||||
"""Tests the BOltonModel calculate_class_weights method.
|
"""Tests the BOltonModel calculate_class_weights method.
|
||||||
|
|
||||||
This test passes invalid params which should raise the expected errors.
|
This test passes invalid params which should raise the expected errors.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
class_weights: the class_weights to use
|
class_weights: the class_weights to use.
|
||||||
class_counts: count of number of samples for each class
|
class_counts: count of number of samples for each class.
|
||||||
num_classes: number of outputs neurons
|
num_classes: number of outputs neurons.
|
||||||
err_msg:
|
err_msg: The expected error message.
|
||||||
"""
|
"""
|
||||||
clf = models.BoltonModel(1, 1)
|
clf = models.BoltonModel(1, 1)
|
||||||
with self.assertRaisesRegexp(ValueError, err_msg): # pylint: disable=deprecated-method
|
with self.assertRaisesRegexp(ValueError, err_msg): # pylint: disable=deprecated-method
|
||||||
|
|
|
@ -310,12 +310,11 @@ class Bolton(optimizer_v2.OptimizerV2):
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
noise_distribution: the noise distribution to pick.
|
noise_distribution: the noise distribution to pick.
|
||||||
see _accepted_distributions and get_noise for
|
see _accepted_distributions and get_noise for possible values.
|
||||||
possible values.
|
|
||||||
epsilon: privacy parameter. Lower gives more privacy but less utility.
|
epsilon: privacy parameter. Lower gives more privacy but less utility.
|
||||||
layers: list of Keras/Tensorflow layers. Can be found as model.layers
|
layers: list of Keras/Tensorflow layers. Can be found as model.layers
|
||||||
class_weights: class_weights used, which may either be a scalar or 1D
|
class_weights: class_weights used, which may either be a scalar or 1D
|
||||||
tensor with dim == n_classes.
|
tensor with dim == n_classes.
|
||||||
n_samples number of rows/individual samples in the training set
|
n_samples number of rows/individual samples in the training set
|
||||||
batch_size: batch size used.
|
batch_size: batch size used.
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -209,7 +209,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase):
|
||||||
args: args to optimizer fn
|
args: args to optimizer fn
|
||||||
result: the expected result
|
result: the expected result
|
||||||
test_attr: None if the fn returns the test result. Otherwise, this is
|
test_attr: None if the fn returns the test result. Otherwise, this is
|
||||||
the attribute of Bolton to check against result with.
|
the attribute of Bolton to check against result with.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
tf.random.set_seed(1)
|
tf.random.set_seed(1)
|
||||||
|
@ -303,9 +303,9 @@ class BoltonOptimizerTest(keras_parameterized.TestCase):
|
||||||
"""Tests the context manager functionality of the optimizer.
|
"""Tests the context manager functionality of the optimizer.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
noise: noise distribution to pick
|
noise: noise distribution to pick
|
||||||
epsilon: epsilon privacy parameter to use
|
epsilon: epsilon privacy parameter to use
|
||||||
class_weights: class_weights to use
|
class_weights: class_weights to use
|
||||||
"""
|
"""
|
||||||
@tf.function
|
@tf.function
|
||||||
def test_run():
|
def test_run():
|
||||||
|
@ -336,9 +336,9 @@ class BoltonOptimizerTest(keras_parameterized.TestCase):
|
||||||
"""Tests the context domains.
|
"""Tests the context domains.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
noise: noise distribution to pick
|
noise: noise distribution to pick
|
||||||
epsilon: epsilon privacy parameter to use
|
epsilon: epsilon privacy parameter to use
|
||||||
err_msg: the expected error message
|
err_msg: the expected error message
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -456,7 +456,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase):
|
||||||
])
|
])
|
||||||
def test_not_reroute_fn(self, fn, args):
|
def test_not_reroute_fn(self, fn, args):
|
||||||
"""Test function is not rerouted.
|
"""Test function is not rerouted.
|
||||||
|
|
||||||
Test that a fn that should not be rerouted to the internal optimizer is
|
Test that a fn that should not be rerouted to the internal optimizer is
|
||||||
in fact not rerouted.
|
in fact not rerouted.
|
||||||
|
|
||||||
|
@ -495,7 +495,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase):
|
||||||
])
|
])
|
||||||
def test_reroute_attr(self, attr):
|
def test_reroute_attr(self, attr):
|
||||||
"""Test a function is rerouted.
|
"""Test a function is rerouted.
|
||||||
|
|
||||||
Test that attribute of internal optimizer is correctly rerouted to the
|
Test that attribute of internal optimizer is correctly rerouted to the
|
||||||
internal optimizer.
|
internal optimizer.
|
||||||
|
|
||||||
|
@ -514,7 +514,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase):
|
||||||
])
|
])
|
||||||
def test_attribute_error(self, attr):
|
def test_attribute_error(self, attr):
|
||||||
"""Test rerouting of attributes.
|
"""Test rerouting of attributes.
|
||||||
|
|
||||||
Test that attribute of internal optimizer is correctly rerouted to the
|
Test that attribute of internal optimizer is correctly rerouted to the
|
||||||
internal optimizer
|
internal optimizer
|
||||||
|
|
||||||
|
@ -561,7 +561,7 @@ class SchedulerTest(keras_parameterized.TestCase):
|
||||||
])
|
])
|
||||||
def test_call(self, step, res):
|
def test_call(self, step, res):
|
||||||
"""Test call.
|
"""Test call.
|
||||||
|
|
||||||
Test that attribute of internal optimizer is correctly rerouted to the
|
Test that attribute of internal optimizer is correctly rerouted to the
|
||||||
internal optimizer
|
internal optimizer
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue