10.PyTorch_元素类型转换

1、data.type(torch.DoubleTensor)

2、data.half/double/float/short/int/long()

python 复制代码
"""
案例:
    创建指定类型的张量.

涉及到的函数:
    type(torch支持的数据类型)
    half()/double()/float()/short()/int()/long()

你要掌握的函数:
    type()
"""

# 导包
import torch

# 场景1: 直接创建指定类型的张量.
t1 = torch.tensor([1, 2, 3, 4, 5], dtype=torch.float)  # 默认是: float32
print(f't1: {t1}, (元素)类型: {t1.dtype}, (张量)类型: {type(t1)}')  # float32
print('-' * 30)

# 场景2: 创建好张量后 -> 做类型转换.
# 思路1: type()函数, 推荐掌握.
t2 = t1.type(torch.int16)
print(f't2: {t2}, (元素)类型: {t2.dtype}, (张量)类型: {type(t2)}')  # int16
print('-' * 30)


# 思路2: half()/double()/float()/short()/int()/int()
print(t2.half())        # float16
print(t2.float())       # float32, 默认
print(t2.double())      # float64
print(t2.short())       # int16
print(t2.int())         # int32
print(t2.long())        # int64, 默认
相关推荐
zyplayer-doc2 分钟前
研发接口文档怎么长期维护:zyplayer-doc把API、Markdown和变更记录放进同一个知识库
大数据·数据库·人工智能·笔记·pdf·ocr
赋创小助手9 分钟前
AMD Helios AI机架技术详解:72颗MI455X、EPYC Venice与UALoE架构
人工智能·架构·amd·amd helios ai机架·mi455x·epyc venice·ualoe架构
动物园猫11 分钟前
PCB表面缺陷目标检测数据集:6类别、3,500张图像 | 目标检测
人工智能·目标检测·计算机视觉
也非非也12 分钟前
Agent支付的真正战争,不在演示台,而在后台
人工智能·ai编程·vibecoding·waic
义嘉泰15 分钟前
国产 eMMC 替代选型:XTX XT28EG08GA5SL / XT28EG16GA5SL 解析
人工智能·科技·芯片
AI人工智能+19 分钟前
高精度表格识别技术通过深度学习与计算机视觉融合,突破传统OCR局限,实现表格结构的智能解析与还原
深度学习·ocr·表格识别
好好沉淀20 分钟前
主键选择(自增 vs UUID vs 雪花算法)
java·算法
delishcomcn20 分钟前
智切未来:碳带分切机的AI进化论
人工智能
奥莱维24 分钟前
经济型酒店智能客控轻量化方案
大数据·网络·人工智能
国科安芯29 分钟前
AS32S601型抗辐射MCU在分布式太空算力架构中的技术演进与应用前景
人工智能·分布式·单片机·嵌入式硬件·架构·边缘计算