torch.slice_scatter — PyTorch 2.7 documentation (original) (raw)
Embeds the values of the src
tensor into input
at the given dimension. This function returns a tensor with fresh storage; it does not create a view.
a = torch.zeros(8, 8) b = torch.ones(2, 8) a.slice_scatter(b, start=6) tensor([[0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.], [1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1.]])
b = torch.ones(8, 2) a.slice_scatter(b, dim=1, start=2, end=6, step=2) tensor([[0., 0., 1., 0., 1., 0., 0., 0.], [0., 0., 1., 0., 1., 0., 0., 0.], [0., 0., 1., 0., 1., 0., 0., 0.], [0., 0., 1., 0., 1., 0., 0., 0.], [0., 0., 1., 0., 1., 0., 0., 0.], [0., 0., 1., 0., 1., 0., 0., 0.], [0., 0., 1., 0., 1., 0., 0., 0.], [0., 0., 1., 0., 1., 0., 0., 0.]])