Pytorch使用教学1-Tensor的创建

0 导读

在我们不知道什么是深度学习计算框架时,我们可以把PyTorch看做是Python的第三方库,在PyTorch中定义了适用于深度学习的张量Tensor,以及张量的各类计算。就相当于NumPy中定义的Array和对应的科学计算方法,正是这些基本数据类型和对应的方法函数,为我们进一步在PyTorch上进行深度学习建模提供了基本对象和基本工具。

因此,我们需要熟练掌握PyTorch中张量的基本操作方法。torch.Tensor是一种包含单一数据类型元素的多维矩阵。

Python 复制代码
import torch
torch.__version__
# '1.7.0'

1 张量的创建

张量的最基本创建方法和Numpy中创建Array的格式一致,都是创建函数的格式。

1.1 通过列表创建

Python 复制代码
t = torch.tensor([1, 2])
print(t)
# tensor([1, 2])

1.2 通过元组创建

Python 复制代码
t = torch.tensor((1, 2))
print(t)
# tensor([1, 2])

1.3 通过Numpy创建

Python 复制代码
import numpy as np
n = np.array([1, 2])
t = torch.tensor(n)
print(t)
# tensor([1, 2])

2 张量的数据类型

Python中,我们可以使用type()方法查看一个变量的数据类型。

2.1 type()

Python 复制代码
t = torch.tensor([1, 2])
print(type(t))
# <class 'torch.Tensor'>

Python环境中直接使用type()方法打印变量t的类型torch.Tensor。那么Tensor下有什么类型呢?我们需要使用dtype方法进行查看。

2.2 dtype

Python 复制代码
t = torch.tensor([1, 2])
print(t.dtype)
# torch.int64

我们可以看到t的大类是Tensor,更具体的说,它是torch.int64类型的变量。

2.3 type()dtype的不同

Python 复制代码
i = torch.tensor([1, 2])
f = torch.tensor([1.0, 2.0])
print(type(i), i.dtype, sep = ' , ')
print(type(f), f.dtype, sep = ' , ')
# <class 'torch.Tensor'> , torch.int64
# <class 'torch.Tensor'> , torch.float32

我们可以看到,type()不能识别出Tensor内部的数据类型,只能识别出变量的基本类型是Tensor,而dtype方法可以识别出变量具体为哪种类型的Tensor

2.4 PyTorchTensor的数据类型

PyTorch中我们常用Tensor的数据类型有整数型、浮点型和布尔型。具体如下:

数据类型 dtype
32bit浮点数 torch.float32或torch.float
64bit浮点数 torch.float64或torch.double
16bit浮点数 torch.half
8bit无符号整数 torch.unit8
8bit有符号整数 torch.int8
16bit有符号整数 torch.int16或torch.short
32bit有符号整数 torch.int32或torch.int
64bit有符号整数 torch.int64
布尔型 torch.bool
复数型 torch.complex64

此外,我们可以在创建张量时通过dtype参数直接定义它的类型。

Python 复制代码
t = torch.tensor([1, 2], dtype = torch.float64)
print(t.dtype)
# torch.float64

3 张量类型的转化

3.1 张量类型的隐式转化

NumpyArray相同,当张量各元素属于不同类型时,系统会自动进行隐式转化。

Python 复制代码
t = torch.tensor([1.1, 2])
print(t)
# tensor([1.1000, 2.0000])
Python 复制代码
t = torch.tensor([True, 2])
print(t)
# tensor([1, 2])

3.2 张量类型的转化方法

可以使用.float().int()等方法对张量类型进行转化。

Python 复制代码
t = torch.tensor([1, 2])
f = t.float()
print(f)
print(t)
# tensor([1., 2.])
# tensor([1, 2])

需要注意的是,这里并不会改变原来t的数据类型。

Pytorch张量操作大全:

Pytorch使用教学1-Tensor的创建
Pytorch使用教学2-Tensor的维度
Pytorch使用教学3-特殊张量的创建与类型转化
Pytorch使用教学4-张量的索引
Pytorch使用教学5-视图view与reshape的区别
Pytorch使用教学6-张量的分割与合并
Pytorch使用教学7-张量的广播
Pytorch使用教学8-张量的科学运算
Pytorch使用教学9-张量的线性代数运算
Pytorch使用教学10-张量操作方法大总结

相关推荐
代码AI弗森17 小时前
从 IDE 到 CLI:AI 编程代理工具全景与落地指南(附对比矩阵与脚本化示例)
ide·人工智能·矩阵
xchenhao18 小时前
SciKit-Learn 全面分析分类任务 breast_cancer 数据集
python·机器学习·分类·数据集·scikit-learn·svm
007tg20 小时前
从ChatGPT家长控制功能看AI合规与技术应对策略
人工智能·chatgpt·企业数据安全
Memene摸鱼日报20 小时前
「Memene 摸鱼日报 2025.9.11」腾讯推出命令行编程工具 CodeBuddy Code, ChatGPT 开发者模式迎来 MCP 全面支持
人工智能·chatgpt·agi
linjoe9920 小时前
【Deep Learning】Ubuntu配置深度学习环境
人工智能·深度学习·ubuntu
独行soc21 小时前
2025年渗透测试面试题总结-66(题目+回答)
java·网络·python·安全·web安全·adb·渗透测试
先做个垃圾出来………21 小时前
残差连接的概念与作用
人工智能·算法·机器学习·语言模型·自然语言处理
AI小书房1 天前
【人工智能通识专栏】第十三讲:图像处理
人工智能
fanstuck1 天前
基于大模型的个性化推荐系统实现探索与应用
大数据·人工智能·语言模型·数据挖掘
多看书少吃饭1 天前
基于 OpenCV 的眼球识别算法以及青光眼算法识别
人工智能·opencv·计算机视觉