.slice_scatter()
Anonymous contributor
Published Jan 23, 2025
Contribute to Docs
In PyTorch, the .slice_scatter()
function inserts all values from the source tensor into the input tensor at the given dimension. It returns a new tensor with fresh storage, rather than creating a view.
Syntax
torch.slice_scatter(input, src, dim=0, start=None, end=None, step=1)
input
: The input tensor.src
: The source tensor containing the values to insert into theinput
tensor.dim
: The dimension along which the values are to be inserted. The default value is0
.start
(Optional): The starting index for inserting the values. The default value isNone
.end
(Optional): The ending index for inserting the values. The default value isNone
.step
: The number of elements to skip while inserting the values. The default value is1
.
Example
The following example demonstrates the usage of the .slice_scatter()
function:
import torch# Create a 4x4 input tensor with all elements set to '0'input = torch.zeros(4, 4)# Create a 2x4 source tensor with all elements set to '1'src = torch.ones(2, 4)# Insert the values along dimension 0 in 'input'res = torch.slice_scatter(input, src, 0, start=2)# Print the resultant tensorprint(res)
The above code produces the following output:
tensor([[0., 0., 0., 0.],[0., 0., 0., 0.],[1., 1., 1., 1.],[1., 1., 1., 1.]])
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours