Add hand-crafted features to Keras sequential model

后端 未结 1 1241
余生分开走
余生分开走 2021-01-13 10:09

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,

1条回答
  •  悲&欢浪女
    2021-01-13 10:29

    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()
    

    0 讨论(0)
提交回复
热议问题