Changes for pylint.

This commit is contained in:
Christopher Choquette Choo 2019-06-19 11:14:02 -04:00
parent 56e16f0a15
commit b120d9c5d8
4 changed files with 8 additions and 303 deletions

View file

@ -317,283 +317,3 @@ class StrongConvexBinaryCrossentropy(
:return: :return:
""" """
return L1L2(l2=self.reg_lambda/2) return L1L2(l2=self.reg_lambda/2)
# class StrongConvexSparseCategoricalCrossentropy(
# losses.CategoricalCrossentropy,
# StrongConvexMixin
# ):
# """
# Strong Convex version of CategoricalCrossentropy loss using l2 weight
# regularization.
# """
#
# def __init__(self,
# reg_lambda: float,
# C: float,
# radius_constant: float,
# from_logits: bool = True,
# label_smoothing: float = 0,
# reduction: str = losses_utils.ReductionV2.SUM_OVER_BATCH_SIZE,
# name: str = 'binarycrossentropy',
# dtype=tf.float32):
# """
# Args:
# reg_lambda: Weight regularization constant
# C: Penalty parameter C of the loss term
# radius_constant: constant defining the length of the radius
# reduction: reduction type to use. See super class
# label_smoothing: amount of smoothing to perform on labels
# relaxation of trust in labels, e.g. (1 -> 1-x, 0 -> 0+x)
# name: Name of the loss instance
# dtype: tf datatype to use for tensor conversions.
# """
# if reg_lambda <= 0:
# raise ValueError("reg lambda: {0} must be positive".format(reg_lambda))
# if C <= 0:
# raise ValueError('c: {0}, should be >= 0'.format(C))
# if radius_constant <= 0:
# raise ValueError('radius_constant: {0}, should be >= 0'.format(
# radius_constant
# ))
#
# self.C = C
# self.dtype = dtype
# self.reg_lambda = tf.constant(reg_lambda, dtype=self.dtype)
# super(StrongConvexSparseCategoricalCrossentropy, self).__init__(
# reduction=reduction,
# name=name,
# from_logits=from_logits,
# label_smoothing=label_smoothing,
# )
# self.radius_constant = radius_constant
#
# def call(self, y_true, y_pred):
# """Compute loss
#
# Args:
# y_true: Ground truth values.
# y_pred: The predicted values.
#
# Returns:
# Loss values per sample.
# """
# loss = super()
# loss = loss * self.C
# return loss
#
# def radius(self):
# """See super class.
# """
# return self.radius_constant / self.reg_lambda
#
# def gamma(self):
# """See super class.
# """
# return self.reg_lambda
#
# def beta(self, class_weight):
# """See super class.
# """
# max_class_weight = self.max_class_weight(class_weight, self.dtype)
# return self.C * max_class_weight + self.reg_lambda
#
# def lipchitz_constant(self, class_weight):
# """See super class.
# """
# max_class_weight = self.max_class_weight(class_weight, self.dtype)
# return self.C * max_class_weight + self.reg_lambda * self.radius()
#
# def kernel_regularizer(self):
# """
# l2 loss using reg_lambda as the l2 term (as desired). Required for
# this loss function to be strongly convex.
# :return:
# """
# return L1L2(l2=self.reg_lambda)
#
# class StrongConvexSparseCategoricalCrossentropy(
# losses.SparseCategoricalCrossentropy,
# StrongConvexMixin
# ):
# """
# Strong Convex version of SparseCategoricalCrossentropy loss using l2 weight
# regularization.
# """
#
# def __init__(self,
# reg_lambda: float,
# C: float,
# radius_constant: float,
# from_logits: bool = True,
# label_smoothing: float = 0,
# reduction: str = losses_utils.ReductionV2.SUM_OVER_BATCH_SIZE,
# name: str = 'binarycrossentropy',
# dtype=tf.float32):
# """
# Args:
# reg_lambda: Weight regularization constant
# C: Penalty parameter C of the loss term
# radius_constant: constant defining the length of the radius
# reduction: reduction type to use. See super class
# label_smoothing: amount of smoothing to perform on labels
# relaxation of trust in labels, e.g. (1 -> 1-x, 0 -> 0+x)
# name: Name of the loss instance
# dtype: tf datatype to use for tensor conversions.
# """
# if reg_lambda <= 0:
# raise ValueError("reg lambda: {0} must be positive".format(reg_lambda))
# if C <= 0:
# raise ValueError('c: {0}, should be >= 0'.format(C))
# if radius_constant <= 0:
# raise ValueError('radius_constant: {0}, should be >= 0'.format(
# radius_constant
# ))
#
# self.C = C
# self.dtype = dtype
# self.reg_lambda = tf.constant(reg_lambda, dtype=self.dtype)
# super(StrongConvexHuber, self).__init__(reduction=reduction,
# name=name,
# from_logits=from_logits,
# label_smoothing=label_smoothing,
# )
# self.radius_constant = radius_constant
#
# def call(self, y_true, y_pred):
# """Compute loss
#
# Args:
# y_true: Ground truth values.
# y_pred: The predicted values.
#
# Returns:
# Loss values per sample.
# """
# loss = super()
# loss = loss * self.C
# return loss
#
# def radius(self):
# """See super class.
# """
# return self.radius_constant / self.reg_lambda
#
# def gamma(self):
# """See super class.
# """
# return self.reg_lambda
#
# def beta(self, class_weight):
# """See super class.
# """
# max_class_weight = self.max_class_weight(class_weight, self.dtype)
# return self.C * max_class_weight + self.reg_lambda
#
# def lipchitz_constant(self, class_weight):
# """See super class.
# """
# max_class_weight = self.max_class_weight(class_weight, self.dtype)
# return self.C * max_class_weight + self.reg_lambda * self.radius()
#
# def kernel_regularizer(self):
# """
# l2 loss using reg_lambda as the l2 term (as desired). Required for
# this loss function to be strongly convex.
# :return:
# """
# return L1L2(l2=self.reg_lambda)
#
#
# class StrongConvexCategoricalCrossentropy(
# losses.CategoricalCrossentropy,
# StrongConvexMixin
# ):
# """
# Strong Convex version of CategoricalCrossentropy loss using l2 weight
# regularization.
# """
#
# def __init__(self,
# reg_lambda: float,
# C: float,
# radius_constant: float,
# from_logits: bool = True,
# label_smoothing: float = 0,
# reduction: str = losses_utils.ReductionV2.SUM_OVER_BATCH_SIZE,
# name: str = 'binarycrossentropy',
# dtype=tf.float32):
# """
# Args:
# reg_lambda: Weight regularization constant
# C: Penalty parameter C of the loss term
# radius_constant: constant defining the length of the radius
# reduction: reduction type to use. See super class
# label_smoothing: amount of smoothing to perform on labels
# relaxation of trust in labels, e.g. (1 -> 1-x, 0 -> 0+x)
# name: Name of the loss instance
# dtype: tf datatype to use for tensor conversions.
# """
# if reg_lambda <= 0:
# raise ValueError("reg lambda: {0} must be positive".format(reg_lambda))
# if C <= 0:
# raise ValueError('c: {0}, should be >= 0'.format(C))
# if radius_constant <= 0:
# raise ValueError('radius_constant: {0}, should be >= 0'.format(
# radius_constant
# ))
#
# self.C = C
# self.dtype = dtype
# self.reg_lambda = tf.constant(reg_lambda, dtype=self.dtype)
# super(StrongConvexHuber, self).__init__(reduction=reduction,
# name=name,
# from_logits=from_logits,
# label_smoothing=label_smoothing,
# )
# self.radius_constant = radius_constant
#
# def call(self, y_true, y_pred):
# """Compute loss
#
# Args:
# y_true: Ground truth values.
# y_pred: The predicted values.
#
# Returns:
# Loss values per sample.
# """
# loss = super()
# loss = loss * self.C
# return loss
#
# def radius(self):
# """See super class.
# """
# return self.radius_constant / self.reg_lambda
#
# def gamma(self):
# """See super class.
# """
# return self.reg_lambda
#
# def beta(self, class_weight):
# """See super class.
# """
# max_class_weight = self.max_class_weight(class_weight, self.dtype)
# return self.C * max_class_weight + self.reg_lambda
#
# def lipchitz_constant(self, class_weight):
# """See super class.
# """
# max_class_weight = self.max_class_weight(class_weight, self.dtype)
# return self.C * max_class_weight + self.reg_lambda * self.radius()
#
# def kernel_regularizer(self):
# """
# l2 loss using reg_lambda as the l2 term (as desired). Required for
# this loss function to be strongly convex.
# :return:
# """
# return L1L2(l2=self.reg_lambda)

View file

@ -53,7 +53,7 @@ class TestLoss(losses.Loss, StrongConvexMixin):
""" """
return _ops.convert_to_tensor_v2(1, dtype=tf.float32) return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
def beta(self, class_weight): def beta(self, class_weight): # pylint: disable=unused-argument
"""Beta smoothess """Beta smoothess
Args: Args:
@ -64,7 +64,7 @@ class TestLoss(losses.Loss, StrongConvexMixin):
""" """
return _ops.convert_to_tensor_v2(1, dtype=tf.float32) return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
def lipchitz_constant(self, class_weight): def lipchitz_constant(self, class_weight): # pylint: disable=unused-argument
""" L lipchitz continuous """ L lipchitz continuous
Args: Args:

View file

@ -20,7 +20,6 @@ from __future__ import print_function
import tensorflow as tf import tensorflow as tf
from tensorflow.python.keras.optimizer_v2 import optimizer_v2 from tensorflow.python.keras.optimizer_v2 import optimizer_v2
from tensorflow.python.ops import math_ops from tensorflow.python.ops import math_ops
from tensorflow.python import ops as _ops
from privacy.bolton.losses import StrongConvexMixin from privacy.bolton.losses import StrongConvexMixin
_accepted_distributions = ['laplace'] # implemented distributions for noising _accepted_distributions = ['laplace'] # implemented distributions for noising

View file

@ -25,7 +25,6 @@ from tensorflow.python.keras.regularizers import L1L2
from tensorflow.python.keras.initializers import constant from tensorflow.python.keras.initializers import constant
from tensorflow.python.keras import losses from tensorflow.python.keras import losses
from tensorflow.python.keras.models import Model from tensorflow.python.keras.models import Model
from tensorflow.python.framework import ops as _ops
from tensorflow.python.framework import test_util from tensorflow.python.framework import test_util
from tensorflow.python import ops as _ops from tensorflow.python import ops as _ops
from absl.testing import parameterized from absl.testing import parameterized
@ -33,7 +32,6 @@ from privacy.bolton.losses import StrongConvexMixin
from privacy.bolton import optimizers as opt from privacy.bolton import optimizers as opt
class TestModel(Model): class TestModel(Model):
""" """
Bolton episilon-delta model Bolton episilon-delta model
@ -69,18 +67,6 @@ class TestModel(Model):
) )
# def call(self, inputs):
# """Forward pass of network
#
# Args:
# inputs: inputs to neural network
#
# Returns:
#
# """
# return self.output_layer(inputs)
class TestLoss(losses.Loss, StrongConvexMixin): class TestLoss(losses.Loss, StrongConvexMixin):
"""Test loss function for testing Bolton model""" """Test loss function for testing Bolton model"""
def __init__(self, reg_lambda, C, radius_constant, name='test'): def __init__(self, reg_lambda, C, radius_constant, name='test'):
@ -105,7 +91,7 @@ class TestLoss(losses.Loss, StrongConvexMixin):
""" """
return _ops.convert_to_tensor_v2(1, dtype=tf.float32) return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
def beta(self, class_weight): def beta(self, class_weight): # pylint: disable=unused-argument
"""Beta smoothess """Beta smoothess
Args: Args:
@ -116,7 +102,7 @@ class TestLoss(losses.Loss, StrongConvexMixin):
""" """
return _ops.convert_to_tensor_v2(1, dtype=tf.float32) return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
def lipchitz_constant(self, class_weight): def lipchitz_constant(self, class_weight): # pylint: disable=unused-argument
""" L lipchitz continuous """ L lipchitz continuous
Args: Args:
@ -217,7 +203,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase):
model.layers[0].kernel = \ model.layers[0].kernel = \
model.layers[0].kernel_initializer((model.layer_input_shape[0], model.layers[0].kernel_initializer((model.layer_input_shape[0],
model.n_outputs)) model.n_outputs))
bolton._is_init = True bolton._is_init = True # pylint: disable=protected-access
bolton.layers = model.layers bolton.layers = model.layers
bolton.epsilon = 2 bolton.epsilon = 2
bolton.noise_distribution = 'laplace' bolton.noise_distribution = 'laplace'
@ -279,7 +265,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase):
model.layers[0].kernel = \ model.layers[0].kernel = \
model.layers[0].kernel_initializer((model.layer_input_shape[0], model.layers[0].kernel_initializer((model.layer_input_shape[0],
model.n_outputs)) model.n_outputs))
bolton._is_init = True bolton._is_init = True # pylint: disable=protected-access
bolton.layers = model.layers bolton.layers = model.layers
bolton.epsilon = 2 bolton.epsilon = 2
bolton.noise_distribution = 'laplace' bolton.noise_distribution = 'laplace'
@ -431,7 +417,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase):
model.layers[0].kernel = \ model.layers[0].kernel = \
model.layers[0].kernel_initializer((model.layer_input_shape[0], model.layers[0].kernel_initializer((model.layer_input_shape[0],
model.n_outputs)) model.n_outputs))
bolton._is_init = True bolton._is_init = True # pylint: disable=protected-access
bolton.layers = model.layers bolton.layers = model.layers
bolton.epsilon = 2 bolton.epsilon = 2
bolton.noise_distribution = 'laplace' bolton.noise_distribution = 'laplace'
@ -467,7 +453,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase):
model.layers[0].kernel = \ model.layers[0].kernel = \
model.layers[0].kernel_initializer((model.layer_input_shape[0], model.layers[0].kernel_initializer((model.layer_input_shape[0],
model.n_outputs)) model.n_outputs))
bolton._is_init = True bolton._is_init = True # pylint: disable=protected-access
bolton.noise_distribution = 'laplace' bolton.noise_distribution = 'laplace'
bolton.epsilon = 1 bolton.epsilon = 1
bolton.layers = model.layers bolton.layers = model.layers