Search code examples
keraskeras-layer

feed LSTM in keras


I am very new to keras. I am wondering if somebody could help me how to feed a LSTM with my data which is EEG. I have 1400 trials from 306 channel with length of 600 points.

1- I want to create a LSTM network that, at each time step t, the first layer takes the input of all channels (all EEG channels are initially fed into the same LSTM layer)

2- and also another network consists of several 306 LSTMs, each connected to only one input channel at the first layer, and the second encoding layer then performs inter-channel analysis, by receiving as input the concatenated output vectors of all channel LSTMs.

Thanks


Solution

  • If I understood it correctly, the code should be something like:

    def lstm_model():
       hidden_units = 512  # may increase/decrease depending on capacity needed
       timesteps = 600
       input_dim = 306
       num_classes = 10    # num of classes for ecg output
       model = Sequential()
       model.add(LSTM(hidden_units, input_shape=(timesteps, input_dim)))
       model.add(Dense(num_classes))
       adam = Adam(lr=0.001)
       model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
       return model
    
    def train():
       xt = np.array([])  # input_data shape = (num_trials, timesteps, input_dim)
       yt = np.array([])  # out_data shape = (num_trials, num_classes)
       batch_size = 16
       epochs = 10
       model = lstm_model()
       model.fit(xt, yt, epochs=epochs, batch_size=batch_size, shuffle=True)