2
votes

I see that people use regulizers in Dense layer but there is a kernel_regulizer argument in Conv2d in keras documentation:

https://keras.io/layers/convolutional/

When I add the regulizers as follows:

conv1 = Conv2D(32, (3, 15), strides=(1, 2), padding='same', data_format='channels_first', kernel_regularizer=regularizers.l2(), input_shape=x_train_n.shape[1:])(g0)

I get this error:

NameError: name 'regularizers' is not defined

I already imported:

import tensorflow as tf
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Input, Activation, Conv2D, MaxPooling2D, BatchNormalization, UpSampling2D, Lambda, \
Conv2DTranspose, Permute, GaussianNoise, advanced_activations, Add, LeakyReLU, Dropout, ActivityRegularization
from tensorflow.python.keras import regularizers

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.figure as fgr
from tensorflow.python.keras import backend
from tensorflow.python.keras.utils import plot_model, normalize
from tensorflow.python.keras.callbacks import EarlyStopping

How can I call regulizers in Conv2D? are there any conflicts in imports?

1
Are you sure that you ran the code correctly? That error seems to imply that the line from tensorflow.python.keras import regularizers was not run before you attempted to make the conv layer. - The Guy with The Hat
No the error is at the same line of conv2d. Before adding from tensorflow.python.keras import regularizers python did not recognize regularizers.l2() nor 'l2', etc. this was the only way I could pass the argument to conv2D without in line errors from Pycharm IDE. - Farnaz
Please make a full example that reproduces the error. - Dr. Snoopy

1 Answers

1
votes

I was able to run your code without any errors. The only time we received the error was in the scenario @The Guy with The Hat has mentioned, that the line from tensorflow.python.keras import regularizers was not run before you attempted to make the Conv2D layer.

In the below code, commented the from tensorflow.python.keras import regularizers, then we got the error you mentioned.

import tensorflow as tf
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Input, Activation, Conv2D, MaxPooling2D, BatchNormalization, UpSampling2D, Lambda, \
Conv2DTranspose, Permute, GaussianNoise, advanced_activations, Add, LeakyReLU, Dropout, ActivityRegularization
# from tensorflow.python.keras import regularizers

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.figure as fgr
from tensorflow.python.keras import backend
from tensorflow.python.keras.callbacks import EarlyStopping

conv1 = Conv2D(32, (3, 15), strides=(1, 2), padding='same', data_format='channels_first', kernel_regularizer=regularizers.l2(), input_shape=(32,32,3))

Output -

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-2ab79df3a82b> in <module>()
     10 from tensorflow.python.keras.callbacks import EarlyStopping
     11 
---> 12 conv1 = Conv2D(32, (3, 15), strides=(1, 2), padding='same', data_format='channels_first', kernel_regularizer=regularizers.l2(), input_shape=(32,32,3))

NameError: name 'regularizers' is not defined

Hope this answers your question. Happy Learning.