Stable Motion Primitives via Imitation and Contrastive Learning