torch.Tensor.expand
时间: 2023-12-02 10:03:14 浏览: 170
torch.Tensor.expand is a method of the PyTorch library that returns a new tensor with the same data as the input tensor, but with the specified size. It is used to replicate the input tensor along specified dimensions.
For example, if we have a tensor x of size (2,3), we can use the expand method to create a new tensor y of size (2,3,4) by replicating the original tensor along the third dimension:
```python
import torch
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
y = x.expand((2, 3, 4))
print(y.size())
```
Output:
```
torch.Size([2, 3, 4])
```
In this case, we used the expand method to create a tensor of size (2,3,4) by replicating the original tensor x along the third dimension, with a size of 4.
阅读全文