python - 可以训练具有多个输出的 KerasRegressor,得分,但不预测
问题描述
介绍
我正在使用scikit-learn的KerasRegressor包装器和功能 API来创建具有多个输出的模型。我的模型有两个分支:一个预测 1 个值,另一个同时预测 3 个值。使用的 keras 版本是可用的最新版本(win64 上的 2.3.1)
问题
我遇到的问题如下:我的模型可以通过管道训练,我可以得到分数,但我无法预测。
# To fit the pipeline, this line runs successfully
pipeline.fit(X_train, [y_train_branch_1, y_train_branch_2])`
# To get the score, it works as well
pipeline.score(X_test, [y_test_branch_1, y_test_branch_2])
# To make predictions however, it doesn't
pipeline.predict(X_test)
最后一行不起作用:
ValueError:无法将输入数组从形状(11963,3)广播到形状(11963)
它发生在这里:
~\Anaconda3\envs\blades\lib\site-packages\keras\wrappers\scikit_learn.py in predict(self, x, **kwargs)
320 """
321 kwargs = self.filter_sk_params(Sequential.predict, kwargs)
--> 322 preds = np.array(self.model.predict(x, **kwargs))
323 if preds.shape[-1] == 1:
324 return np.squeeze(preds, axis=-1)
输入形状是:
- X_train:(47849, 4)
- y_train_branch_1: (47849, 3)
- y_train_branch_2: (47849, 1)
- X_test: (11963, 4)
- y_test_branch_1: (11963, 3)
- y_test_branch_2: (11963, 1)
似乎预测的结果分为两部分,在使用时没有正确合并.predict
。有没有人遇到过这个问题?直接连接模型中的两个输出可能是一种解决方法,但无论如何我想解决这个问题。
谢谢你的帮助!
代码
进口
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from keras.wrappers.scikit_learn import KerasRegressor
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint
from keras import Input, Model
from keras.layers import Dense, Activation, Concatenate
from keras.optimizers import Adam
模型定义
def build_branch_1(inputs):
x = Dense(12)(inputs)
x = Activation('elu')(x)
x = Dense(12)(x)
x = Activation('elu')(x)
a = Dense(12)(x)
a = Activation('elu')(a)
a = Dense(1)(a)
b = Dense(12)(x)
b = Activation('elu')(b)
b = Dense(1)(b)
c = Dense(12)(x)
c = Activation('elu')(c)
c = Dense(1)(c)
x = Concatenate(name='branch_1')([a, b, c])
return x
def build_branch_2(inputs):
x = Dense(12)(inputs)
x = Activation('elu')(x)
x = Dense(12)(x)
x = Activation('elu')(x)
x = Dense(12)(x)
x = Activation('elu')(x)
x = Dense(1, name='branch_2')(x)
return x
def build_model():
inputs = Input(shape=(4,))
branch_1 = build_branch_1(inputs)
branch_2 = build_branch_2(inputs)
model = Model(inputs=inputs, outputs=[branch_1, branch_2])
model.compile(optimizer=Adam(), loss='mse')
return model
管道定义
pipeline = Pipeline([
('stdscaling', StandardScaler()),
('model', KerasRegressor(
build_fn=build_model,
batch_size=128,
epochs=10,
verbose=2,
validation_split=.2,
callbacks=[
EarlyStopping(monitor='loss', min_delta=.01, patience=250,
restore_best_weights=True),
ModelCheckpoint(filepath='model/model_' + model_name + '.hdf5',
monitor='val_loss',
save_best_only=True,
save_weights_only=False)
]
))
])
解决方案
推荐阅读
- python-3.x - 将 sqlite3 中的表数据与整数主键匹配
- google-apps-script - OnEdit(e) Google Sheets 清除相邻单元格
- java - 在 IntelliJ IDEA 中使用 Gradle 导出时,JAR 文件中不包含 Resources 文件夹
- python - 如何查找数组中大于所有元素的所有元素
- mechanicalturk - 为类别分类添加强制性说明
- google-sheets - 转置在谷歌表格中的arrayformula内不起作用
- javascript - 如何在 React Project 中读取 Github 的文件(特别是 Markdown 文件)?
- swift - 错误:命令 CompileSwiftSources 失败,退出代码为非零
- sql - 如何在表中查找空或 NULL 列值?
- regex - 如何使用正则表达式搜索带有附加数字的子字符串?