From b120d9c5d84fe03d88ca78ce297d748f6ef38cf7 Mon Sep 17 00:00:00 2001 From: Christopher Choquette Choo Date: Wed, 19 Jun 2019 11:14:02 -0400 Subject: [PATCH] Changes for pylint. --- privacy/bolton/losses.py | 280 ------------------------------ privacy/bolton/models_test.py | 4 +- privacy/bolton/optimizers.py | 1 - privacy/bolton/optimizers_test.py | 26 +-- 4 files changed, 8 insertions(+), 303 deletions(-) diff --git a/privacy/bolton/losses.py b/privacy/bolton/losses.py index 6a54576..a99187b 100644 --- a/privacy/bolton/losses.py +++ b/privacy/bolton/losses.py @@ -317,283 +317,3 @@ class StrongConvexBinaryCrossentropy( :return: """ 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) - diff --git a/privacy/bolton/models_test.py b/privacy/bolton/models_test.py index 05119d3..63954cc 100644 --- a/privacy/bolton/models_test.py +++ b/privacy/bolton/models_test.py @@ -53,7 +53,7 @@ class TestLoss(losses.Loss, StrongConvexMixin): """ 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 Args: @@ -64,7 +64,7 @@ class TestLoss(losses.Loss, StrongConvexMixin): """ 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 Args: diff --git a/privacy/bolton/optimizers.py b/privacy/bolton/optimizers.py index 28c1735..ec7a7e5 100644 --- a/privacy/bolton/optimizers.py +++ b/privacy/bolton/optimizers.py @@ -20,7 +20,6 @@ from __future__ import print_function import tensorflow as tf from tensorflow.python.keras.optimizer_v2 import optimizer_v2 from tensorflow.python.ops import math_ops -from tensorflow.python import ops as _ops from privacy.bolton.losses import StrongConvexMixin _accepted_distributions = ['laplace'] # implemented distributions for noising diff --git a/privacy/bolton/optimizers_test.py b/privacy/bolton/optimizers_test.py index 1d0fbfb..6a499fc 100644 --- a/privacy/bolton/optimizers_test.py +++ b/privacy/bolton/optimizers_test.py @@ -25,7 +25,6 @@ from tensorflow.python.keras.regularizers import L1L2 from tensorflow.python.keras.initializers import constant from tensorflow.python.keras import losses 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 import ops as _ops from absl.testing import parameterized @@ -33,7 +32,6 @@ from privacy.bolton.losses import StrongConvexMixin from privacy.bolton import optimizers as opt - class TestModel(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): """Test loss function for testing Bolton model""" 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) - def beta(self, class_weight): + def beta(self, class_weight): # pylint: disable=unused-argument """Beta smoothess Args: @@ -116,7 +102,7 @@ class TestLoss(losses.Loss, StrongConvexMixin): """ 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 Args: @@ -217,7 +203,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase): model.layers[0].kernel = \ model.layers[0].kernel_initializer((model.layer_input_shape[0], model.n_outputs)) - bolton._is_init = True + bolton._is_init = True # pylint: disable=protected-access bolton.layers = model.layers bolton.epsilon = 2 bolton.noise_distribution = 'laplace' @@ -279,7 +265,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase): model.layers[0].kernel = \ model.layers[0].kernel_initializer((model.layer_input_shape[0], model.n_outputs)) - bolton._is_init = True + bolton._is_init = True # pylint: disable=protected-access bolton.layers = model.layers bolton.epsilon = 2 bolton.noise_distribution = 'laplace' @@ -431,7 +417,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase): model.layers[0].kernel = \ model.layers[0].kernel_initializer((model.layer_input_shape[0], model.n_outputs)) - bolton._is_init = True + bolton._is_init = True # pylint: disable=protected-access bolton.layers = model.layers bolton.epsilon = 2 bolton.noise_distribution = 'laplace' @@ -467,7 +453,7 @@ class BoltonOptimizerTest(keras_parameterized.TestCase): model.layers[0].kernel = \ model.layers[0].kernel_initializer((model.layer_input_shape[0], model.n_outputs)) - bolton._is_init = True + bolton._is_init = True # pylint: disable=protected-access bolton.noise_distribution = 'laplace' bolton.epsilon = 1 bolton.layers = model.layers