LSTM错误python keras

Posted

技术标签:

【中文标题】LSTM错误python keras【英文标题】:LSTM Error python keras 【发布时间】:2017-08-28 13:09:38 【问题描述】:

早上好,我正在尝试训练 lstm 来分类垃圾邮件而不是垃圾邮件,我遇到了以下错误:

ValueError: Input 0 is incompatible with layer lstm_1: expected ndim = 3, found ndim = 4

谁能帮我理解问题出在哪里?

我的代码:

import sys
import pandas as pd
import numpy as np
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from sklearn.feature_extraction.text import CountVectorizer

if __name__ == "__main__":
    np.random.seed(7)

    with open('SMSSpamCollection') as file:
        dataset = [[x.split('\t')[0],x.split('\t')[1]] for x in [line.strip() for line in file]]

    data   = np.array([dat[1] for dat in dataset])
    labels = np.array([dat[0] for dat in dataset])

    dataVectorizer = CountVectorizer(analyzer = "word",  
                             tokenizer = None,   
                             preprocessor = None,
                             stop_words = None,  
                             max_features = 5000) 
    labelVectorizer = CountVectorizer(analyzer = "word",  
                             tokenizer = None,   
                             preprocessor = None,
                             stop_words = None,  
                             max_features = 5000) 

    data = dataVectorizer.fit_transform(data).toarray()
    labels = labelVectorizer.fit_transform(labels).toarray()
    vocab = labelVectorizer.get_feature_names()

    print(vocab)
    print(data)
    print(labels)

    data = np.reshape(data, (data.shape[0], 1, data.shape[1]))

    input_dim = data.shape
    tam = len(data[0])

    print(data.shape)
    print(tam)

    model = Sequential()
    model.add(LSTM(tam, input_shape=input_dim))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.fit(data, labels, epochs=100, batch_size=1, verbose=2)

我尝试在数据数组中添加另一个位置,但也没有结果 我的档案SMSSpamCollection

ham Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...
ham Ok lar... Joking wif u oni...
spam    Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005. Text FA to 87121 to receive entry question(std txt rate)T&C's apply 08452810075over18's
ham U dun say so early hor... U c already then say...
ham Nah I don't think he goes to usf, he lives around here though
spam    FreeMsg Hey there darling it's been 3 week's now and no word back! I'd like some fun you up for it still? Tb ok! XxX std chgs to send, £1.50 to rcv
ham Even my brother is not like to speak with me. They treat me like aids patent.
...

谢谢

【问题讨论】:

【参考方案1】:

问题实际上在于您正在添加与样本相关的附加维度。试试:

input_dim = (data.shape[1], data.shape[2])

这应该可行。

【讨论】:

以上是关于LSTM错误python keras的主要内容,如果未能解决你的问题,请参考以下文章

使用 Keras 编译 LSTM 时出现断言错误

python中kera LSTM网络的模型拟合和尺寸大小误差

Keras 功能 api 输入形状错误,lstm 层收到 2d 而不是 3d 形状

Keras LSTM 第二层(但不是第一层)的输入形状错误

将 gridsearchCV 与 Keras RNN-LSTM 一起使用时出现尺寸错误

为啥我会收到 Keras LSTM RNN input_shape 错误?