2017-06-22 2 views
3

Mon entrée est tout simplement un fichier csv avec 339732 lignes et deux colonnes:Erreur lors du contrôle d'entrée du modèle: lstm_1_input devrait avoir 3 dimensions, mais a tableau avec la forme (339732, 29)

  • le premier étant 29 valeurs de caractéristiques, à savoir X
  • la deuxième étant une valeur d'étiquette binaire, à savoir Y

Je suis en train de former mes données sur un modèle de LSTM empilés:

data_dim = 29 
timesteps = 8 
num_classes = 2 

model = Sequential() 
model.add(LSTM(30, return_sequences=True, 
       input_shape=(timesteps, data_dim))) # returns a sequence of vectors of dimension 30 
model.add(LSTM(30, return_sequences=True)) # returns a sequence of vectors of dimension 30 
model.add(LSTM(30)) # return a single vector of dimension 30 
model.add(Dense(1, activation='softmax')) 

model.compile(loss='binary_crossentropy', 
       optimizer='rmsprop', 
       metrics=['accuracy']) 

model.summary() 
model.fit(X_train, y_train, batch_size = 400, epochs = 20, verbose = 1) 

Cela jette l'erreur:

Traceback (most recent call last): File "first_approach.py", line 80, in model.fit(X_train, y_train, batch_size = 400, epochs = 20, verbose = 1)

ValueError: Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29)

J'ai essayé remodelant mon entrée à l'aide X_train.reshape((1,339732, 29)) mais cela n'a pas d'erreur indiquant:

ValueError: Error when checking model input: expected lstm_1_input to have shape (None, 8, 29) but got array with shape (1, 339732, 29)

Comment puis-je nourrir mon entrée au LSTM?

Répondre

3

Réglage timesteps = 1 (depuis, je veux un timestep pour chaque instance) et en remodelant le X_train et X_test comme:

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

Cela a fonctionné!

1

Pour timesteps != 1, vous pouvez utiliser la fonction ci-dessous (adapté de here)

import numpy as np 
def create_dataset(dataset, look_back=1): 
    dataX, dataY = [], [] 
    for i in range(len(dataset)-look_back+1): 
    a = dataset[i:(i+look_back), :] 
    dataX.append(a) 
    dataY.append(dataset[i + look_back - 1, :]) 
    return np.array(dataX), np.array(dataY) 

Exemples

X = np.reshape(range(30),(3,10)).transpose() 
array([[ 0, 10, 20], 
     [ 1, 11, 21], 
     [ 2, 12, 22], 
     [ 3, 13, 23], 
     [ 4, 14, 24], 
     [ 5, 15, 25], 
     [ 6, 16, 26], 
     [ 7, 17, 27], 
     [ 8, 18, 28], 
     [ 9, 19, 29]]) 

create_dataset(X, look_back=1) 
(array([[[ 0, 10, 20]], 
     [[ 1, 11, 21]], 
     [[ 2, 12, 22]], 
     [[ 3, 13, 23]], 
     [[ 4, 14, 24]], 
     [[ 5, 15, 25]], 
     [[ 6, 16, 26]], 
     [[ 7, 17, 27]], 
     [[ 8, 18, 28]], 
     [[ 9, 19, 29]]]), 
array([[ 0, 10, 20], 
     [ 1, 11, 21], 
     [ 2, 12, 22], 
     [ 3, 13, 23], 
     [ 4, 14, 24], 
     [ 5, 15, 25], 
     [ 6, 16, 26], 
     [ 7, 17, 27], 
     [ 8, 18, 28], 
     [ 9, 19, 29]])) 

create_dataset(X, look_back=3) 
(array([[[ 0, 10, 20], 
     [ 1, 11, 21], 
     [ 2, 12, 22]], 
     [[ 1, 11, 21], 
     [ 2, 12, 22], 
     [ 3, 13, 23]], 
     [[ 2, 12, 22], 
     [ 3, 13, 23], 
     [ 4, 14, 24]], 
     [[ 3, 13, 23], 
     [ 4, 14, 24], 
     [ 5, 15, 25]], 
     [[ 4, 14, 24], 
     [ 5, 15, 25], 
     [ 6, 16, 26]], 
     [[ 5, 15, 25], 
     [ 6, 16, 26], 
     [ 7, 17, 27]], 
     [[ 6, 16, 26], 
     [ 7, 17, 27], 
     [ 8, 18, 28]], 
     [[ 7, 17, 27], 
     [ 8, 18, 28], 
     [ 9, 19, 29]]]), 
array([[ 2, 12, 22], 
     [ 3, 13, 23], 
     [ 4, 14, 24], 
     [ 5, 15, 25], 
     [ 6, 16, 26], 
     [ 7, 17, 27], 
     [ 8, 18, 28], 
     [ 9, 19, 29]]))