Solved by AlbertZhang in post #2 
              
              
                The loops in your customized loss function slow down the computation. You can try to re-code it into no-loops forms to speed up the computation.