在 Keras 中合并 2 个序列模型 [英] Merge 2 sequential models in Keras

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

问题描述

我试图在 keras 中合并 2 个顺序模型.代码如下:

I a trying to merge 2 sequential models in keras. Here is the code:

model1 = Sequential(layers=[
    # input layers and convolutional layers
    Conv1D(128, kernel_size=12, strides=4, padding='valid', activation='relu', input_shape=input_shape),
    MaxPooling1D(pool_size=6),
    Conv1D(256, kernel_size=12, strides=4, padding='valid', activation='relu'),
    MaxPooling1D(pool_size=6),
    Dropout(.5),

])

model2 = Sequential(layers=[
    # input layers and convolutional layers
    Conv1D(128, kernel_size=20, strides=5, padding='valid', activation='relu', input_shape=input_shape),
    MaxPooling1D(pool_size=5),
    Conv1D(256, kernel_size=20, strides=5, padding='valid', activation='relu'),
    MaxPooling1D(pool_size=5),
    Dropout(.5),

])

model = merge([model1, model2], mode = 'sum')
Flatten(),
Dense(256, activation='relu'),
Dropout(.5),
Dense(128, activation='relu'),
Dropout(.35),
# output layer
Dense(5, activation='softmax')
return model

错误日志如下:

文件/nics/d/home/dsawant/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py",第 392 行,在 is_keras_tensor 中raise ValueError('Unexpectedly found an instance of type ' + str(type(x)) + '. ' ValueError: Unexpectedly found an instance of输入 .期望符号张量实例.

File "/nics/d/home/dsawant/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 392, in is_keras_tensor raise ValueError('Unexpectedly found an instance of type ' + str(type(x)) + '. ' ValueError: Unexpectedly found an instance of type <class 'keras.models.Sequential'>. Expected a symbolic tensor instance.

更多日志:

ValueError: Layer merge_1 被调用的输入不是符号张量.接收类型:类keras.models.Sequential".完整输入:[keras.models.Sequential 对象在 0x2b32d518a780,keras.models.Sequential 对象在 0x2b32d521ee80].所有输入到层应该是张量.

ValueError: Layer merge_1 was called with an input that isn't a symbolic tensor. Received type: class 'keras.models.Sequential'. Full input: [keras.models.Sequential object at 0x2b32d518a780, keras.models.Sequential object at 0x2b32d521ee80]. All inputs to the layer should be tensors.

如何合并这两个使用不同窗口大小的 Sequential 模型并对它们应用max"、sum"等函数?

How can I merge these 2 Sequential models that use different window sizes and apply functions like 'max', 'sum' etc to them?

推荐答案

使用功能 API 为您带来所有可能性.

Using the functional API brings you all possibilities.

使用函数式 API 时,您需要跟踪输入和输出,而不仅仅是定义层.

When using the functional API, you need to keep track of inputs and outputs, instead of just defining layers.

你定义一个层,然后你用一个输入张量调用这个层来得到输出张量.模型和层的调用方式完全相同.

You define a layer, then you call the layer with an input tensor to get the output tensor. Models and layers can be called exactly the same way.

对于合并层,我更喜欢使用其他更直观的合并层,例如Add()Multiply()Concatenate() 例如.

For the merge layer, I prefer using other merge layers that are more intuitive, such as Add(), Multiply() and Concatenate() for instance.

from keras.layers import *

mergedOut = Add()([model1.output,model2.output])
    #Add() -> creates a merge layer that sums the inputs
    #The second parentheses "calls" the layer with the output tensors of the two models
    #it will demand that both model1 and model2 have the same output shape

同样的想法适用于以下所有层.我们不断更新输出张量,将其提供给每一层并获得新的输出(如果我们对创建分支感兴趣,我们将对每个感兴趣的输出使用不同的 var 来跟踪它们):

This same idea apply to all the following layers. We keep updating the output tensor giving it to each layer and getting a new output (if we were interested in creating branches, we would use a different var for each output of interest to keep track of them):

mergedOut = Flatten()(mergedOut)    
mergedOut = Dense(256, activation='relu')(mergedOut)
mergedOut = Dropout(.5)(mergedOut)
mergedOut = Dense(128, activation='relu')(mergedOut)
mergedOut = Dropout(.35)(mergedOut)

# output layer
mergedOut = Dense(5, activation='softmax')(mergedOut)

现在我们创建了路径",是时候创建Model.创建模型就像告诉它从哪个输入张量开始,在哪里结束:

Now that we created the "path", it's time to create the Model. Creating the model is just like telling at which input tensors it starts and where it ends:

from keras.models import Model

newModel = Model([model1.input,model2.input], mergedOut)
    #use lists if you want more than one input or output    

请注意,由于此模型有两个输入,因此您必须使用列表中的两个不同的 X_training 变量来训练它:

Notice that since this model has two inputs, you have to train it with two different X_training vars in a list:

newModel.fit([X_train_1, X_train_2], Y_train, ....)    

<小时>

现在,假设您只需要一个输入,并且模型 1 和模型 2 将采用相同的输入.


Now, suppose you wanted only one input, and both model1 and model2 would take the same input.

函数式 API 通过创建输入张量并将其提供给模型(我们将模型称为层)来轻松实现这一点:

The functional API allows that quite easily by creating an input tensor and feeding it to the models (we call the models as if they were layers):

commonInput = Input(input_shape)

out1 = model1(commonInput)    
out2 = model2(commonInput)    

mergedOut = Add()([out1,out2])

在这种情况下,模型会考虑这个输入:

In this case, the Model would consider this input:

oneInputModel = Model(commonInput,mergedOut)

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

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