我知道 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/