Given a one-dimensional input tensor S
, I want to evaluate the following expression:
J_ij = S_i(delta_ij - S_j)
where delta_ij
represents the Kronecker delta. The result J
of this expression is a square matrix. I would like to know how I can efficiently evaluate this expression using PyTorch? My current implementation is very slow and looks as follows:
S = torch.arange(start=1, end=10)
I = torch.eye(S.size()[0])
S = S.repeat(S.size()[0], 1)
J = S.T * (I - S)
I would be very happy if someone could give me a hint.