向Keras顺序模型添加手工制作的功能 [英] Add hand-crafted features to Keras sequential model

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

问题描述

我有一维序列,希望将其用作Keras VGG分类模型的输入,并分为x_trainx_test.对于每个序列,我还具有存储在feats_trainfeats_test中的自定义功能,这些功能我不想输入到卷积层,而是输入到第一个完全连接的层.

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.

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

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模型不是很灵活.您应该查看功能性API .

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

我会尝试这样的事情:

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天全站免登陆