3

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 2

6

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 
Sign up to request clarification or add additional context in comments.

5 Comments

My model uses 3 LSTM Layers (each 512 neurons) and 2 Dense Layers (32 and 1). I am getting this error on output: ValueError: Error when checking target: expected output to have 3 dimensions, but got array with shape (1607, 1)
seems the error you got in the first layer only, either you passed test data for input or the number of samples , you need to pass the shape of array , plz post the network to have clear understanding
I updated the post - my model is there. Thanks for the help :)
@MPatel I just modified your network, check the update
Thank you so much!!
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.