[英]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 功能模型的整个概念是错误的。
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] 无效
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,但根据设计也可以相反。
You need to specify the input_length for the input data.您需要为输入数据指定 input_length。
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
时间维度将被传递到下一个维度,这在您的情况下是不需要的。
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.