optimize training code