Keras LSTM training data format

ⅰ亾dé卋堺 提交于 2019-12-03 13:10:39

The input format for the LSTM should have a shape (sequence_length, input_dim). So in your case, numpy arrays of shape (4,3) should do it.

What you will feed to the model will then be a numpy array of shape (number_of_train_examples, sequence_length, input_dim). In other words, you will feed number_of_train_examples tables of shape (4,3). Build a list of :

1,0,0
0,1,0
0,1,0
0,0,1

and then do np.array(list_of_train_example).

However, I don't understand why you return the whole sequence for the second LSTM? It will output you something with the shape (4,4), the Dense layer will probably fail on that. Return sequence means that you will return the whole sequence, so every hidden output at each step of LSTM. I would set this to False for the second LSTM to only get a "summary" vector of shape (4,) that your Dense layer can read. Anyway, even for the first LSTM it means that with an input of shape (4,3), you output something which has shape (4,4), so you will have more parameters than input data for this layer... Can't be really good.

Regarding the activations, I would also use softmax but only on the last layer, softmax is used to get probabilities as output of the layer. It doesn't make really sense to use a softmax out of LSTM's and the Dense before the last. Go for some other non linearity like "sigmoid" or "tanh".

This is what I would do model-wise

def createNet(summary=False):
    print("Start Initialzing Neural Network!")
    model = Sequential()
    model.add(LSTM(4,input_dim=input_dim,input_length=input_length,
            return_sequences=True,activation='tanh'))
    model.add(Dropout(0.1))
    # output shape : (4,4)
    model.add(LSTM(4,
            return_sequences=False,activation='tanh'))
    model.add(Dropout(0.1))
    # output shape : (4,)
    model.add(Dense(3,activation='tanh'))
    model.add(Dropout(0.1))
    # output shape : (3,)
    model.add(Dense(3,activation='softmax'))
    # output shape : (3,)
    model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])
    if summary:
        print(model.summary())
    return model
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!