向 Keras 序列模型添加手工制作的特征 [英] Add hand-crafted features to Keras sequential model

查看:39
本文介绍了向 Keras 序列模型添加手工制作的特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个一维序列,我想将其用作 Keras VGG 分类模型的输入,分为 x_trainx_test.对于每个序列,我还在 feats_trainfeats_test 中存储了自定义特征,我不想将它们输入到卷积层,而是输入到第一个全连接层.

因此,一个完整的训练或测试样本将由一个一维序列加上 n 个浮点特征组成.

首先将自定义特征提供给全连接层的最佳方法是什么?我想过将输入序列和自定义特征连接起来,但我不知道如何在模型内部将它们分开.还有其他选择吗?

没有自定义功能的代码:

x_train, x_test, y_train, y_test, feats_train, feats_test = load_balanced_datasets()模型 = 顺序()model.add(Conv1D(10, 5, activation='relu', input_shape=(timesteps, 1)))model.add(Conv1D(10, 5, activation='relu'))模型.add(MaxPooling1D(pool_size=2))模型.添加(辍学(0.5,种子= 789))model.add(Conv1D(5, 6, activation='relu'))model.add(Conv1D(5, 6, activation='relu'))模型.add(MaxPooling1D(pool_size=2))模型.添加(辍学(0.5,种子= 789))模型.添加(展平())模型.添加(密集(512,激活='relu'))模型.添加(辍学(0.5,种子= 789))model.add(Dense(2, activation='softmax'))model.compile(loss='logcosh', optimizer='adam', metrics=['accuracy'])模型.fit(x_train,y_train,batch_size=batch_size,epochs=20,shuffle=False,verbose=1)y_pred = model.predict(x_test)

解决方案

Sequential 模型不是很灵活.您应该查看函数式 API.

我会尝试这样的事情:

from keras.layers import (Conv1D, MaxPool1D, Dropout, Flatten, Dense,输入,连接)从 keras.models 导入模型,顺序时间步长 = 50n = 5定义网络():序列=输入(形状=(时间步长,1),名称=序列")特征=输入(形状=(n,),名称=特征")conv = 顺序()conv.add(Conv1D(10, 5, activation='relu', input_shape=(timesteps, 1)))conv.add(Conv1D(10, 5, activation='relu'))conv.add(MaxPool1D(2))conv.add(辍学(0.5,种子= 789))conv.add(Conv1D(5, 6, activation='relu'))conv.add(Conv1D(5, 6, activation='relu'))conv.add(MaxPool1D(2))conv.add(辍学(0.5,种子= 789))conv.add(展平())第 1 部分 = 转换(序列)合并 = 连接([第 1 部分,功能])final = Dense(512, activation='relu')(合并)最终 = 辍学(0.5,种子 = 789)(最终)final = Dense(2, activation='softmax')(final)模型 = 模型(输入=[序列,特征],输出=[最终])model.compile(loss='logcosh', optimizer='adam', metrics=['accuracy'])回报模式m = 网络()

I have 1D sequences which I want to use as input to a Keras VGG classification model, split in x_train and x_test. For each sequence, I also have custom features stored in feats_train and feats_test which I do not want to input to the convolutional layers, but to the first fully connected layer.

A complete sample of train or test would thus consist of a 1D sequence plus n floating point features.

What is the best way to feed the custom features first to the fully connected layer? I thought about concatenating the input sequence and the custom features, but I do not know how to make them separate inside the model. Are there any other options?

The code without the custom features:

x_train, x_test, y_train, y_test, feats_train, feats_test = load_balanced_datasets()

model = Sequential()
model.add(Conv1D(10, 5, activation='relu', input_shape=(timesteps, 1)))
model.add(Conv1D(10, 5, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.5, seed=789))

model.add(Conv1D(5, 6, activation='relu'))
model.add(Conv1D(5, 6, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.5, seed=789))

model.add(Flatten())

model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5, seed=789))
model.add(Dense(2, activation='softmax'))

model.compile(loss='logcosh', optimizer='adam', metrics=['accuracy'])

model.fit(x_train, y_train, batch_size=batch_size, epochs=20, shuffle=False, verbose=1)

y_pred = model.predict(x_test)

解决方案

Sequential model is not very flexible. You should look into the functional API.

I would try something like this:

from keras.layers import (Conv1D, MaxPool1D, Dropout, Flatten, Dense,
                          Input, concatenate)
from keras.models import Model, Sequential

timesteps = 50
n = 5

def network():
    sequence = Input(shape=(timesteps, 1), name='Sequence')
    features = Input(shape=(n,), name='Features')

    conv = Sequential()
    conv.add(Conv1D(10, 5, activation='relu', input_shape=(timesteps, 1)))
    conv.add(Conv1D(10, 5, activation='relu'))
    conv.add(MaxPool1D(2))
    conv.add(Dropout(0.5, seed=789))

    conv.add(Conv1D(5, 6, activation='relu'))
    conv.add(Conv1D(5, 6, activation='relu'))
    conv.add(MaxPool1D(2))
    conv.add(Dropout(0.5, seed=789))
    conv.add(Flatten())
    part1 = conv(sequence)

    merged = concatenate([part1, features])

    final = Dense(512, activation='relu')(merged)
    final = Dropout(0.5, seed=789)(final)
    final = Dense(2, activation='softmax')(final)

    model = Model(inputs=[sequence, features], outputs=[final])

    model.compile(loss='logcosh', optimizer='adam', metrics=['accuracy'])

    return model

m = network()

这篇关于向 Keras 序列模型添加手工制作的特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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