pytorch tensor prodcast tensor with size (1,nclass) to (n, nclass)
- User
pytorch tensor prodcast tensor with size (1,n_class) to (n, n_class)
Assistant
To broadcast a PyTorch tensor of shape
(1, n_class)to shape(n, n_class), you can use the.expand()method or rely on automatic broadcasting in operations.
✅ Option 1: Using
.expand()import torch # Example input x = torch.randn(1, n_class) # e.g., x.shape = (1, 10) n = 5 expanded_x = x.expand(n, -1) # -1 means keep the original size for that dimension print(expanded_x.shape) # Output: torch.Size([5, 10])This is memory-efficient because it returns a view of the original tensor (no copy).
✅ Option 2: Using
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.