Tensorflow:如何应用一维卷积 feature/channel 虎钳?

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

我想处理形状为[时间点,#个特征]的多元时间序列

我的目标是分别对每个特征流([时间点,1])应用一维卷积(带有自己的过滤器)。我不想使用 2D 卷积,因为它们会对所有特征流应用相同的过滤器。

我知道 tf.keras.layers.DepthwiseConv2D and tf.keras.layers.SeparableConv2D 存在,但我不确定这些是否适合解决问题,如果适合如何解决。

是否可以在不拆分 #feature many inputs 中的输入并对这些输入应用卷积的情况下执行此操作?

我用的是手动分流的方法:

input_shape = (self.hyper.time_series_length, self.hyper.time_series_depth, 1)
full_input = tf.keras.Input(shape=input_shape)
outputs = []

# Split the input tensors along the feature dimension, so 1D convolutions can be applied feature wise
feature_splits = tf.unstack(full_input, num=self.hyper.time_series_depth, axis=2)

for feature_input in feature_splits:
    x = feature_input

    for num_filters, kernel_size, strides in zip(self.hyper.conv_filters,
                                                 self.hyper.conv_kernel_size,
                                                 self.hyper.conv_strides):
        # Because these can also be used for the 2d variant the values are stored in lists
        # so the first element needs to be retrieved
        kernel_size = kernel_size[0]
        strides = strides[0]

        x = tf.keras.layers.Conv1D(filters=num_filters, kernel_size=kernel_size, strides=strides,
                                   padding='SAME', activation=tf.keras.activations.relu)(x)

    outputs.append(x)

# Merge the encoder outputs for each feature back into a single tensor
output = tf.keras.layers.Concatenate(axis=2)(outputs)

您可以使用 Tensorflow 中定义的 Conv1D 函数(link) with the groups 参数为每​​个特征定义单独的过滤器。

这方面的一个例子:

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)

请注意选择 padding 类型的重要性(有关详细信息,请参阅 padding documentation)。