python - LSTM输入形状错误: Input 0 is incompatible with layer sequential_1

标签 python tensorflow keras lstm multiclass-classification

我是机器学习和 Keras 新手。我试图为我的分类问题创建一个 LSTM 模型,但收到此错误:(我从互联网上获得了一些样本并尝试修改它们)

ValueError:输入0与层sequential_1不兼容:预期形状=(无,无,30),发现形状= [无,3,1]

这就是我需要的,我有一个像这样的序列1,2,3,4,其中1,2,3是我的X_train,4是标签(Y),所以我的意思是时间步长是3并且每个只有一个功能

我的标签有 30 个类别。所以我期望输出是这 30 个类之一。 64是内存单元的数量。

这是我的代码

def get_lstm():
    model = Sequential()  
    model.add(LSTM(64, input_shape=(3, 30), return_sequences=True))  
    model.add(LSTM(64))  
    model.add(Dropout(0.2))  
    model.add(Dense(30, activation='softmax'))

X_train = user_data[:, 0:3]
X_train = np.asarray(X_train).astype(np.float32)  
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
Y_train = user_data[:, 3]    
Y_train = np.asarray(Y_train).astype(np.float32)
local_model = Mymodel.get_lstm()  
local_model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
local_model.set_weights(global_weights)           
local_model.fit(X_train, Y_train, batch_size=32,
                                        epochs=1)

如果您需要更多信息或不清楚,请告诉我。我真的需要你们的帮助,谢谢

最佳答案

不确定为什么要将第一个 LSTM 的输入形状设置为 (3,30)。正如你提到的 -

This is what I need, I have a sequence like this 1,2,3,4 which 1,2,3 are my X_train and 4 is label(Y). so I mean timestep size is 3 and each has one feature only

如果您有 3 个时间步骤,并且只有一个特征,那么您应该这样定义每个序列。

此外,由于模型将始终输出 30 长度的概率分布,但您的 y_train 是单个值(在唯一的 30 个类中),因此您需要使用损失 sparse_categorical_crossentropy 而不是 categorical_crossentropyRead more here .

from tensorflow.keras import layers, Model, utils

#Dummy data and its shapes
X = np.random.random((100,3,1)) #(100,3,1)
y = np.random.randint(0,29,(100,)) #(100,)

#Design model
inp = layers.Input((3,1))
x = layers.LSTM(64, return_sequences=True)(inp)
x = layers.LSTM(64)(x)
x = layers.Dropout(0.2)(x)
out = layers.Dense(30, activation='softmax')(x)
model = Model(inp, out)

#Compile and fit
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
model.fit(X, y, batch_size=32,epochs=3)
Epoch 1/3
4/4 [==============================] - 0s 4ms/step - loss: 3.4005 - accuracy: 0.0400
Epoch 2/3
4/4 [==============================] - 0s 5ms/step - loss: 3.3953 - accuracy: 0.0700
Epoch 3/3
4/4 [==============================] - 0s 8ms/step - loss: 3.3902 - accuracy: 0.0900
utils.plot_model(model, show_layer_names=False, show_shapes=True)

enter image description here

关于python - LSTM输入形状错误: Input 0 is incompatible with layer sequential_1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65327942/

相关文章:

python - 如何为keras使用自定义损失函数

python - 隐藏直方图

python - 在 wxpython 中设置背景图像

tensorflow - TF Keras ModelCheckpoint 文件路径批号

machine-learning - TensorFlow CIFAR-10 示例教程中的 Distorted_inputs() 函数如何获取每批 128 张图像?

keras - 无论如何要在带有 AMD GPU 的 Mac 中使用 Keras?

tensorflow - 序列数据上的 LSTM,预测离散列

python - 使用 subprocess.call 时如何输入 sudo 密码?

python - 为什么使用 .conf 文件而不是 config.py 文件?

python - 如何在 TFRecord 中保存不同长度的列表列表?