@tom thanks for your comments. I have some doubts regarding your answer. This code it will supposed to work in gpu. Then, I understand the first method is inefficient. The first question is, the second method, should it work on gpu?
If the answer is yes,  I have some doubts in the second method using the accessor. Dim is the dimension of the tensor, right?