How can I achieve this efficiently?

Hello everyone, now, I have a 2-D tensor, A = [1, 2 ; 3, 4], and a 1-D vector, B = [5, 6]. I want to achieve C which is C = [15, 25; 36, 46]. Can pytorch0.4.1 calculate this efficiently? Sure, I know that this can be achieved by A*diag(B), but diag(B) need too much memory. Looking forward to your reply.


You can use expand and element-wise multiplication:
C = A * B.unsqueeze(1).expand_as(A).