简体   繁体   English

LSTM Keras-值输入尺寸错误

[英]LSTM Keras- Value input dimension error

I am trying to implement LSTM using Keras for a multi class problem. 我正在尝试使用Keras实现LSTM来解决多类问题。 I have input csv of dimension 1007x5. 我输入了尺寸为1007x5的csv。 number of features per instances are 5 and there are total 12 classes. 每个实例的功能数量为5,共有12个类。 Below is the code 下面是代码

seed = 7
numpy.random.seed(seed)

input_file = 'input.csv'

def load_data(test_split = 0.2):
    print ('Loading data...')
    dataframe = pandas.read_csv(input_file, header=None)
    dataset = dataframe.values

    X = dataset[:,0:5].astype(float)
    print(X)
    Y = dataset[:,5]
    print("y=", Y)    
    return X,Y


def create_model(X):
    print ('Creating model...')
    model = Sequential()

    model.add(LSTM(128, input_shape =(5,)))
    model.add(Dense(12, activation='sigmoid'))

    print ('Compiling...')
    model.compile(loss='categorical_crossentropy',
                  optimizer='rmsprop',
                  metrics=['accuracy'])
    return model


X,Y,dummy_y= load_data()
print("input Lnegth X=",len(X[0]))

model = create_model(X)

print ('Fitting model...')
hist = model.fit(X, Y, batch_size=5, nb_epoch=10, validation_split = 0.1, verbose = 1)


score, acc = model.evaluate(dummy_x,dummy_y)
print('Test score:', score)
print('Test accuracy:', acc)

Following this error in different forums and posts on here, I have tried different inputs shapes but still it is not working. 在不同的论坛和此处的帖子中出现此错误之后,我尝试了不同的输入形状,但仍然无法正常工作。 When I am giving input data shape then I get following errors: 1. when I give input_shape as X.shape[1:]) - error is "input 0 is incompatible layer lstm_1: expected ndim =3, found ndim=2" 当我给定输入数据形状时,会出现以下错误:1.当我给input_shape作为X.shape [1:])-错误是“输入0是不兼容的层lstm_1:预期ndim = 3,发现ndim = 2”

  1. When I give input_shape=X.shape[1:]), error is "value error: when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (1007,5)" 当我给定input_shape = X.shape [1:])时,错误是“值错误:检查输入时:预期lstm_1_input具有3维,但数组的形状为(1007,5)”

  2. Other than shape, if ndim is set to 5, it says "input 0 is incompatible layer lstm_1: expected ndim =3, found ndim=2" 除形状外,如果ndim设置为5,则表示“输入0是不兼容的层lstm_1:预期ndim = 3,找到的ndim = 2”

What should be input to lstm first layer? lstm第一层应该输入什么? My dimension to layer 1 should be (128,1007,5), right? 我对第1层的尺寸应该是(128,1007,5),对吗?

LSTMs require a 3 dimensional input. LSTM需要3维输入。 Your input shape should be in the form of (samples, timesteps, features). 您的输入形状应采用(样本,时间步长,特征)的形式。 Since keras infers the first dimension is samples, you should be inputting (timesteps, features) as your input shape. 由于keras推断第一个维度是样本,因此您应该输入(时间步长,要素)作为输入形状。

Since your csv is of dimension 1007*5, I think that the best course of action is to reshape your input to (1007, 5, 1), so your LSTM can get a 3D input. 由于您的csv尺寸为1007 * 5,因此我认为最好的做法是将输入的形状调整为(1007,5,1),这样LSTM可以获取3D输入。

So inside load_data: 因此在load_data内部:

X = X.reshape(X.shape[0], x.shape[1], 1)

And inside create_model: 在create_model里面:

model.add(LSTM(128, input_shape =(5,1)))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM