78.md 7.8 KB
Newer Older
片刻小哥哥's avatar
片刻小哥哥 已提交
1 2 3 4 5 6 7 8 9 10 11
# 张量属性

> 原文: [https://pytorch.org/docs/stable/tensor_attributes.html](https://pytorch.org/docs/stable/tensor_attributes.html)

每个`torch.Tensor`具有 [`torch.dtype`](#torch.torch.dtype "torch.torch.dtype")[`torch.device`](#torch.torch.device "torch.torch.device")[`torch.layout`](#torch.torch.layout "torch.torch.layout")

## torch类型

* * *

```
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
12
class torch.dtype
片刻小哥哥's avatar
片刻小哥哥 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
```

[`torch.dtype`](#torch.torch.dtype "torch.torch.dtype") 是表示 [`torch.Tensor`](tensors.html#torch.Tensor "torch.Tensor") 的数据类型的对象。 PyTorch 具有九种不同的数据类型:

| 

数据类型

 | 

dtype

 | 

张量类型

 |
| --- | --- | --- |
| 32 位浮点 | `torch.float32``torch.float` | `torch.*.FloatTensor` |
| 64 位浮点 | `torch.float64``torch.double` | `torch.*.DoubleTensor` |
| 16 位浮点 | `torch.float16``torch.half` | `torch.*.HalfTensor` |
| 8 位整数(无符号) | `torch.uint8` | `torch.*.ByteTensor` |
| 8 位整数(有符号) | `torch.int8` | `torch.*.CharTensor` |
| 16 位整数(有符号) | `torch.int16``torch.short` | `torch.*.ShortTensor` |
| 32 位整数(有符号) | `torch.int32``torch.int` | `torch.*.IntTensor` |
| 64 位整数(有符号) | `torch.int64``torch.long` | `torch.*.LongTensor` |
| 布尔型 | `torch.bool` | `torch.*.BoolTensor` |

要确定 [`torch.dtype`](#torch.torch.dtype "torch.torch.dtype") 是否为浮点数据类型,可以使用属性 [`is_floating_point`](torch.html#torch.is_floating_point "torch.is_floating_point") ,如果数据类型为浮点数据,则返回`True`。 类型。

当算术运算的输入 dtypes(<cite></cite><cite></cite><cite>div</cite><cite>mul</cite> )不同时,我们通过寻找最小值来促进 满足以下规则的 dtype:

*   如果标量操作数的类型比张量操作数(浮动>整数>布尔值)具有更高的类别,则我们将其提升为具有足够大小的类型,以容纳该类别的所有标量操作数。

*   如果零维张量操作数的类别高于维操作数的类别,我们将提升为具有足够大小和类别的类型,以容纳该类别的所有零维张量操作数。

*   如果没有更高类别的零维操作数,我们将提升为具有足够大小和类别的类型以容纳所有尺寸的操作数。

浮点标量操作数的 dtype 为 <cite>torch.get_default_dtype()</cite>,整数非布尔标量操作数的 dtype 为 <cite>torch.int64</cite> 。 与 numpy 不同,在确定操作数的最小 <cite>dtypes</cite> 时,我们不检查值。 尚不支持量化和复杂类型。

促销示例:

```
>>> float_tensor = torch.ones(1, dtype=torch.float)
>>> double_tensor = torch.ones(1, dtype=torch.double)
>>> int_tensor = torch.ones(1, dtype=torch.int)
>>> long_tensor = torch.ones(1, dtype=torch.long)
>>> uint_tensor = torch.ones(1, dtype=torch.uint8)
>>> double_tensor = torch.ones(1, dtype=torch.double)
>>> bool_tensor = torch.ones(1, dtype=torch.bool)
# zero-dim tensors
>>> long_zerodim = torch.tensor(1, dtype=torch.long)
>>> int_zerodim = torch.tensor(1, dtype=torch.int)

>>> torch.add(5, 5).dtype
torch.int64
# 5 is an int64, but does not have higher category than int_tensor so is not considered.
>>> (int_tensor + 5).dtype
torch.int32
>>> (int_tensor + long_zerodim).dtype
torch.int32
>>> (long_tensor + int_tensor).dtype
torch.int64
>>> (bool_tensor + long_tensor).dtype
torch.int64
>>> (bool_tensor + uint_tensor).dtype
torch.uint8
>>> (float_tensor + double_tensor).dtype
torch.float64
>>> (bool_tensor + int_tensor).dtype
torch.int32
# Since long is a different kind than float, result dtype only needs to be large enough
# to hold the float.
>>> torch.add(long_tensor, float_tensor).dtype
torch.float32

```

```
When the output tensor of an arithmetic operation is specified, we allow casting to its dtype except that:
```

*   积分输出张量不能接受浮点张量。

*   布尔输出张量不能接受非布尔张量。

投放示例:

```
# allowed:
>>> float_tensor *= double_tensor
>>> float_tensor *= int_tensor
>>> float_tensor *= uint_tensor
>>> float_tensor *= bool_tensor
>>> float_tensor *= double_tensor
>>> int_tensor *= long_tensor
>>> int_tensor *= uint_tensor
>>> uint_tensor *= int_tensor

# disallowed (RuntimeError: result type can't be cast to the desired output type):
>>> int_tensor *= float_tensor
>>> bool_tensor *= int_tensor
>>> bool_tensor *= uint_tensor

```

## torch设备

* * *

```
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
124
class torch.device
片刻小哥哥's avatar
片刻小哥哥 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
```

[`torch.device`](#torch.torch.device "torch.torch.device") 是表示在其上或将要分配 [`torch.Tensor`](tensors.html#torch.Tensor "torch.Tensor") 的设备的对象。

[`torch.device`](#torch.torch.device "torch.torch.device") 包含设备类型(`'cpu'``'cuda'`)和该设备类型的可选设备序号。 如果不存在设备序号,则即使调用 [`torch.cuda.set_device()`](cuda.html#torch.cuda.set_device "torch.cuda.set_device") ,该对象也始终代表设备类型的当前设备。 例如,用设备`'cuda'`构造的 [`torch.Tensor`](tensors.html#torch.Tensor "torch.Tensor") 等效于`'cuda:X'`,其中 X 是 [`torch.cuda.current_device()`](cuda.html#torch.cuda.current_device "torch.cuda.current_device") 的结果。

可以通过 [`Tensor.device`](tensors.html#torch.Tensor.device "torch.Tensor.device") 属性访问 [`torch.Tensor`](tensors.html#torch.Tensor "torch.Tensor") 的设备。

[`torch.device`](#torch.torch.device "torch.torch.device") 可以通过字符串或通过字符串和设备序号构造

通过字符串:

```
>>> torch.device('cuda:0')
device(type='cuda', index=0)

>>> torch.device('cpu')
device(type='cpu')

>>> torch.device('cuda')  # current cuda device
device(type='cuda')

```

通过字符串和设备序数:

```
>>> torch.device('cuda', 0)
device(type='cuda', index=0)

>>> torch.device('cpu', 0)
device(type='cpu', index=0)

```

注意

函数中的 [`torch.device`](#torch.torch.device "torch.torch.device") 参数通常可以用字符串替换。 这样可以快速编写代码原型。

```
>>> # Example of a function that takes in a torch.device
>>> cuda1 = torch.device('cuda:1')
>>> torch.randn((2,3), device=cuda1)

```

```
>>> # You can substitute the torch.device with a string
>>> torch.randn((2,3), device='cuda:1')

```

Note

出于遗留原因,可以通过单个设备序号(被视为 cuda 设备)构造设备。 这与 [`Tensor.get_device()`](tensors.html#torch.Tensor.get_device "torch.Tensor.get_device") 匹配,后者为 cuda 张量返回序数,而 cpu 张量不支持此序数。

```
>>> torch.device(1)
device(type='cuda', index=1)

```

Note

使用设备的方法通常会接受(正确格式化的)字符串或(旧式)整数设备序数,即以下所有等效方法:

```
>>> torch.randn((2,3), device=torch.device('cuda:1'))
>>> torch.randn((2,3), device='cuda:1')
>>> torch.randn((2,3), device=1)  # legacy

```

## torch布局

* * *

```
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
203
class torch.layout
片刻小哥哥's avatar
片刻小哥哥 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
```

[`torch.layout`](#torch.torch.layout "torch.torch.layout") 是表示 [`torch.Tensor`](tensors.html#torch.Tensor "torch.Tensor") 的内存布局的对象。 目前,我们支持`torch.strided`(密集张量),并为`torch.sparse_coo`(稀疏 COO 张量)提供实验性支持。

`torch.strided`代表密集的张量,是最常用的内存布局。 每个跨步张量都有一个关联的`torch.Storage`,它保存其数据。 这些张量提供了存储的多维[跨度](https://en.wikipedia.org/wiki/Stride_of_an_array)视图。 步幅是一个整数列表:第 k 个步幅表示在张量的第 k 个维度中从一个元素到下一个元素所需的内存跳转。 这个概念使得有可能高效地执行许多张量运算。

例:

```
>>> x = torch.Tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
>>> x.stride()
(5, 1)

>>> x.t().stride()
(1, 5)

```

有关`torch.sparse_coo`张量的更多信息,请参见 [torch.sparse](sparse.html#sparse-docs)