Python Keras中沿一维的全局算子?

Python Keras中沿一维的全局算子?,python,tensorflow,keras,dimension-reduction,Python,Tensorflow,Keras,Dimension Reduction,假设我有一个包含灰度视频的数据集。每个视频的长度和大小可能不同,因此我通过以下形状以三维形式表示数据: 批量大小 时间 Y x 渠道 没有一个 没有一个 没有一个 没有一个 1. 如果不想使用自定义图层,我相信您可以执行以下操作: x = Input(shape=(Batch_size, time, y, x, 1)) # Process data wrt time and spaciall info, as many layer as you want x = Conv3D(filters_n

假设我有一个包含灰度视频的数据集。每个视频的长度和大小可能不同,因此我通过以下形状以三维形式表示数据:

批量大小 时间 Y x 渠道 没有一个 没有一个 没有一个 没有一个 1.
如果不想使用自定义图层,我相信您可以执行以下操作:

x = Input(shape=(Batch_size, time, y, x, 1))
# Process data wrt time and spaciall info, as many layer as you want
x = Conv3D(filters_num_0, kernel_size_0, strides=1, padding="same", activation=activation_0)(x) # (Batch_size, time, y, x, filters_num_0)
x = Conv3D(filters_num_1, kernel_size_1, strides=1, padding="same", activation=activation_1)(x) # (Batch_size, time, y, x, filters_num_1)
# # Aggregate channel info
x = Conv3D(1, kernel_size_2, strides=1, padding="same", activation=activation_2)(x) # (Batch_size, time, y, x, 1)
# Reshape for dimension reduce
x = Reshape(target_shape=(Batch_size, y, x, time))(x) 
# Aggregate time info
x = Conv2D(16, kernel_size_3, strides=1, padding="same", activation=activation_3 ) # (Batch_size, y, x, 16)
使用自定义层跨时间为全局平均池执行以下操作:

class GlobalAvgPoolAcrossTime(layers.Layer):
    def __init__(self, **kwargs):
        super(GlobalAvgPoolAcrossTime, self).__init__(**kwargs)

    # (Batch_size, time, y, x, channels) -> (Batch_size, 1, y, x, channels)
    def call(self, inputs):
        return keras.backend.mean(inputs, axis=1, keepdims=True)

x = Input(shape=(Batch_size, time, y, x, 1))
# Process data wrt time and spaciall info, as many layer as you want
x = Conv3D(filters_num_0, kernel_size_0, strides=1, padding="same", activation=activation_0)(x) # (Batch_size, time, y, x, filters_num_0)
x = Conv3D(filters_num_1, kernel_size_1, strides=1, padding="same", activation=activation_1)(x) # (Batch_size, time, y, x, filters_num_1)
x = GlobalAvgPoolAcrossTime()(x) # (Batch_size, 1, y, x, filters_num_1)
# Reshape for dimension reduce
x = Reshape(target_shape=(Batch_size, y, x, filters_num_1))(x)
x = Conv2D(16, kernel_size_2, strides=1, padding="same", activation=activation_3 ) # (Batch_size, y, x, 16)

将功能发送到一个,而不是将时间点发送到一个,这是一个我从未考虑过的好主意。我不确定这将如何影响模型的性能,但值得一试。不过,实际上我更喜欢你的第二个想法。我不知道定制Keras图层这么容易制作。我将把这个问题保留到本周末,以便其他人可以提供他们的解决方案,但在尝试自定义池层之后,我将在本周晚些时候接受您的回答。非常感谢您的时间和努力。@Anthony没问题,总是很乐意帮助:),如果您尝试后遗漏了什么,请告诉我。@Anthony您尝试过吗?结果如何?