Understanding target.eq(pred).sum().template item<int64_t>();

I am just starting out with libtorch and pytorch as well, so I apologize if I haven’t searched enough for an answer before posting.

Looking at some examplary code found online and trying that on my own machine I have stumbled upon this expression: target.eq(pred).sum().template item<int64_t>();

I have never seen a .template item<>() like expression in C++ before, maybe I am just too unexperienced with templates in C++ but I don’t understand what that is. Is it some form of template metaprogramming that I am not familiar with? Or something libtorch specific?

The template keyword is to disambiguate the actual call, as otherwise the expression might be understood by the compiler as item less-than int64_t greater-than ().
This post goes into detail and can explain it way better than I can. :wink:

1 Like