Keras: TypeError: run() got an unexpected keyword argument 'kernel_regularizer'

Viewed 4297

I am using tensorflow==1.2.1 and Keras==2.0.6 to build a model:

input_num = X_norm_keras[:,2:].shape[1]
model_keras = Sequential()
model_keras.add(Dense(10, input_dim=input_num, activation='relu'))
model_keras.add(Dense(1, activation='linear'))
kernel_regularizer=regularizers.l2(0.2), optimizer='adam')
model_keras.compile(loss='mean_squared_error', kernel_regularizer=regularizers.l2(0.2), optimizer='adam')
model_keras.fit(X_norm_train[:,2:], y_norm_train, batch_size=25, epochs=250)

But got the following errors:

Using TensorFlow backend.
total data points =  (25, 106)
Epoch 1/250
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-4cbd897903e7> in <module>()

    102     model_keras.compile(loss='mean_squared_error', kernel_regularizer=regularizers.l2(0.2), optimizer='adam')
--> 103     model_keras.fit(X_norm_train[:,2:], y_norm_train, batch_size=25, epochs=250)

/usr/local/lib/python3.4/dist-packages/keras/models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
    861                               class_weight=class_weight,
    862                               sample_weight=sample_weight,
--> 863                               initial_epoch=initial_epoch)
    864 
    865     def evaluate(self, x, y, batch_size=32, verbose=1,

/usr/local/lib/python3.4/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
   1428                               val_f=val_f, val_ins=val_ins, shuffle=shuffle,
   1429                               callback_metrics=callback_metrics,
-> 1430                               initial_epoch=initial_epoch)
   1431 
   1432     def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):

/usr/local/lib/python3.4/dist-packages/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch)
   1077                 batch_logs['size'] = len(batch_ids)
   1078                 callbacks.on_batch_begin(batch_index, batch_logs)
-> 1079                 outs = f(ins_batch)
   1080                 if not isinstance(outs, list):
   1081                     outs = [outs]

/usr/local/lib/python3.4/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
   2266         updated = session.run(self.outputs + [self.updates_op],
   2267                               feed_dict=feed_dict,
-> 2268                               **self.session_kwargs)
   2269         return updated[:len(self.outputs)]
   2270 

TypeError: run() got an unexpected keyword argument 'kernel_regularizer'

Am I missing anything here? Thanks!

1 Answers

The regularizer kernel_regularizer=regularizers.l2(0.2) should be an argument of Dense(), not model.compile().

From the documentation of model.compile():

**kwargs: When using the Theano/CNTK backends, these arguments are passed into K.function. When using the TensorFlow backend, these arguments are passed into tf.Session.run.

That's why you are seeing an error coming from run().

Related