PyTorch torch.expand_as Function
PyTorch torch Reference Manual
torch.expand_asis a function in PyTorch used to expand a tensor to the same dimensions as another tensor. It istorch.expanda convenient version that automatically uses the dimensions of the reference tensor for expansion.
Function Definition
torch.expand_as(other)
Parameters:
other(Tensor): The reference tensor; the current tensor will be expanded to the same dimensions as this tensor.
Return Value:
torch.Tensor: Returns the expanded tensor view.
Usage Examples
Example
import torch
# Create a vector
x = torch.tensor([1, 2, 3, 4])
# Create a target matrix
other = torch.randn(3, 4)
# Expand x to the same shape as other
y = x.expand_as(other)
print("Original vector shape:", x.shape)
print("Reference tensor shape:", other.shape)
print("Expanded shape:", y.shape)
print("nExpanded tensor:")
print(y)
# Create a vector
x = torch.tensor([1, 2, 3, 4])
# Create a target matrix
other = torch.randn(3, 4)
# Expand x to the same shape as other
y = x.expand_as(other)
print("Original vector shape:", x.shape)
print("Reference tensor shape:", other.shape)
print("Expanded shape:", y.shape)
print("nExpanded tensor:")
print(y)
The output is:
原始向量形状: torch.Size([4])
参考张量形状: torch.Size([3, 4])
扩展后形状: torch.Size([3, 4])
扩展后的张量:
tensor([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])
Example
import torch
# Create a column vector
x = torch.tensor([[1], [2], [3]])
# Create a target 3D tensor
other = torch.randn(3, 4, 5)
# Expand x to the same shape as other
y = x.expand_as(other)
print("Original column vector shape:", x.shape)
print("Reference tensor shape:", other.shape)
print("Expanded shape:", y.shape)
# Create a column vector
x = torch.tensor([[1], [2], [3]])
# Create a target 3D tensor
other = torch.randn(3, 4, 5)
# Expand x to the same shape as other
y = x.expand_as(other)
print("Original column vector shape:", x.shape)
print("Reference tensor shape:", other.shape)
print("Expanded shape:", y.shape)
The output is:
原始列向量形状: torch.Size([3, 1]) 参考张量形状: torch.Size([3, 4, 5]) 扩展后形状: torch.Size([3, 4, 5])
Example
import torch
# Example of application in neural networks
# Assume there is a bias vector that needs to be broadcast to the feature map
bias = torch.tensor([0.1, 0.2, 0.3]) # Bias for 3 channels
# Simulate the feature map output of convolution (batch, channel, height, width)
feature_map = torch.randn(8, 3, 32, 32)
# Expand the bias to the same shape as the feature map
bias_expanded = bias.expand_as(feature_map)
print("Bias shape:", bias.shape)
print("Feature map shape:", feature_map.shape)
print("Expanded bias shape:", bias_expanded.shape)
# Add the bias
output = feature_map + bias_expanded
print("nOutput shape after adding bias:", output.shape)
# Example of application in neural networks
# Assume there is a bias vector that needs to be broadcast to the feature map
bias = torch.tensor([0.1, 0.2, 0.3]) # Bias for 3 channels
# Simulate the feature map output of convolution (batch, channel, height, width)
feature_map = torch.randn(8, 3, 32, 32)
# Expand the bias to the same shape as the feature map
bias_expanded = bias.expand_as(feature_map)
print("Bias shape:", bias.shape)
print("Feature map shape:", feature_map.shape)
print("Expanded bias shape:", bias_expanded.shape)
# Add the bias
output = feature_map + bias_expanded
print("nOutput shape after adding bias:", output.shape)
The output is:
偏置形状: torch.Size([3]) 特征图形状: torch.Size([8, 3, 32, 32]) 扩展后偏置形状: torch.Size([8, 3, 32, 32]) 加上偏置后的输出形状: torch.Size([8, 3, 32, 32])
Note:torch.expand_asOnly dimensions of size 1 can be expanded to larger sizes.otherThe tensor's size must be compatible with the expanded size (i.e., the current tensor's dimensions can be expanded to the target size via broadcasting). It returns a view, not a copy.
Other Extensions