torch.tensor
时间: 2023-09-12 16:03:16 浏览: 89
Python库 | torch-tensor-type-0.0.1.tar.gz
torch.tensor is a class in the PyTorch library that is used to create a multi-dimensional array of data. It is similar to NumPy's ndarray, but with additional features optimized for deep learning applications.
The torch.tensor class can be instantiated with a Python list, tuple, or NumPy array, and can also specify the data type and device (CPU or GPU) where the tensor will be stored.
For example, to create a 2D tensor of shape (3, 2) with random values between 0 and 1, and store it on the GPU:
```
import torch
tensor = torch.tensor([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]], dtype=torch.float32, device='cuda')
print(tensor)
```
Output:
```
tensor([[0.1000, 0.2000],
[0.3000, 0.4000],
[0.5000, 0.6000]], device='cuda:0')
```
The tensor can be manipulated using various PyTorch operations such as element-wise arithmetic, matrix multiplication, and more.
阅读全文