gradient decent template matcher