Some cats have limited tolerance for repeated stroking, and once sensation crosses a threshold they whip around and bite, called petting-induced aggression, not bad character.
A signal chain usually precedes it: tail twitching, skin rippling, ears rotating sideways, and the head turning toward the hand; stop at any link.
Adjust by shortening sessions, sticking to the head and cheeks most cats accept, avoiding the tail base and belly, and letting the cat re-initiate to extend.
If touching one specific spot always triggers attacks, especially with flinching or cries, get that area checked for pain; pain-driven aggression is not a training problem.