Skip to content

Commit

Permalink
Merge pull request #83 from deel-ai/fix_pylint_warnings
Browse files Browse the repository at this point in the history
Fix Pylint warnings
  • Loading branch information
thib-s authored Nov 13, 2023
2 parents 8f4635b + fa537c8 commit de7c524
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 64 deletions.
26 changes: 25 additions & 1 deletion deel/lip/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
# -*- coding: utf-8 -*-
# Copyright IRT Antoine de Saint Exupéry et Université Paul Sabatier Toulouse III - All
# rights reserved. DEEL is a research program operated by IVADO, IRT Saint Exupéry,
# CRIAQ and ANITI - https://www.deel.ai/
# =====================================================================================
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
DEEL-LIP
--------
DEEL-LIP provides a simple interface to build and train Lipschitz-constrained neural
networks based on TensorFlow/Keras framework.
"""
from os import path

with open(path.join(path.dirname(__file__), "VERSION")) as f:
Expand Down
10 changes: 6 additions & 4 deletions deel/lip/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _monitor(self, step):
).numpy()
sig = sigmas[0]
else:
RuntimeWarning(
raise RuntimeWarning(
f"[MonitorCallback] layer {layer_name} has no "
f"attribute {self.target}"
)
Expand All @@ -137,11 +137,13 @@ def _monitor(self, step):
sigmas,
step=step,
buckets=None,
description="distribution of singular values for layer %s"
% layer_name,
description=(
f"distribution of singular values for layer "
f"{layer_name}"
),
)
if not result:
RuntimeWarning(
raise RuntimeWarning(
"[MonitorCallback] unable to find filewriter, no logs were written,"
)

Expand Down
2 changes: 1 addition & 1 deletion deel/lip/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def __init__(
super(SpectralConstraint, self).__init__()

def __call__(self, w):
wbar, u, sigma = reshaped_kernel_orthogonalization(
wbar, _, _ = reshaped_kernel_orthogonalization(
w,
self.u,
self.k_coef_lip,
Expand Down
7 changes: 6 additions & 1 deletion deel/lip/initializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# rights reserved. DEEL is a research program operated by IVADO, IRT Saint Exupéry,
# CRIAQ and ANITI - https://www.deel.ai/
# =====================================================================================
"""
This module contains extra Keras initializers, e.g. SpectralInitializer for 1-Lipschitz
matrix initialization.
They can be used as kernel initializers in any Keras layer.
"""
from tensorflow.keras.initializers import Initializer
from tensorflow.keras import initializers
from .normalizers import (
Expand Down Expand Up @@ -44,7 +49,7 @@ def __init__(

def __call__(self, shape, dtype=None, partition_info=None):
w = self.base_initializer(shape=shape, dtype=dtype)
wbar, u, sigma = reshaped_kernel_orthogonalization(
wbar, _, _ = reshaped_kernel_orthogonalization(
w,
None,
self.k_coef_lip,
Expand Down
27 changes: 27 additions & 0 deletions deel/lip/layers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
# -*- coding: utf-8 -*-
# Copyright IRT Antoine de Saint Exupéry et Université Paul Sabatier Toulouse III - All
# rights reserved. DEEL is a research program operated by IVADO, IRT Saint Exupéry,
# CRIAQ and ANITI - https://www.deel.ai/
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
The submodule `deel.lip.layers` contains all custom Keras layers to build
Lipschitz-constrained neural networks. They all inherit from `keras.layers.Layer` from
Keras API.
"""
from . import unconstrained
from .activations import FullSort, GroupSort, GroupSort2, Householder, MaxMin, PReLUlip
from .base_layer import Condensable, LipschitzLayer
Expand Down
17 changes: 6 additions & 11 deletions deel/lip/layers/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

@register_keras_serializable("deel-lip", "MaxMin")
class MaxMin(Layer, LipschitzLayer):
def __init__(self, data_format="channels_last", k_coef_lip=1.0, *args, **kwargs):
def __init__(self, data_format="channels_last", k_coef_lip=1.0, **kwargs):
"""
MaxMin activation [Relu(x),reLU(-x)]
Args:
data_format (str): either channels_first or channels_last
k_coef_lip (float): the lipschitz coefficient to be enforced
*args: params passed to Layers
**kwargs: params passed to layers (named fashion)
Input shape:
Expand All @@ -43,7 +42,7 @@ def __init__(self, data_format="channels_last", k_coef_lip=1.0, *args, **kwargs)
"""
self.set_klip_factor(k_coef_lip)
super(MaxMin, self).__init__(*args, **kwargs)
super(MaxMin, self).__init__(**kwargs)
if data_format == "channels_last":
self.channel_axis = -1
elif data_format == "channels_first":
Expand All @@ -59,7 +58,7 @@ def build(self, input_shape):
def _compute_lip_coef(self, input_shape=None):
return 1.0

def call(self, x, **kwargs):
def call(self, x):
return (
K.concatenate(
(K.relu(x, alpha=0), K.relu(-x, alpha=0)), axis=self.channel_axis
Expand All @@ -83,9 +82,7 @@ def compute_output_shape(self, input_shape):

@register_keras_serializable("deel-lip", "GroupSort")
class GroupSort(Layer, LipschitzLayer):
def __init__(
self, n=None, data_format="channels_last", k_coef_lip=1.0, *args, **kwargs
):
def __init__(self, n=None, data_format="channels_last", k_coef_lip=1.0, **kwargs):
"""
GroupSort activation
Expand All @@ -94,7 +91,6 @@ def __init__(
size (fullSort behavior)
data_format (str): either channels_first or channels_last
k_coef_lip (float): the lipschitz coefficient to be enforced
*args: params passed to Layers
**kwargs: params passed to layers (named fashion)
Input shape:
Expand All @@ -107,14 +103,13 @@ def __init__(
"""
self.set_klip_factor(k_coef_lip)
super(GroupSort, self).__init__(*args, **kwargs)
super(GroupSort, self).__init__(**kwargs)
if data_format == "channels_last":
self.channel_axis = -1
elif data_format == "channels_first":
raise RuntimeError(
"channels_first not implemented for GroupSort activation"
)
self.channel_axis = 1
else:
raise RuntimeError("data format not understood")
self.n = n
Expand All @@ -138,7 +133,7 @@ def _compute_lip_coef(self, input_shape=None):
return 1.0

@tf.function
def call(self, x, **kwargs):
def call(self, x):
fv = tf.reshape(x, self.flat_shape)
if self.n == 2:
b, c = tf.split(fv, 2, -1)
Expand Down
32 changes: 12 additions & 20 deletions deel/lip/layers/convolutional.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _compute_conv_lip_factor(kernel_size, strides, input_shape, data_format):
elif data_format == "channels_first":
h, w = input_shape[-2], input_shape[-1]
else:
raise RuntimeError("data_format not understood: " % data_format)
raise RuntimeError(f"data_format not understood: {data_format}")

if stride == 1:
return np.sqrt(
Expand Down Expand Up @@ -99,7 +99,7 @@ def __init__(
beta_bjorck=DEFAULT_BETA_BJORCK,
maxiter_spectral=DEFAULT_MAXITER_SPECTRAL,
maxiter_bjorck=DEFAULT_MAXITER_BJORCK,
**kwargs
**kwargs,
):
"""
This class is a Conv2D Layer constrained such that all singular of it's kernel
Expand Down Expand Up @@ -164,11 +164,7 @@ def __init__(
This documentation reuse the body of the original keras.layers.Conv2D doc.
"""
if not (
(dilation_rate == (1, 1))
or (dilation_rate == [1, 1])
or (dilation_rate == 1)
):
if dilation_rate not in ((1, 1), [1, 1], 1):
raise RuntimeError("SpectralConv2D does not support dilation rate")
if padding != "same":
raise RuntimeError("SpectralConv2D only supports padding='same'")
Expand All @@ -188,7 +184,7 @@ def __init__(
activity_regularizer=activity_regularizer,
kernel_constraint=kernel_constraint,
bias_constraint=bias_constraint,
**kwargs
**kwargs,
)
self._kwargs = kwargs
self.set_klip_factor(k_coef_lip)
Expand Down Expand Up @@ -299,7 +295,7 @@ def vanilla_export(self):
use_bias=self.use_bias,
kernel_initializer="glorot_uniform",
bias_initializer="zeros",
**self._kwargs
**self._kwargs,
)
layer.build(self.input_shape)
layer.kernel.assign(self.wbar)
Expand Down Expand Up @@ -334,7 +330,7 @@ def __init__(
beta_bjorck=DEFAULT_BETA_BJORCK,
maxiter_spectral=DEFAULT_MAXITER_SPECTRAL,
maxiter_bjorck=DEFAULT_MAXITER_BJORCK,
**kwargs
**kwargs,
):
"""
This class is a Conv2DTranspose layer constrained such that all singular values
Expand Down Expand Up @@ -419,7 +415,7 @@ def __init__(
activity_regularizer,
kernel_constraint,
bias_constraint,
**kwargs
**kwargs,
)

if self.dilation_rate != (1, 1):
Expand Down Expand Up @@ -598,7 +594,7 @@ def vanilla_export(self):
data_format=self.data_format,
activation=self.activation,
use_bias=self.use_bias,
**self._kwargs
**self._kwargs,
)
layer.build(self.input_shape)
layer.kernel.assign(self.wbar)
Expand Down Expand Up @@ -631,15 +627,11 @@ def __init__(
kernel_constraint=None,
bias_constraint=None,
k_coef_lip=1.0,
**kwargs
**kwargs,
):
if not ((strides == (1, 1)) or (strides == [1, 1]) or (strides == 1)):
if strides not in ((1, 1), [1, 1], 1):
raise RuntimeError("FrobeniusConv2D does not support strides")
if not (
(dilation_rate == (1, 1))
or (dilation_rate == [1, 1])
or (dilation_rate == 1)
):
if dilation_rate not in ((1, 1), [1, 1], 1):
raise RuntimeError("FrobeniusConv2D does not support dilation rate")
if padding != "same":
raise RuntimeError("FrobeniusConv2D only supports padding='same'")
Expand Down Expand Up @@ -667,7 +659,7 @@ def __init__(
activity_regularizer=activity_regularizer,
kernel_constraint=kernel_constraint,
bias_constraint=bias_constraint,
**kwargs
**kwargs,
)
self.set_klip_factor(k_coef_lip)
self.wbar = None
Expand Down
Loading

0 comments on commit de7c524

Please sign in to comment.