How to input a 2D array in Keras-Python?

Viewed 8698

I am very new to machine-learning and keras and was stuck on trying to input data; I have data which looks like:

[[[0.01363717 0.        ]
  [0.01577874 0.        ]
  [0.01463021 0.        ]]

 [[0.01577874 0.        ]
  [0.01463021 0.        ]
  [0.01006721 0.        ]]

 [[0.01463021 0.        ]
  [0.01006721 0.        ]
  [0.00762504 0.        ]]...]

The data's shape is: (1607, 3, 2). How can I pass:

 [[0.01363717 0.        ]
  [0.01577874 0.        ]
  [0.01463021 0.        ]]

as an input in a layer of 512 CuDNNLSTM cells?

Here is my whole network:

def create_model():
    model = Sequential()

    model.add(CuDNNLSTM(512, input_shape=(3,2), return_sequences=True, name='inputlstm1'))
    model.add(Dropout(0.2))

    model.add(CuDNNLSTM(512, return_sequences=True,name='lstm2'))
    model.add(Dropout(0.2))

    model.add(CuDNNLSTM(512, return_sequences=True,name='lstm3'))
    model.add(Dropout(0.2))

    model.add(Dense(32, activation='relu', name='dense1'))
    model.add(Dropout(0.2))

    model.add(Dense(1, activation='softmax', name='denseoutput2'))


    # Compile model
    model.compile(
        loss='mse',
        optimizer='adam',
        metrics=['accuracy'],
    )
    return model 

And its fitting:

model=create_model()
history=model.fit(xtrain, ytrain,batch_size=1, epochs=5, validation_data=(xtest, ytest), verbose=1)
2 Answers

Building of keras layers includes specifying the shape of passed array, here the array shape to be trained is (3,2) with 1607 samples,

input_shape = (3,2)
X = LSTM(124, activation = 'sigmoid', name='layer1', dropout = 0.4) (temp)

If you want to use stacked LSTM, you can use this

input_shape = (3,2)
    X = LSTM(124, activation = 'sigmoid', name='layer1', dropout = 0.4,return_sequences=True) (temp)
    X = LSTM(64, activation = 'sigmoid', name='layer2', dropout = 0.4) (X)

edit

def create_model():
    model = keras.models.Sequential()

    model.add(keras.layers.CuDNNLSTM(512, input_shape=(3,2), return_sequences=True, name='inputlstm1'))
    model.add(keras.layers.Dropout(0.2))

    model.add(keras.layers.CuDNNLSTM(512, return_sequences=True,name='lstm2'))
    model.add(keras.layers.Dropout(0.2))

    # The last layer of Stacked LSTM need not to return the input sequences
    model.add(keras.layers.CuDNNLSTM(512,name='lstm3'))
    model.add(keras.layers.Dropout(0.2))

    model.add(keras.layers.Dense(32, activation='relu', name='dense1'))
    model.add(keras.layers.Dropout(0.2))

    model.add(keras.layers.Dense(1, activation='softmax', name='denseoutput2'))


    # Compile model
    model.compile(
        loss='mse',
        optimizer='adam',
        metrics=['accuracy'],
    )
    return model 

You can set batch_size = 1 and Keras will take care of it. Just put the whole array to the model input.

Related