简体   繁体   English

将多个输入传递给CNN模型

[英]Passing multiple inputs to CNN model

I have vector of integers representing each character in the domain name and another vector of integers representing the timeline information. 我有代表域名中每个字符的整数向量和另一个表示时间轴信息的整数向量。 I need to give both these vectors as input to a CNN model to classify domain names as good or spam. 我需要将这两个向量作为CNN模型的输入,将域名分类为好或垃圾邮件。

For instance, 例如,

Vector representing domain name -> 1 x 75 vector. 矢量代表域名 - > 1 x 75矢量。 Each element in the vector represents each character in the domain name. 向量中的每个元素表示域名中的每个字符。 If there are 1000 domain names, then it will be a matrix of shape 1000 x 75 如果有1000个域名,那么它将是1000 x 75的形状矩阵

Vector representing timeline information -> 1 x 1440 vector. 矢量表示时间线信息 - > 1 x 1440矢量。 Each element representing number of mails sent from a particular domain for each minute. 每个元素表示每分钟从特定域发送的邮件数。 If there are 1000 domain names, then it will be a matrix of shape 1000 x 1440 如果有1000个域名,那么它将是一个形状为1000 x 1440的矩阵

How do I input these two vectors to a single CNN model? 如何将这两个向量输入到单个CNN模型?

My current model is given only the domain name as input, 我当前的模型只给出了域名作为输入,

def build_model(max_features, maxlen):
"""Build CNN model"""
model = Sequential()
model.add(Embedding(max_features, 8, input_length=maxlen))
model.add(Convolution1D(6, 4, border_mode='same'))
model.add(Convolution1D(4, 4, border_mode='same'))
model.add(Convolution1D(2, 4, border_mode='same'))
model.add(Flatten())
#model.add(Dropout(0.2))
#model.add(Dense(2,activation='sigmoid'))
#model.add(Dense(180,activation='sigmoid'))
#model.add(Dropout(0.2))
model.add(Dense(2,activation='softmax'))
sgd = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['categorical_accuracy', 'f1score', 'precision', 'recall'])

Thanks! 谢谢!

In convolutions, you need a "length" dimension and a "channels" dimension. 在卷积中,您需要“长度”维度和“渠道”维度。

(In 2D, they would be "width", "height" and "channels"). (在2D中,它们将是“宽度”,“高度”和“通道”)。

Now, I can't think of any way to relate the 75 characters with the 1440 minutes. 现在,我想不出任何方法可以将75个字符与1440分钟联系起来。 (Maybe you can, and if you can state how, maybe we can work better) (也许你可以,如果你能说明如何,也许我们可以更好地工作)

Here is what I'm assuming: 这是我假设的:

  • A sequence for the domain name, containing length 75. 域名序列,长度为75。
  • The domain name will have 8 channels (since your embedding layer has 8 outputs) 域名将有8个通道(因为您的嵌入层有8个输出)
  • Another sequence for the 1440 minutes. 1440分钟的另一个序列。
  • Only one channel for the e-mails per minute 每分钟只有一个电子邮件渠道

So, we'd have two inputs: 所以,我们有两个输入:

from keras.layers import *

input1 = Input((75,))
input2 = Input((1440,))

Only the domain name should pass through an embedding layer: 只有域名应该通过嵌入层:

name = Embedding(max_features, 8, input_length=maxlen)(input1)

Now, reshaping to fit the convolutional inputs (None,length,channels) . 现在,重新整形以适应卷积输入(None,length,channels)

# the embedding output is already (Batch, 75, 8) -- See: https://keras.io/layers/embeddings/    

mails = Reshape((1440,1))(input2) #adding 1 channel at the end    

Parallel convolutions: 平行卷积:

name = Conv1D( feel free to customize )(name)
name = Conv1D( feel free to customize )(name)

mails = Conv1D( feel free to customize )(mails)
mails = Conv1D( feel free to customize )(mails)

Concatenate - Since they have totally different shapes, maybe we should simply flatten both (or you could think of fancy operations to match them) 连接 - 由于它们具有完全不同的形状,也许我们应该简单地将它们平铺(或者你可以想到花哨的操作来匹配它们)

name = Flatten()(name)
mails = Flatten()(mails)
out = Concatenate()([name,mails])

out = add your extra layers

out = Dense(2,activation='softmax')(out)

And finally we create the model: 最后我们创建了模型:

from keras.models import Model
model = Model([input1,input2], out)

Train it like this: 训练如下:

model.fit([xName,xMails], Y, ....)

You could build a multi-input network using Keras' functional API. 您可以使用Keras的功能API构建多输入网络。 Have a 1D convolution network separately for each input dimensions. 为每个输入维度单独设置一维卷积网络。 Then concatenate the output of each of these networks and pass that concatenated vector into some shared fully-connected layers which sit on top of both of the other networks. 然后连接每个网络的输出,并将该连接的矢量传递到位于两个其他网络之上的一些共享的完全连接的层。

https://keras.io/getting-started/functional-api-guide/#multi-input-and-multi-output-models https://keras.io/getting-started/functional-api-guide/#multi-input-and-multi-output-models

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM