搜索

pdf文档 深度学习与PyTorch入门实战 - 06. 基本数据类型

1.09 MB 16 页 1 下载 64 浏览 0 评论 0 收藏
语言 格式 评分
中文(简体)
.pdf
3
摘要
文档介绍了PyTorch中的基本数据类型,重点讲解了Tensor的创建、维度、数据类型以及在GPU上的使用。通过示例展示了如何使用FloatTensor和IntTensor,并介绍了PyTorch支持的多种数据类型及其对应的CPU和GPU张量。文档还展示了如何通过type()函数和isinstance()方法检查Tensor的类型,并提供了数据类型转换的示例。
AI总结
### PyTorch 基本数据类型总结 1. **核心概念** PyTorch 的核心是 **Tensor(张量)**,它是数据存储和计算的基本单位。PyTorch 的数据类型主要围绕 Tensor 展开。 2. **数据类型与转换** - **Python 数据类型与 PyTorch 的对应关系**: - Python 的整数(Int)对应 PyTorch 的 IntTensor。 - Python 的浮点数(Float)对应 PyTorch 的 FloatTensor。 - **数组与 PyTorch 的对应关系**: - Python 的整数数组(Int array)对应 PyTorch 的 IntTensor。 - Python 的浮点数数组(Float array)对应 PyTorch 的 FloatTensor。 - **字符串处理**:PyTorch 通过 One-Hot 编码或 Embedding 方式处理字符串数据。 3. **张量维度与形状** - **维度(Dimension)**:张量的维度决定了数据的结构。 - **Rank 0**:标量(0 维)。 - **Rank 1**:一维数组,如 `torch.tensor([1.1])` 或 `torch.tensor([1.1, 2.2])`。 - **形状(Shape)**:通过 `a.shape` 查看张量的形状,例如 `torch.Size([2])` 表示一维张量的长度为 2。 4. **数据类型** PyTorch 支持多种数据类型,包括: - **浮点数**: - `torch.float32` 或 `torch.float`:32 位浮点数。 - `torch.float64` 或 `torch.double`:64 位浮点数。 - `torch.float16` 或 `torch.half`:16 位浮点数。 - **整数**: - `torch.uint8`:8 位无符号整数。 - `torch.int8`:8 位有符号整数。 - `torch.int16` 或 `torch.short`:16 位有符号整数。 - `torch.int32` 或 `torch.int`:32 位有符号整数。 - `torch.int64` 或 `torch.long`:64 位有符号整数。 5. **数据类型检查与转换** - 使用 `isinstance()` 检查张量类型,例如: ```python isinstance(a, torch.FloatTensor) # 检查是否为 FloatTensor isinstance(data, torch.cuda.DoubleTensor) # 检查是否为 GPU 上的 DoubleTensor ``` - 使用 `type()` 查看张量类型,例如: ```python a.type() # 输出 'torch.FloatTensor' ``` 6. **GPU 支持** - PyTorch 支持 GPU 加速,通过 `.cuda()` 方法将数据移动到 GPU。 - GPU 上的张量类型包括 `torch.cuda.FloatTensor`、`torch.cuda.DoubleTensor` 等。 7. **总结** PyTorch 的数据类型系统以 Tensor 为核心,支持多种数据类型和维度结构,适用于深度学习中的数据处理和计算。通过合理的数据类型选择和转换,可以提高计算效率和准确性。
P1
P2
P3
P4
P5
P6
P7
下载文档到本地,方便使用
- 可预览页数已用完,剩余 9 页请下载阅读 -
文档评分
请文明评论,理性发言.