Search code examples
tensorflowtime-seriesconvolution

Tensorflow: How to apply 1d convolutions feature/channel vise?


I want to process multivariate times series with a shape of [points in time, # features]

My goal is to apply 1d convolutions (with its own filters) to each feature stream ([points in time, 1]) separately. I don't want to use the 2D Convolutions since those would apply the same filters over all feature streams.

I know that tf.keras.layers.DepthwiseConv2D and tf.keras.layers.SeparableConv2D exist, but i'm not sure whether these are appropriate to solve the problem and if so how.

Is it possible to perform this operation without splitting up the input in # feature many inputs and applying covolutions on those?


Solution

  • You can make use of the Conv1D function defined in Tensorflow (link) with the groups argument to define individual filters for each feature.

    An example of this:

    import tensorflow as tf
    
    BATCH_SIZE = 128
    N_TIME_POINTS = 300
    N_FEATURES = 25
    N_FILTERS_PER_FEATURE = 4
    
    x = tf.random.normal(input_shape = (BATCH_SIZE, N_FEATURES, N_TIME_POINTS))
    y = tf.keras.layers.Conv1D(
        filters=N_FILTERS_PER_FEATURE*N_FEATURES,
        kernel_size=3,      # How many temporal samples fit into each filter
        activation='relu',
        padding='causal',
        groups=N_FEATURES,  # Important! treat each feature as a separate input
        input_shape=x.shape[1:])(x)
    

    Note the importance on choosing the type of padding (see padding documentation for more info).