""" import torch x = torch.tensor([[1.0, 2.0, 3.0]]) # shape (1, 3) n = 4 Broadcast to…
- User
""" import torch
x = torch.tensor([[1.0, 2.0, 3.0]]) # shape (1, 3) n = 4
Broadcast to (4, 3)
y = x.expand(n, -1) """ What does -1 mean in x.expand
Assistant
Of course! That's an excellent question.
In
x.expand(n, -1), the-1is a special placeholder that means:"Do not change the size of this dimension. Infer it from the original tensor."
Let's break it down with your example:
-
Original Tensor:
x = torch.tensor([[1.0, 2.0, 3.0]])- The shape of
xis(1, 3).
-
The
.expand()Call:y = x.expand(4, -1)- PyTorch processes the arguments
(4, -1)one by one, matching them to the dimensions ofx.
-
Dimension by Dimension:
- First dimension (dim 0): The original size is
1. You are asking to expand it to size4. This is a valid operation because you can expand a dimension of size 1 to any larger size. - Second dimension (dim 1): The original size is
3. You provided-1. PyTorch sees the-1and knows to keep the original size for this dimension, which is3.
- First dimension (dim 0): The original size is
So, the call
x.expand(4, -1)is interpreted by PyTorch asx.expand(4, 3).
Why is this useful?
Using
-1makes your code more robust and readable. You don't have to manually get the size of that dimension.Compare these two ways of doing the same thing:
The "Hardcoded" Way (Less Flexible)
x = torch.tensor([[1.0, 2.0, 3.0]]) # shape (1, 3) n_class = x.shape[1] # Manually get the size of the second dimension y = x.expand(4, n_class) # Use the variable hereThis works, but it's more verbose. If you change the number of classes in
x, you don't have to change this line, but it's still an extra step.The
-1Way (More Flexible and Common)x = torch.tensor([[1.0, 2.0, 3.0]]) # shape (1, 3) y = x.expand(4, -1) # -1 automatically means 3 in this caseThis is concise and directly states your intent: "Expand the first dimension to 4 and leave the other one alone." If you later change
xto have shape(1, 100), this line of code will still work perfectly without any changes.Key Takeaway
x.expand(..., -1, ...)The
-1tells PyTorch to keep the size of that specific dimension from the original tensorx. It's a powerful convenience feature for writing clean and flexible code.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.