keras - 如何在 keras 中实现 Conv1DTranspose?

标签 keras keras-layer

我知道 keras 中有 Conv2DTranspose 可以在 Image 中使用。我们需要在 NLP 中使用它,因此需要进行 1D 反卷积。

我们如何在 keras 中实现 Conv1DTranspose?

最佳答案

使用 keras 后端将输入张量拟合到 2D 转置卷积。不要总是使用转置操作,因为它会消耗大量时间。

import keras.backend as K
from keras.layers import Conv2DTranspose, Lambda


def Conv1DTranspose(input_tensor, filters, kernel_size, strides=2, padding='same'):
    """
        input_tensor: tensor, with the shape (batch_size, time_steps, dims)
        filters: int, output dimension, i.e. the output tensor will have the shape of (batch_size, time_steps, filters)
        kernel_size: int, size of the convolution kernel
        strides: int, convolution step size
        padding: 'same' | 'valid'
    """
    x = Lambda(lambda x: K.expand_dims(x, axis=2))(input_tensor)
    x = Conv2DTranspose(filters=filters, kernel_size=(kernel_size, 1), strides=(strides, 1), padding=padding)(x)
    x = Lambda(lambda x: K.squeeze(x, axis=2))(x)
    return x

关于keras - 如何在 keras 中实现 Conv1DTranspose?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44061208/

相关文章:

python - 精度比 gridsearchCV 低

python - Keras、Tensorflow - 计算指标时 K.epsilon 的含义是什么

python - Keras 层之间的自定义连接

python - TensorFlow 分配内存 : Allocation of 38535168 exceeds 10% of system memory

python - 如何使用 Keras 创建自定义激活函数?

python - 设置 Keras 模型可训练与使每一层可训练之间有什么区别

tensorflow - 如何更改 channel 数量以在 Keras 中微调 VGG16 网络

machine-learning - 使用keras识别手写数字

python - 使用 ImageDataGenerator 时 Keras 卡住

python - 在推理时删除 Keras 模型中的辅助分支