Keras:TypeError:run()得到了一个意外的关键字参数' kernel_regularizer'

时间:2017-09-27 19:00:19

标签: python-3.x tensorflow deep-learning keras

我正在使用tensorflow==1.2.1Keras==2.0.6来构建模型:

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)

但是出现了以下错误:

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'

我在这里遗漏了什么?谢谢!

1 个答案:

答案 0 :(得分:4)

规范制定者kernel_regularizer=regularizers.l2(0.2)应该是Dense()的参数,而不是model.compile()

来自model.compile()的文档:

  

** kwargs:当使用Theano / CNTK后端时,这些参数将传递给K.function。使用TensorFlow后端时,这些参数将传递到tf.Session.run。

这就是为什么你会看到来自run()的错误。