LSTM Keras-值输入尺寸错误 [英] LSTM Keras- Value input dimension error

查看:105
本文介绍了LSTM Keras-值输入尺寸错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Keras实现LSTM,以解决多类问题.我输入了尺寸为1007x5的csv.每个实例的功能数量为5,共有12个类.下面是代码

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)

在此之后的不同论坛和帖子中出现此错误之后,我尝试了不同的输入形状,但仍然无法正常工作. 当我提供输入数据形状时,会出现以下错误: 1.当我将input_shape设为X.shape [1:])-错误是输入0是不兼容的层lstm_1:预期ndim = 3,发现ndim = 2"

  1. 当我给定input_shape = X.shape [1:])时,错误是值错误:检查输入时:预期lstm_1_input具有3个维,但数组的形状为(1007,5)" p>

  2. 除了形状以外,如果ndim设置为5,则表示输入0是不兼容的图层lstm_1:预期ndim = 3,找到的ndim = 2"

应该向lstm第一层输入什么?我对第1层的尺寸应该是(128,1007,5),对吧?

解决方案

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)))

这篇关于LSTM Keras-值输入尺寸错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆