When I train my model, I always randomly get the following error:

IndexError: tensors used as indices must be long, byte or bool tensors.

I know what this error message saying about but why this error comes out randomly? And should I add .long() whenever trying to index a tensor by another tensor?

If this error is thrown randomly, the index tensor is most likely changing the type â€śrandomlyâ€ť in your code base.
I would recommend to narrow down, why the index tensor changes the type and make sure you are using valid indices.

Thanks, Since this error is thrown randomly, the program runs normally for some iterations until raise this error, it is really hard to find where does the index tensor get changed. And I think I havenâ€™t change the type of index tensor explicitly, do you know which operations in pytorch might change the index tensor implicitly?