简体   繁体   English

Keras LSTM 输入 ValueError:形状不兼容

[英]Keras LSTM input ValueError: Shapes are incompatible

Not sure about why I'm getting an error with my LSTM neural network.不确定为什么我的 LSTM 神经网络出现错误。 It seems to be related with the input shape.它似乎与输入形状有关。

This is my neural network architecture:这是我的神经网络架构:

from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout

model = Sequential()

# Recurrent layer
model.add(LSTM(64, return_sequences=False, 
           dropout=0.1, recurrent_dropout=0.1))

# Fully connected layer
model.add(Dense(64, activation='relu'))

# Dropout for regularization
model.add(Dropout(0.5))

# Output layer
model.add(Dense(y_train.nunique(), activation='softmax'))

# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

This is how I train it:这是我训练它的方式:

history = model.fit(X_train_padded, y_train_padded, 
                    batch_size=2048, epochs=150,
                    validation_data=(X_test_padded, y_test_padded))

This is the shape of my input data:这是我输入数据的形状:

print(X_train_padded.shape, X_test_padded.shape, y_train_padded.shape, y_test_padded.shape)
(98, 20196, 30) (98, 4935, 30) (98, 20196, 1) (98, 4935, 1)

This is part of my X_train_padded:这是我的 X_train_padd 的一部分:

X_train_padded
array([[[ 2.60352379e-01, -1.66420518e-01, -3.12893162e-01, ...,
         -1.51210476e-01, -3.56188897e-01, -1.02761131e-01],
        [ 1.26103191e+00, -1.66989382e-01, -3.13025807e-01, ...,
          6.61329839e+00, -3.56188897e-01, -1.02761131e-01],
        [ 1.04418243e+00, -1.66840157e-01, -3.12994596e-01, ...,
         -1.51210476e-01, -3.56188897e-01, -1.02761131e-01],
        ...,
        [ 1.27399408e+00, -1.66998426e-01, -3.13025807e-01, ...,
          6.61329839e+00, -3.56188897e-01, -1.02761131e-01],

This is the error that I'm getting:这是我得到的错误:

Epoch 1/150
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-52-52422b54faa4> in <module>
----> 1 history = model.fit(X_train_padded, y_train_padded, 
      2                     batch_size=2048, epochs=150,
      3                     validation_data=(X_test_padded, y_test_padded))
...
ValueError: Shapes (None, 20196) and (None, 12) are incompatible

As I'm using a LSTM layer, I have a 3D input shape.当我使用LSTM层时,我有一个 3D 输入形状。 My output layer has 12 nodes (y_train.nunique()) because I have 12 different classes in my input.我的输出层有 12 个节点 (y_train.nunique()),因为我的输入中有 12 个不同的类。 Given that I have 12 classes, I'm using softmax as activation function in my output layer and categorical_crossentropy as my loss function.鉴于我有 12 个类,我在输出层使用softmax作为激活函数,使用categorical_crossentropy作为损失函数。

EDIT:编辑:

Let me try to explain better my dataset :让我试着更好地解释我的数据集

I'm dealing with geological wells.我正在处理地质井。 My samples are different types of sedimentary rocks layers, where the features are the rocks' properties (such as gammay ray emission) and the label is the rock type (such as limestone).我的样本是不同类型的沉积岩层,其中特征是岩石的属性(如伽马射线发射),标签是岩石类型(如石灰岩)。 One of my features is the depth of the layer.我的特点之一是层的深度。

The idea behing using an LSTM in this case, is to consider the depth of a well as a sequence.在这种情况下使用 LSTM 的想法是将井的深度视为序列。 So that the previous sedimentary layer (rock) helps to predict the next sedimentary layer (rock).这样前一个沉积层(岩石)有助于预测下一个沉积层(岩石)。

How did I get to my input shape:我是如何得到我的输入形状的:

I have a total of 98 wells in my dataset.我的数据集中共有98口井。 I splitted the dataset: X_train_init, X_test_init, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) .X_train_init, X_test_init, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)了数据集: X_train_init, X_test_init, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) The well with the most samples (layers) has, in the training set, 20196 samples.具有最多样本(层)的井在训练集中有20196 个样本。 The wells that didn't have this many samples, I padded them with zeros so that they had 20196 samples.没有这么多样本的井,我用零填充它们,以便它们有20196 个样本。 The well with the most samples (layers) has, in the test set, 4935 samples.具有最多样本(层)的井在测试集中有4935 个样本。 The wells that didn't have this many samples, I padded them with zeros so that they had 4935 samples.没有这么多样品的孔,我用零填充它们,这样它们就有4935 个样品。 Removing the well feature and the depth feature (among other features) I ended up with 30 features total.删除特征和深度特征(以及其他特征),我最终得到了30 个特征。 My y_train and y_test has only 1 column which represents the label.我的y_trainy_test只有1列代表标签。

I guess that my problem is actually getting this dataset to work in a LSTM.我想我的问题实际上是让这个数据集在 LSTM 中工作。 Most of the examples that I see, don't have 98 different time series, they just have one.我看到的大多数示例都没有 98 个不同的时间序列,它们只有一个。 I'm not really sure about how to deal with 98 different time series (wells).我不太确定如何处理 98 个不同的时间序列(井)。

It won't work.它不会工作。 Except for the batch size, every other input dimension should be the same.除了批量大小外,所有其他输入维度都应该相同。 Also, your inputs dimensions are all going crazy.此外,您的输入维度都变得疯狂。 for example -例如 -

print(X_train_padded.shape, # (98, 20196, 30)
X_test_padded.shape, # (98, 4935, 30)
y_train_padded.shape, # (98, 20196, 1)
y_test_padded.shape) # (98, 4935, 1)

from what I see the first dimension is supposed to represent the total number of samples (in x_train,y_train, and x_test,y_test) but in your case, the total samples are represented by the second dimension.从我看到的第一个维度应该代表样本总数(在 x_train、y_train 和 x_test、y_test 中),但在您的情况下,总样本由第二个维度表示。 The first dimension should be in second place.第一个维度应该排在第二位。 So as to say the dimensions should所以说尺寸应该

print(X_train_padded.shape, # (20196, 98, 30)
X_test_padded.shape, # (4935, 98, 30)
y_train_padded.shape, # (20196, 98, 1)
y_test_padded.shape) # (4935, 98, 1)

This will put everything in the right place.这会将所有内容放在正确的位置。 You just need to look at how you came to the wrong dimensions and change that part.你只需要看看你是如何得到错误的尺寸并改变那个部分的。

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

相关问题 ValueError:LSTM 模型中的形状不兼容 - ValueError: Shapes are incompatible in LSTM model ValueError:使用 RandomizedSearchCV 的 Tensorflow LSTM 中的形状不兼容 - ValueError: Shapes are incompatible in Tensorflow LSTM using RandomizedSearchCV 不兼容的形状:Tensorflow/Keras Sequential LSTM with Autoencoder - Incompatible Shapes: Tensorflow/Keras Sequential LSTM with Autoencoder Keras Layer LSTM中的输入不兼容 - Incompatible input in Keras Layer LSTM Keras 2 输入 model,不兼容的形状 - Keras 2 input model, incompatible Shapes ValueError: Shapes (None, 1) 和 (None, 64) 是不兼容的 Keras - ValueError: Shapes (None, 1) and (None, 64) are incompatible Keras Keras ValueError:形状 (32, 2) 和 (32, 4) 不兼容 - Keras ValueError: Shapes (32, 2) and (32, 4) are incompatible Keras LSTM ValueError:层“顺序”的输入 0 与层不兼容:预期形状 =(无,478405,33),找到形状 =(1、33) - Keras LSTM ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 478405, 33), found shape=(1, 33) Keras model 形状不兼容/ValueError:形状(无,3)和(无,3,3)不兼容 - Keras model shape incompatible / ValueError: Shapes (None, 3) and (None, 3, 3) are incompatible LSTM nlp 多类模型中的错误:- ValueError:Shapes (None, 1) 和 (None, 3) 不兼容 - Error in LSTM nlp multiclass model :- ValueError: Shapes (None, 1) and (None, 3) are incompatible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM