简体   繁体   English

层 lstm_9 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:[None, 2, 4000, 256]

[英]Input 0 of layer lstm_9 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 2, 4000, 256]

I try to create model with RNN network but I receive: Input 0 of layer lstm_9 is incompatible with the layer: expected ndim=3, found ndim=4.我尝试使用 RNN 网络创建 model 但我收到: lstm_9 层的输入 0 与该层不兼容:预期 ndim=3,发现 ndim=4。 Full shape received: [None, 2, 4000, 256] error.收到完整形状:[None, 2, 4000, 256] 错误。

INPUT输入

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

train_labels.shape() =(100,)

labels_values = 0 or 1 (two classes)

MODEL MODEL

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) 

Your whole concept of designing Keras functional models with embedding layers is wrong, unfortunately.不幸的是,您设计带有嵌入层的 Keras 功能模型的整个概念是错误的。

  1. When you are using the embedding layer, it expects 2-d data.当您使用嵌入层时,它需要二维数据。
Input shape

2D tensor with shape: (batch_size, sequence_length).

Output shape

3D tensor with shape: (batch_size, sequence_length, output_dim).

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

It takes a sequence of IDs or tokens for the vocabulary.它需要一系列 ID 或标记作为词汇表。 This must be an integer array.这必须是 integer 阵列。

Let's say our vocabulary has len 36, we pass it a list of integer arrays in range (0, 36)假设我们的词汇表有 len 36,我们将 integer arrays 的列表传递给它,范围为 (0, 36)

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

  1. Usually, we use Embedding to represent the vectors in reduced space, so output_dim is lower than input_dim, but the opposite can be true too based on design.通常,我们使用 Embedding 来表示缩减空间中的向量,因此 output_dim 低于 input_dim,但根据设计也可以相反。

  2. You need to specify the input_length for the input data.您需要为输入数据指定 input_length。

  3. If you use return_sequences = True the temporal dimension will be passed to the next dimension, it's not desired in your case.如果您使用return_sequences = True时间维度将被传递到下一个维度,这在您的情况下是不需要的。

  4. You have labels in the form (0, 1, 0, 1, 0, 0, ...) and not in one-hot-encoded form, so don't use softmax but sigmoid with 1 unit in the last dense.您有 (0, 1, 0, 1, 0, 0, ...) 形式的标签,而不是单热编码形式,所以不要使用 softmax,而是使用最后一个密集的 1 个单位的 sigmoid。

This is the somewhat corrected network.这是经过修正的网络。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 层 lstm_9 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:[None, 300, 300, 1] - Input 0 of layer lstm_9 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 300, 300, 1] 层 lstm_35 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:[None, 1966, 7059, 256] - Input 0 of layer lstm_35 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 1966, 7059, 256] 层 lstm_11 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到完整形状:[无、5000、1、6] - Input 0 of layer lstm_11 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 5000, 1, 6] ValueError: 层 lstm 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:[无,18] - ValueError : Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 18] ValueError: 层 max_pooling1d 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:(无、128、1、32) - ValueError: Input 0 of layer max_pooling1d is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 128, 1, 32) ValueError:层 sequential_16 的输入 0 与层不兼容:预期 ndim=5,发现 ndim=4。 收到的完整形状:[无,224、224、3] - ValueError: Input 0 of layer sequential_16 is incompatible with the layer: expected ndim=5, found ndim=4. Full shape received: [None, 224, 224, 3] ValueError: 层双向的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:(无、120、1024、1024) - ValueError: Input 0 of layer bidirectional is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 120, 1024, 1024) ValueError:layersequential_1 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[无、256、256] - ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 256, 256] LSTM 模型:ValueError:层“lstm”的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:(无,7) - LSTM model: ValueError: Input 0 of layer "lstm" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 7) ValueError: 层 lstm_1 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:(无,64) - ValueError: Input 0 of layer lstm_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 64)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM