Implementation of LSTM on edge impulse

I am trying to impement LSTM on edge impulse Expert mode and the code is as below

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, InputLayer, Dropout, Conv1D, Conv2D, Flatten, Reshape, MaxPooling1D, MaxPooling2D, BatchNormalization, TimeDistributed, LSTM,RepeatVector
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.optimizers import Adam

reshape input to be [samples, time steps, features]

X_train=tf.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test=tf.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
repeat_vector_units =X_train.shape[0]
output_units = X_train.shape[1]
#train_dataset =, Y_train))

model architecture

model = Sequential()
model.add(LSTM(50, activation=‘relu’,input_shape=(X_train.shape[1], X_train.shape[2])))
#model.add(LSTM(50, activation=‘relu’))
#model.add(Dense(classes, name=‘y_pred’))

this controls the learning rate

opt = Adam(lr=0.005, beta_1=0.9, beta_2=0.999)

this controls the batch size, or you can manipulate the objects yourself

train_dataset = train_dataset.batch(BATCH_SIZE, drop_remainder=False)
validation_dataset = validation_dataset.batch(BATCH_SIZE, drop_remainder=False)
callbacks.append(BatchLoggerCallback(BATCH_SIZE, train_sample_count))

train the neural network

model.compile(loss=‘mean_squared_error’, optimizer=opt),Y_train, epochs=5, validation_data=(X_test, Y_test), verbose=2, callbacks=callbacks)

but i get this error shown below. i reduced the epochs to get a quick output.

59/59 - 3s - loss: 26.9613 - val_loss: 20.5063
Epoch 4/5
59/59 - 3s - loss: 21.9133 - val_loss: 16.6791
Epoch 5/5
59/59 - 3s - loss: 17.1886 - val_loss: 12.5210
Finished training

Saving best performing model…
Still saving model…
Traceback (most recent call last):
File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/”, line 759, in set_shape
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes must be equal rank, but are 3 and 2

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/”, line 272, in
File “/home/”, line 180, in main_function
concrete_func = ei_tensorflow.conversion.get_concrete_function(tf_saved_model, cf_input_shape)
File “./resources/libraries/ei_tensorflow/”, line 8, in get_concrete_function
File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/”, line 762, in set_shape
raise ValueError(str(e))
ValueError: Shapes must be equal rank, but are 3 and 2

Application exited with code 1 (Error)

Job failed (see above)

When i edit as a notebook i get a model but i need it to work for edge impulse do that i can get a library compatible with arduino. can you help me understand why it works on the python notebook on google colabs and not on the edge impulse platform

Hi @marvin,

LSTM is not supported yet as it’s not implemented in TFLite Micro ( However you can get similar results with a 1D Conv network by creating features on different time scales, e.g features previously generated on 1min or 5 min windows and use them as inputs to your Neural Network.
Feel free to share more details about your project here or in DM.


So LSTM requires a specific input shape. is there a way to do this