1414It returns a dictionary indicating for each layer name a tuple (min sv, max sv).
1515"""
1616
17+ import keras
1718import numpy as np
18- import tensorflow as tf
1919
2020from .layers import Condensable , GroupSort , MaxMin
2121from .layers .unconstrained import PadConv2D
@@ -27,7 +27,7 @@ def _compute_sv_dense(layer, input_sizes=None):
2727 The singular values are computed using the SVD decomposition of the weight matrix.
2828
2929 Args:
30- layer (tf. keras.Layer): the Dense layer.
30+ layer (keras.Layer): the Dense layer.
3131 input_sizes (tuple, optional): unused here.
3232
3333 Returns:
@@ -46,16 +46,14 @@ def _generate_conv_matrix(layer, input_sizes):
4646 dirac input.
4747
4848 Args:
49- layer (tf. keras.Layer): the convolutional layer to convert to dense.
49+ layer (keras.Layer): the convolutional layer to convert to dense.
5050 input_sizes (tuple): the input shape of the layer (with batch dimension as first
5151 element).
5252
5353 Returns:
5454 np.array: the equivalent matrix of the convolutional layer.
5555 """
56- single_layer_model = tf .keras .models .Sequential (
57- [tf .keras .layers .Input (input_sizes [1 :]), layer ]
58- )
56+ single_layer_model = keras .Sequential ([keras .Input (input_sizes [1 :]), layer ])
5957 dirac_inp = np .zeros ((input_sizes [2 ],) + input_sizes [1 :]) # Line by line generation
6058 in_size = input_sizes [1 ] * input_sizes [2 ]
6159 channel_in = input_sizes [- 1 ]
@@ -69,8 +67,8 @@ def _generate_conv_matrix(layer, input_sizes):
6967 w_eqmatrix = np .zeros (
7068 (in_size * channel_in , np .prod (out_pred .shape [1 :]))
7169 )
72- w_eqmatrix [start_index : (start_index + input_sizes [2 ]), :] = tf . reshape (
73- out_pred , (input_sizes [2 ], - 1 )
70+ w_eqmatrix [start_index : (start_index + input_sizes [2 ]), :] = (
71+ keras . ops . reshape ( out_pred , (input_sizes [2 ], - 1 ) )
7472 )
7573 dirac_inp = 0.0 * dirac_inp
7674 start_index += input_sizes [2 ]
@@ -86,7 +84,7 @@ def _compute_sv_conv2d_layer(layer, input_sizes):
8684 the weight matrix.
8785
8886 Args:
89- layer (tf. keras.Layer): the convolutional layer.
87+ layer (keras.Layer): the convolutional layer.
9088 input_sizes (tuple): the input shape of the layer (with batch dimension as first
9189 element).
9290
@@ -103,14 +101,14 @@ def _compute_sv_activation(layer, input_sizes=None):
103101
104102 Warning: This is not singular values for non-linear functions but gradient norm.
105103 """
106- if isinstance (layer , tf . keras .layers .Activation ):
107- function2SV = {tf . keras .activations .relu : (0 , 1 )}
104+ if isinstance (layer , keras .layers .Activation ):
105+ function2SV = {keras .activations .relu : (0 , 1 )}
108106 if layer .activation in function2SV .keys ():
109107 return function2SV [layer .activation ]
110108 else :
111109 return (None , None )
112110 layer2SV = {
113- tf . keras .layers .ReLU : (0 , 1 ),
111+ keras .layers .ReLU : (0 , 1 ),
114112 GroupSort : (1 , 1 ),
115113 MaxMin : (1 , 1 ),
116114 }
@@ -145,25 +143,25 @@ def compute_layer_sv(layer, supplementary_type2sv={}):
145143 ReLU, Activation, and deel-lip layers)
146144
147145 Args:
148- layer (tf. keras.layers.Layer): a single tf. keras.layer
146+ layer (keras.layers.Layer): a single keras.layer
149147 supplementary_type2sv (dict, optional): a dictionary linking new layer type with
150148 user-defined function to compute the singular values. Defaults to {}.
151149 Returns:
152150 tuple: a 2-tuple with lowest and largest singular values.
153151 """
154152 default_type2sv = {
155- tf . keras .layers .Conv2D : _compute_sv_conv2d_layer ,
156- tf . keras .layers .Conv2DTranspose : _compute_sv_conv2d_layer ,
153+ keras .layers .Conv2D : _compute_sv_conv2d_layer ,
154+ keras .layers .Conv2DTranspose : _compute_sv_conv2d_layer ,
157155 PadConv2D : _compute_sv_conv2d_layer ,
158- tf . keras .layers .Dense : _compute_sv_dense ,
159- tf . keras .layers .ReLU : _compute_sv_activation ,
160- tf . keras .layers .Activation : _compute_sv_activation ,
156+ keras .layers .Dense : _compute_sv_dense ,
157+ keras .layers .ReLU : _compute_sv_activation ,
158+ keras .layers .Activation : _compute_sv_activation ,
161159 GroupSort : _compute_sv_activation ,
162160 MaxMin : _compute_sv_activation ,
163- tf . keras .layers .Add : _compute_sv_add ,
164- tf . keras .layers .BatchNormalization : _compute_sv_bn ,
161+ keras .layers .Add : _compute_sv_add ,
162+ keras .layers .BatchNormalization : _compute_sv_bn ,
165163 }
166- input_shape = layer .input_shape
164+ input_shape = layer .input . shape if hasattr ( layer . input , "shape" ) else None
167165 if isinstance (layer , Condensable ):
168166 layer .condense ()
169167 layer = layer .vanilla_export ()
@@ -179,7 +177,7 @@ def compute_model_sv(model, supplementary_type2sv={}):
179177 """Compute the largest and lowest singular values of all layers in a model.
180178
181179 Args:
182- model (tf. keras.Model): a tf. keras Model or Sequential.
180+ model (keras.Model): a keras Model or Sequential.
183181 supplementary_type2sv (dict, optional): a dictionary linking new layer type
184182 with user defined function to compute the min and max singular values.
185183
@@ -188,7 +186,7 @@ def compute_model_sv(model, supplementary_type2sv={}):
188186 """
189187 list_sv = []
190188 for layer in model .layers :
191- if isinstance (layer , tf . keras .Model ):
189+ if isinstance (layer , keras .Model ):
192190 list_sv .append ((layer .name , (None , None )))
193191 list_sv += compute_model_sv (layer , supplementary_type2sv )
194192 else :
0 commit comments