torch.tensor()的简单实用示例

参考链接: torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False)
代码实验展示:

Microsoft Windows [版本 10.0.18363.1256]
(c) 2019 Microsoft Corporation。保留所有权利。

C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0

(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]]) # 从列表接收数据
tensor([[0.1000, 1.2000],
        [2.2000, 3.1000],
        [4.9000, 5.2000]])
>>>
>>> torch.tensor([0, 1])  # Type inference on data 从列表中的数据类型推断出所要使用的类型
tensor([0, 1])
>>>
>>>
>>> torch.tensor([[0.11111, 0.222222, 0.3333333]],
...             dtype=torch.float64,
...             device=torch.device('cuda:0'))  # creates a torch.cuda.DoubleTensor
tensor([[0.1111, 0.2222, 0.3333]], device='cuda:0', dtype=torch.float64)
>>>
>>>
>>> torch.tensor(3.14159)  # Create a scalar (zero-dimensional tensor)
tensor(3.1416)
>>> # 创建一个标量
>>>
>>> torch.tensor([])  # Create an empty tensor (of size (0,))
tensor([])
>>>
>>>
>>> print(torch.__version__)
1.2.0+cu92
>>> print(torch.cuda.is_available())
True
>>>
>>>

你可能感兴趣的:(torch.tensor()的简单实用示例)