First, it is my apology to have a mistake in my original numpy code. Each entry of tensor shall be sum of index , but I did not square it. And your code may be needed a slight change, and I am trying to figure out how to make a square before taking the sum. I am very new to torch … But I learned a lot tricks from this short code given by you.