What's worked really well for me (and I had to completely retrain my leg because I learned to ride from my knees) was to have my instructor remind me when I got to the fence "Soften your knees!" or "Leg, leg, leg!" I know it sounds kind of silly, but if she can repeat it enough it really does stick.
It also helps if you have a really flat saddle, so you can think about keeping your knees completely off of the leather. If you don't have a really flat saddle see if someone has one you can borrow for a while. The saddle does more than you think it does!