I am trying to use Kernel Regularizer which is the normal regularization on weights in machine learning.
Here is the code I have:
def model_param(self):
""" Method to do deep learning
"""
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
from keras import regularizers
self.model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
self.model.add(Dense(32, activation='relu', input_dim=self.x_train_std.shape[1]),\
kernel_regularizer=regularizers.l2(0.01))
self.model.add(Dropout(0.5))
#self.model.add(Dense(60, activation='relu'))
#self.model.add(Dropout(0.5))
self.model.add(Dense(1, activation='sigmoid'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
self.model.compile(loss='binary_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
get an error below which says the keyword argument not recognized.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-225-3cf87672aeed> in <module>()
----> 1 dl_2.model_param()
<ipython-input-223-08450db8ff4d> in model_param(self)
65 # in the first layer, you must specify the expected input data shape:
66 # here, 20-dimensional vectors.
---> 67 self.model.add(Dense(32, activation='relu', input_dim=self.x_train_std.shape[1]), kernel_regularizer=regularizers.l2(0.01))
68 self.model.add(Dropout(0.5))
69 #self.model.add(Dense(60, activation='relu'))
TypeError: add() got an unexpected keyword argument 'kernel_regularizer'
Try this:
self.model.add(Dense(32, activation='relu', input_dim=self.x_train_std.shape[1], kernel_regularizer=regularizers.l2(0.01)))
kernel_regularizer is a param of Dense, not the add function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With