Traditional stall prediction methods often rely on empirical formulas and models, which have certain limitations. The deep learning model was introduced to predict stall and surge under distorted inflow conditions in an axial flow compressor, and the model can learn from the dynamic pressure data containing stall processes measured on the casing wall by use of a long short-term memory neural network. In order to enhance the model's generalization capability and prediction accuracy, the model parameters are optimized through the Northern Goshawk optimization algorithm. In the experimental validation, the stall prediction model was first trained by using the collected stalling signal. Then, a step-by-step prediction method was used to verify the accuracy of the prediction model under uniform and distorted inflow conditions. Subsequently, the recursive prediction technology is used to predict the instability under different inflow conditions in both subsonic and supersonic axial flow compressors. By comparing with the measured stalling data under uniform and distorted inflow, the model accurately and timely predicts stall and surge signal through a self-learning mechanism when inputting non-stall pressure data. Regardless of whether the instability routes are spikes and modal-wave stall or surge, the model can predict the instability at least 1 s in advance, and it leaves enough time for the anti-surge actuator to operate. This study not only significantly improves the real-time and accuracy of predictions but also demonstrates the potential application value of deep learning in the field of aero engines, contributing to enhanced safety and reliability of aircraft engines.