This is the piecewise training code for learning parameters for random field