Home > Blockchain >  'l2' not defined as regularizer
'l2' not defined as regularizer

Time:10-04

The following is my code:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import layers
from tensorflow.keras import regularizers

model = Sequential()
model.add(Dense(units=10, input_shape=[784], activation='sigmoid', kernal_regularizer=l2(0.01)))
model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
model.fit(x_train, y_train, batch_size = 256, epochs=100, validation_data=(x_test, y_test))

I keep getting the following error:

NameError                                 Traceback (most recent call last)

<ipython-input-23-76c00e884f10> in <module>()
      7 
      8 model = Sequential()
----> 9 model.add(Dense(units=10, input_shape=[784], activation='sigmoid', kernal_regularizer=l2(0.01)))
     10 model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
     11 model.summary()

NameError: name 'l2' is not defined

What is going on? I'd appreciate any help.

CodePudding user response:

The syntax is

kernal_regularizer=regularizers.l1_l2(l1=0, l2=0.01)

instead of kernal_regularizer=l2(0.01)

Link: https://www.tensorflow.org/api_docs/python/tf/keras/regularizers/l1_l2

  • Related