0

I got it when I was running:

def case3():
    a = torch.randn(2,2)
    torch.kron(a,a.T)

but it works for torch.kron(a,a) And then I try:

def case4():
    a = torch.randn(1,4)
    torch.kron(a,a.T)

It works! So I am confusing why torch.kron would not work on tensor of size 2x2? Thanks!

Engineero
  • 12,340
  • 5
  • 53
  • 75

1 Answers1

0

I have solved it by myself!

The reason lies in that .T actually only reshape and shows the tensor instead of changing the actual tensor in memory. so by modifying it as this, it solves the problem

def case4():
    a = torch.randn(2,2)
    b = a.T.contiguous()
    print(torch.kron(a,b))
    print(torch.kron(a,a))

.contiguous makes a copy of tensor

referring to What does .contiguous() do in PyTorch?

Engineero
  • 12,340
  • 5
  • 53
  • 75
  • But it still doubts that why torch.mul(a,a.T) can work but torch.kron didn't. And kron can actually work for 2D matrix of any shape, why it didn't work? – user21146003 Feb 04 '23 at 11:43