处理Bagging方法时,如何解决TypeError:+不支持的操作数类型:“ int”和“ NoneType”

我需要对LSTM使用Bagging方法,对时间序列数据进行培训。我已经定义了模型库,并使用KerasRegressor链接到scikit-learn。但是具有AttributeError:'KerasRegressor'对象没有属性'loss'。我该如何解决?

更新:我使用了Manoj Mohan的方法(在第一个评论中),并且在拟合步骤中成功了。但是,当我将Manoj Mohan的类修改为

class MyKerasRegressor(KerasRegressor): 
    def fit(self, x, y, **kwargs):
        x = np.expand_dims(x, -2)
        super().fit(x, y, **kwargs)

    def predict(self, x, **kwargs):
        x = np.expand_dims(x, -2)
        super().predict(x, **kwargs)

它解决了与(.fit())相同的predict()的尺寸问题。问题是:

TypeError                                 Traceback (most recent call last)
<ipython-input-84-68d76cb73e8b> in <module>
----> 1 pred_bag = bagging_model.predict(x_test)
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

完整脚本:

def model_base_LSTM():

    model_cii = Sequential()

    # Make layers
    model_cii.add(CuDNNLSTM(50, return_sequences=True,input_shape=((1, 20))))
    model_cii.add(Dropout(0.4))

    model_cii.add(CuDNNLSTM(50, return_sequences=True))
    model_cii.add(Dropout(0.4))

    model_cii.add(CuDNNLSTM(50, return_sequences=True))
    model_cii.add(Dropout(0.4))

    model_cii.add(CuDNNLSTM(50, return_sequences=True))
    model_cii.add(Dropout(0.4))

    model_cii.add(Flatten())
    # Output layer
    model_cii.add(Dense(1))

    # Compile
    model_cii.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics=['accuracy'])

    return model_cii

model = MyKerasRegressor(build_fn = model_base_LSTM, epochs=100, batch_size =70)
bagging_model = BaggingRegressor(base_estimator=model, n_estimators=10)
train_model = bagging_model.fit(x_train, y_train)

bagging_model.predict(x_test)

Output:
TypeError                                 Traceback (most recent call last)
<ipython-input-84-68d76cb73e8b> in <module>
----> 1 pred_bag = bagging_model.predict(x_test)
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
玛诺·莫汉(Manoj Mohan)

model_base_LSTM()方法有错误更换

return model

return model_cii

修复了“检查输入时出错”的问题,可以添加一个额外的尺寸。这也解决了scikit-learn(2维)与Keras LSTM(3维)问题。创建KerasRegressor的子类来处理尺寸不匹配。

class MyKerasRegressor(KerasRegressor):
    def fit(self, x, y, **kwargs):
        x = np.expand_dims(x, -2)
        return super().fit(x, y, **kwargs)

    def predict(self, x, **kwargs):
        x = np.expand_dims(x, -2)
        return super().predict(x, **kwargs)

model = MyKerasRegressor(build_fn = model_base_LSTM, epochs=100, batch_size =70)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

试图解决“TypeError:+ 不支持的操作数类型:“NoneType”和“int”

TypeError:+时不支持的操作数类型:求和列表时为“ int”和“ NoneType”

TypeError:打印时%不支持的操作数类型:'NoneType'和'int'

如何修复,TypeError:% 不支持的操作数类型:'NoneType' 和 'int'

Keras报告TypeError:+不支持的操作数类型:'NoneType'和'int'

TensorFlow 1.x:TypeError:/:不支持的操作数类型:'NoneType'和'int'

TypeError:%不支持的操作数类型:“ NoneType”和“ int”

Python:TypeError:+不支持的操作数类型:“ NoneType”和“ int”

递归阶乘报告错误`TypeError:*不支持的操作数类型:'int'和'NoneType'`

TypeError:+不支持的操作数类型:“ NoneType”和“ int”

TypeError: 不支持的操作数类型 +: 'NoneType' 和 'int' 在 while 循环中

蟒蛇 | TypeError: 不支持的操作数类型 /: 'NoneType' 和 'int' |使用变量来划分

TypeError: 不支持的操作数类型 *: 'float' 和 'NoneType' 在构建 pyomo 模型时

H2O异常TypeError:+不支持的操作数类型:'NoneType'和'unicode'

出现错误“TypeError:% 不支持的操作数类型:‘NoneType’和‘str’”

TypeError:&:不支持的操作数类型:“ NoneType”和“ str”

TypeError:+不支持的操作数类型:“ NoneType”和“ list”

TypeError:+ =不支持的操作数类型:“ datetime.timedelta”和“ NoneType”

TypeError:+ =:Python 3中不支持的操作数类型为“ float”和“ NoneType”

TypeError: 不支持的操作数类型 +: 'NoneType' 和 'str'"?

* 不支持的操作数类型:“NoneType”和“NoneType”

训练GRU模型TypeError时出现此错误:*不支持的操作数类型:'int'和'NoneType'

python函数处理给出错误TypeError:不支持的操作数类型-:'NoneType'和'NoneType'

TypeError:%的不支持的操作数类型:带有随机数生成器的'NoneType'和'tuple'?

类型错误:* 不支持的操作数类型:'int' 和 'NoneType'

+不支持的操作数类型:'NoneType'和'str'和'int'

*:Python 3中不支持*:'NoneType'和'int'的操作数类型

Python错误:+不支持的操作数类型:“ int”和“ NoneType”

+ 不支持的操作数类型:'NoneType' 和 'NoneType' 与 PyXL