python - Classification and regression using the same Neural Network using Keras
问题描述
I would like to build a Neural Network that at the same time output a label for classification and a value for regression. I would like to do that using Keras. Right now my code is only for classification:
mdl = Sequential()
mdl.add(Dense(100, activation='relu', input_dim=X_train.shape[1]))
mdl.add(Dense(200, activation='relu'))
mdl.add(Dense(100, activation='relu'))
mdl.add(Dense(6, activation='softmax'))
mdl.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
# early stopping implementation
filepath="weights.best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
save_best_only=True, mode='max')
early_stop = EarlyStopping(monitor='val_acc', patience=100, mode='max')
callbacks_list = [checkpoint, early_stop]
# fit network
history = mdl.fit(X_train, y_train, epochs=2000, batch_size=32,
validation_split=0.2, verbose=2, shuffle=True, callbacks=callbacks_list)
So right now I have a softmax activation function on the output layer that correspond to the probability that I use for classification. How can I modify this code to output also a continuos value that will represent my regression problem. I know that Keras Functional API allow to specify multi input and multi output network. Anyone that have an idea on how can I do that?
解决方案
The same code in a slightly different pattern
There's a straightforward transformation of your code to the Keras Functional API as illustrated in their documentation. You'd need to change your Sequential declaration
mdl = Sequential()
mdl.add(Dense(100, activation='relu', input_dim=X_train.shape[1]))
mdl.add(Dense(200, activation='relu'))
mdl.add(Dense(100, activation='relu'))
mdl.add(Dense(6, activation='softmax'))
to its Functional equivalent:
inputs = Input(shape=(X_train.shape[1],))
layer1 = Dense(100, activation='relu')(inputs)
layer2 = Dense(200, activation='relu')(layer1)
layer3 = Dense(100, activation='relu')(layer2)
classifier = Dense(6, activation='softmax')(layer3)
mdl = Model(inputs=inputs, outputs=classifier)
(often people just re-use the same variable for all the intermediate layers, it's even done in the documentation samples but this IMHO is a bit clearer).
Once you've done that, you can add another output layer that "branches" from the last Dense layer layer3
, and set that your model has two outputs, for example:
regression = Dense(1, activation='linear')(layer3)
mdl = Model(inputs=inputs, outputs=[classifier, regression])
推荐阅读
- python - 运行 Python 3.7 和 Selenium:运行脚本时如何回复 Firefox 密码管理器弹出窗口?
- javascript - 在 Rails 6 中替代 $.ready() 来切换一个类
- r - 使用 geom_bar 为堆积条形图格式化数据
- node.js - cls-hooked或错误使用中的内存泄漏?
- javascript - 使用打字稿在单击时切换表行
- reactjs - React native 和 MobX:对装饰器的实验性支持是一个主题 [...]。但是“tsconfig”或“jsconfig”不存在
- python - 为什么我在一个函数中正确加载的图像只在另一个函数中加载为 1 x 4 像素?
- c++ - 如何将 mysqlcppconn.lib 链接到 C++ 项目
- amazon-web-services - AWS CDK 将多个 Lambda 订阅到同一个 SNS 队列
- r - 如何使用R在所有行的某个位置插入一个字符