That doesn’t seem to work (I just tried t1 = t2 = torch.randn(2, 2))
This should work for arbitrarily sized matrices, though I’m not sure how fast/slow it will be:
def kronecker_product(t1, t2):
"""
Computes the Kronecker product between two tensors.
See https://en.wikipedia.org/wiki/Kronecker_product
"""
t1_height, t1_width = t1.size()
t2_height, t2_width = t2.size()
out_height = t1_height * t2_height
out_width = t1_width * t2_width
tiled_t2 = t2.repeat(t1_height, t1_width)
expanded_t1 = (
t1.unsqueeze(2)
.unsqueeze(3)
.repeat(1, t2_height, t2_width, 1)
.view(out_height, out_width)
)
return expanded_t1 * tiled_t2