From my experience NaN usually happens when you divide by 0 (or a small enough number). I see in your code that you divide y_i by a norm, it could be there.
The other solution is to use a debugger (or many print) and try to find a case where you get NaNs to determine during which operation it happens.