简体   繁体   English

'l2' 未定义为正则化器

[英]'l2' not defined as regularizer

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.我将不胜感激任何帮助。

The syntax is语法是

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

instead of kernal_regularizer=l2(0.01)而不是kernal_regularizer=l2(0.01)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM