Search code examples
kerasconv-neural-networksequential

How to manage extra input of a different type and shape to a CNN


I'm training a CNN to detect certain pattern on my input data and return 1 if the pattern is found. The inputs are all of shape (200,), the labels are (1,). I have limited data so I need to find ways to assist the training.

When the inputs are originally from an earlier time, the patterns and the peaks tend to be rounder. When they are originally from a later time, they tend to be noisier and spikier.

I think if I divide all my data into 3 bins according to the time they came from (maybe 0 to 2 from rounder to spikier), I can feed that and it could help the CNN to understand the pattern better, but I'm not sure how I can feed that information.

This is my code so far:

model_m5 = Sequential()
model_m5.add(Reshape((200, 1), input_shape=(200,)))
model_m5.add(Conv1D(60, 4, activation='relu', input_shape=(200, 1)))
model_m5.add(Conv1D(60, 4, activation='relu'))
model_m5.add(MaxPooling1D(pool_size=5))
model_m5.add(Conv1D(80, 10, activation='relu'))
model_m5.add(Conv1D(80, 10, activation='relu'))
model_m5.add(GlobalAveragePooling1D())
model_m5.add(Dropout(0.5))
model_m5.add(Dense(1, activation='sigmoid'))
print(model_m5.summary())

Solution

  • The WideDeepModel from TensorFlow does what I asked.

    This makes it easy to include a linear component (linear_model) once you have your deep model (dnn_model); this way, you can train them together in a combined model:

    tf.keras.experimental.WideDeepModel(
        linear_model, dnn_model, activation=None, **kwargs
    )
    

    Documentation and examples can be found here: https://www.tensorflow.org/api_docs/python/tf/keras/experimental/WideDeepModel#example_5