Assume I have a 3*3 tensor a which is
a = torch.rand(3,3)
tensor([[0.5456, 0.2233, 0.1322],
[0.6037, 0.4913, 0.6274],
[0.6362, 0.7671, 0.6741]])
and a 2*2 tensor b which is
b = torch.tensor([[10,20], [30,40]])
tensor([[10, 20],
[30, 40]])
How can I assign tensor b onto specific position on tensor a, for example I want to get like this efficiently (without for loop)
tensor([[10.0000, 0.2233, 20.0000],
[ 0.6037, 0.4913, 0.6274],
[30.0000, 0.7671, 40.0000]])
I’ve tried using a mask to extract a part of a, and assign b to a view of tensor a like below, but seems failed.
mask: tensor([[1, 0, 1],
[0, 0, 0],
[1, 0, 1]], dtype=torch.uint8)
a[mask] = b
RuntimeError: expand(torch.LongTensor{[2, 2]}, size=[4]): the number of sizes provided (1) must be greater or equal to the number of dimensions in the tensor (2)
or
c = a[mask].view(2,2)
c = b
but seems c and a do not share memory
Thanks!
Supplement: for each iteration, I always want to put tensor b into the same position.
In this example, I always want to put a 2 * 2 tensor into the four corners of a 3 * 3 tensor