finish nncostFunction