Fully implemented training of hmm.