Keras意外的内核规则化程序错误

时间:2017-06-28 22:41:50

标签: python tensorflow deep-learning keras

我正在尝试使用内核规则化器,这是机器学习中权重的正常正则化。

这是我的代码:

 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'])

得到一个错误,表示关键字参数无法识别。

---------------------------------------------------------------------------
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'

1 个答案:

答案 0 :(得分:2)

试试这个:

 self.model.add(Dense(32, activation='relu', input_dim=self.x_train_std.shape[1], kernel_regularizer=regularizers.l2(0.01)))

kernel_regularizer是Dense的一个参数,而不是添加函数