【问题标题】:Input 0 of layer lstm_9 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 2, 4000, 256]层 lstm_9 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。收到的完整形状:[None, 2, 4000, 256]
【发布时间】:2020-08-13 09:24:19
【问题描述】:

我尝试使用 RNN 网络创建模型,但收到:lstm_9 层的输入 0 与该层不兼容:预期 ndim=3,发现 ndim=4。收到完整形状:[None, 2, 4000, 256] 错误。

输入

train_data.shape() = (100,2,4000)

train_labels.shape() =(100,)

labels_values = 0 or 1 (two classes)

型号

input = Input(shape=(2,4000)) # shape from train_data
embedded = Embedding(2, 256)(input) 
lstm = LSTM(1024, return_sequences=True)(embedded) # ERROR
dense = Dense(2, activation='softmax')(lstm) 

【问题讨论】:

    标签: python neural-network recurrent-neural-network


    【解决方案1】:

    很遗憾,您设计带有嵌入层的 Keras 功能模型的整个概念是错误的。

    1. 当您使用嵌入层时,它需要二维数据。
    Input shape
    
    2D tensor with shape: (batch_size, sequence_length).
    
    Output shape
    
    3D tensor with shape: (batch_size, sequence_length, output_dim).
    

    参考:https://keras.io/layers/embeddings/

    词汇表需要一系列 ID 或标记。这必须是一个整数数组。

    假设我们的词汇表有 len 36,我们向它传递一个范围为 (0, 36) 的整数数组列表

    [1, 34, 32, 23] 有效 [0.2, 0.5] 无效

    1. 通常,我们使用 Embedding 来表示缩减空间中的向量,因此 output_dim 低于 input_dim,但根据设计,也可以相反。

    2. 需要指定输入数据的input_length。

    3. 如果您使用 return_sequences = True,时间维度将被传递到下一个维度,您的情况不希望这样做。

    4. 您有 (0, 1, 0, 1, 0, 0, ...) 形式的标签,而不是单热编码形式,所以不要使用 softmax,而是使用 1 个单位的 sigmoid在最后一个密集。

    这是经过修正的网络。

    from tensorflow.keras.layers import *
    from tensorflow.keras.models import *
    import numpy as np
    train_data = np.random.randint(0,3, (100, 4000))
    y_labels = np.random.randint(0,2, (100,))
    
    input_ = Input(shape=(4000)) # shape from train_data
    embedded = Embedding(36, 256, input_length = 4000)(input_) 
    lstm = LSTM(256, return_sequences=False)(embedded) # --> ERROR
    dense = Dense(1, activation='softmax')(lstm) 
    
    model = Model(input_, dense)
    model.summary()
    
    Model: "model"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    input_6 (InputLayer)         [(None, 4000)]            0         
    _________________________________________________________________
    embedding_5 (Embedding)      (None, 4000, 256)         9216      
    _________________________________________________________________
    lstm_5 (LSTM)                (None, 256)               525312    
    _________________________________________________________________
    dense (Dense)                (None, 1)                 257       
    =================================================================
    Total params: 534,785
    Trainable params: 534,785
    Non-trainable params: 0
    

    【讨论】:

    • 我有时间序列类型的数据,其中 train_datat[0] 是时间,train_data[1] 是幅度,两者都采用 x.x 形式。我的任务是创建 RNN 模型。我是完整的 bwginner :) 在这种情况下怎么办?如果我理解我的数据格式不正确
    • 是的。嵌入层不与浮点时间序列数据一起使用,我认为如果没有巧妙的操作,就没有明确的向量空间来映射。那就去掉嵌入层吧。
    猜你喜欢
    • 2020-09-01
    • 2020-11-15
    • 2020-08-11
    • 2021-01-14
    • 2021-04-09
    • 2021-11-13
    • 2021-03-22
    • 1970-01-01
    • 2019-12-17
    相关资源
    最近更新 更多