且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LSTM Keras-值输入尺寸错误

更新时间:2023-12-01 23:28:52

LSTM需要3维输入.您的输入形状应采用(样本,时间步长,特征)的形式.由于keras推断第一个维度是样本,因此您应该输入(时间步长,要素)作为输入形状.

由于您的csv尺寸为1007 * 5,因此我认为***的做法是将输入的形状调整为(1007,5,1),这样LSTM可以获取3D输入.

因此在load_data内部:

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

在create_model内:

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

I am trying to implement LSTM using Keras for a multi class problem. I have input csv of dimension 1007x5. number of features per instances are 5 and there are total 12 classes. 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. 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)"

  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"

What should be input to lstm first layer? My dimension to layer 1 should be (128,1007,5), right?

LSTMs require a 3 dimensional input. 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.

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.

So inside load_data:

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

And inside create_model:

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