In this article, an innovative offline deep imitation learning algorithm for optimal trajectory planning is proposed. While many state‐of‐the‐art works achieved optimal trajectory planning, their systems were stable or quasistable, and their approaches rarely optimized the system's initial conditions (ICs). Here, a new unstable dynamic system task called “internal sliding object stabilization control” is proposed, modeled, and solved by deep imitation learning. Given the system's ICs, the neural networks (NNs) can imitate the iterative linear quadratic regulator (iLQR), generate optimal trajectories, and compute faster. A proportional–integral–derivative (PID) controller is used to track the unstable trajectories. Leveraging on the gradients of NNs, it can optimize the system's ICs, avoid obstacles stepwise, and ensure the worst bounds of NNs for safety. Subsequently, thorough simulations are conducted, including comparing the iLQR and PID controllers in the task, optimizing the system's different ICs by gradient descent, and finding the worst bound of the performance by gradient ascent. Results show that the proposed algorithm achieves considerably improved performance. Finally, experiments are conducted with a real manipulator to compare the proposed structure with the original iLQR. Results indicate that the proposed algorithm resembles the iLQR well. Program code and experiment results are in https://github.com/DanielYamChen/ISOSC.git.