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

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

我在这里错过了什么吗?谢谢!

最佳答案

正则化器 kernel_regularizer=regularizers.l2(0.2)应该是 Dense() 的参数,不是 model.compile() .

来自 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.



这就是您看到来自 run() 的错误的原因。 .

关于python-3.x - Keras: TypeError: run() 得到了一个意外的关键字参数 'kernel_regularizer',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46455374/

相关文章:

python - setup.py 不安装要求

python - 使用不修改函数的参数创建装饰器

python - 属性错误: module 'tensorflow' has no attribute 'executing_eagerly'

TensorFlow DataSet API 导致图大小爆炸

tensorflow - 从长远来看, `config.gpu_options.allow_growth=True` 会降低性能吗?

python - 在 `tf.estimator` 中,如何在训练结束时(不是每次迭代时)将变量设置为 `tf.assign` ?

python - random.shuffle() 的倒数?

python - 我用 Python 写了一个 naughs and crosses 游戏,但是赢家没有被正确宣布

tensorflow - keras fit_generator : 'zip' object has no attribute 'shape'

machine-learning - 使用 deeplearning4j 进行字符时间序列的自动编码器