在Keras中合并顺序模型 [英] Merge Sequential models in Keras

查看:85
本文介绍了在Keras中合并顺序模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中合并这两个模型,我在这个领域还很陌生,所以请不要轻易判断我.这是代码.

I want to concatenate these two models in my project, I am quite new in this field so please don't judge me hard. So here is the code.

model2 = Sequential()
model2.add(Dense(10, input_dim=df2_x.shape[1], activation='relu'))
model2.add(Dense(50, input_dim=df2_x.shape[1], activation='relu'))
model2.add(Dense(10, input_dim=df2_x.shape[1], activation='relu'))
model2.add(Dense(1, kernel_initializer='normal'))
model2.add(Dense(df2_y.shape[1],activation='softmax'))
model2.compile(loss='categorical_crossentropy', optimizer='adam')
monitor2 = EarlyStopping(monitor='val_loss', min_delta=1e-3, 
                        patience=5, verbose=1, mode='auto',
                           restore_best_weights=True)
model2.fit(df2_x_train,df2_y_train,validation_data=(df2_x_test, df2_y_test),
          callbacks=[monitor2],verbose=2,epochs=1000)


model = Sequential()
model.add(Dense(10, input_dim=df_x.shape[1], activation='relu'))
model.add(Dense(50, input_dim=df_x.shape[1], activation='relu'))
model.add(Dense(10, input_dim=df_x.shape[1], activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
model.add(Dense(df_y.shape[1],activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, 
                        patience=5, verbose=1, mode='auto',
                           restore_best_weights=True)
model.fit(df_x_train,df_y_train,validation_data=(df_x_test, df_y_test),
          callbacks=[monitor],verbose=2,epochs=1000)

在获得模型之后,我要做出预测.

And after the model obtained, I want to make predictions.

所以我有两个数据集,一个用于DOS端口映射攻击,另一个用于DOS-UDP攻击.

So I have two datasets, one for DOS-portmap attacks, and one for DOS-UDP attacks.

如果我要预测一些东西,如何区分这两个?

If I want to predict something, how can I distinguish between these two?

推荐答案

您可以为此使用Keras功能API.模型基本上就像一个图层一样,可以调用,并且可以包含在另一个模型中.

You can use Keras functional API for this. Model is basically same like a layer, it's callable, and can be included in another model.

例如,这是来自2个模型的连接结果的大大简化的版本:

For example, this is greatly simplified version of concatenating results from 2 Models:

inputs = keras.Input(input_shape)
y_1 = model1(inputs)
y_2 = model2(inputs)
outputs = tf.concat([y_1, y_2], axis=0)

new_model = keras.Model(inputs, outputs)

当然,您要确保输出是您想要的结果.因此,关键是在最后一层中,您要对从y_1和y_2获得的值执行哪种操作.

Of course, you want to make sure that the outputs is your desired result. Therefore, the key is in the last layer, what kind of operations you want to do with the values you get from y_1 and y_2.

为此使用一些自定义层,您也可以使用它.您可以在Tensorflow文档中查找有关功能API和自定义层的更多文章.

Some use custom layer for this, you can use that as well. You can look for more article about Functional API and Custom Layer in Tensorflow doc.

这篇关于在Keras中合并顺序模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆