| 12345678910111213141516171819202122 |
- import torch
- # 创建一个 2*3 的全 0 张量
- a = torch.zeros(2, 3)
- print(a)
- # 创建一个 2*3 的全 1 张量
- b = torch.ones(2, 3)
- print(b)
- # 创建一个 2*3 的随机张量
- c = torch.randn(2, 3)
- print(c)
- # 从 NumPy 数组创建张量
- import numpy as np
- numpy_array = np.array([[1,2],[3,4]])
- tensor_from_numpy = torch.from_numpy(numpy_array)
- print(tensor_from_numpy)
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
- d = torch.randn(2,3, device=device)
- print(d)
|