Search code examples
pythonswiftneural-networkcoremlcoremltools

Process sequence by LSTM (Core ML)


I've converted Caffe model with LSTM to CoreML via coremltools. Now I'm trying to execute it. But, I can't find a way to process whole sequence

np.ndarray( (7, #sequence
             1, # batch
             120, 1, 1)) #items dims

because I can't find a way to set only initials of hidden state (LSTM_1_c_in) and initial history (LSTM_1_h_in) and automatically use previous states/result while processing next item of sequence.

It works via manually restarting method 'predict' with manually set LSTM_1_c_in and LSTM_1_h_in from previous outputs (the model reutrns LSTM_1_h_out and LSTM_1_c_out respectively).

Is it possible to process whole sequence via 1 run?

P.S. ways using Swift are also acceptible.


Solution

  • For example, LSTM has num_outputs equals to 3.

    • If lstm gets initial history and state as np.ndarray((1,1,3)) it will proccess only one element of sequence. This was my mistake

    • If lstm gets initial history and state as np.ndarray((3)) it will process the whole sequence (but it returns only result of processing last element without whole history).