【问题标题】:Unable to add CNN Layer after Embedding Layer Keras嵌入层 Keras 后无法添加 CNN 层
【发布时间】:2019-08-07 10:55:30
【问题描述】:

我有 7 个分类特征 我正在尝试在嵌入层之后添加一个 CNN 层

我的第一层是输入层 第二层是嵌入层 第三层我想添加一个 Conv2D 层

我在 Conv_2D 中尝试过 input_shape=(7,36,1) 但没有成功

input2 = Input(shape=(7,))
embedding2 = Embedding(76474, 36)(input2)

# 76474 is the number of datapoints (rows)
# 36 is the output dim of embedding Layer

cnn1 = Conv2D(64, (3, 3), activation='relu')(embedding2)
flat2 = Flatten()(cnn1)

但是我收到了这个错误

 Input 0 of layer conv2d is incompatible with the layer: expected 
 ndim=4, found ndim=3. Full shape received: [None, 7, 36]

【问题讨论】:

  • 您需要使用一维卷积,因为您的数据是一维的。其次,只有当输入数据是元素顺序很重要并表示意义的序列时(例如,单词序列、基因组序列、音频样本序列),使用 1D conv 才有意义。如果这七个分类特征彼此之间没有顺序关系,那么使用卷积就没有意义了。

标签: python-3.x keras deep-learning conv-neural-network


【解决方案1】:

一个嵌入层的输出是3D的,即(samples, seq_length, features),其中features = 36是嵌入空间的维数,seq_length = 7是序列长度。 Conv2D 层需要一个图像,通常表示为 4D 张量 (samples, width, height, channels)

只有Conv1D 层才有意义,因为它还需要 3D 形状的数据,通常是 (samples, width, channels),然后您需要决定是要跨序列长度还是跨特征维度进行卷积。这是你需要试验的东西,最终是决定嵌入输出中哪个是“空间维度”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    相关资源
    最近更新 更多