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技术几秒前
云栖实录 | 从多模态数据到 Physical AI,PAI 助力客户快速启动 Physical AI 实践
人工智能
小关会打代码8 分钟前
计算机视觉进阶教学之颜色识别
人工智能·计算机视觉
IT小哥哥呀14 分钟前
基于深度学习的数字图像分类实验与分析
人工智能·深度学习·分类
布林模型20 分钟前
缠论工具czsc快速使用入门(二)
python·缠论·快速入门·czsc
邂逅you34 分钟前
用python操作mysql之pymysql库基本操作
数据库·python·mysql
啊森要自信37 分钟前
【GUI自动化测试】YAML 配置文件应用:从语法解析到 Python 读写
android·python·缓存·pytest·pip·dash
机器之心38 分钟前
VAE时代终结?谢赛宁团队「RAE」登场,表征自编码器或成DiT训练新基石
人工智能·openai
机器之心39 分钟前
Sutton判定「LLM是死胡同」后,新访谈揭示AI困境
人工智能·openai
大模型真好玩43 分钟前
低代码Agent开发框架使用指南(四)—Coze大模型和插件参数配置最佳实践
人工智能·agent·coze
jerryinwuhan43 分钟前
基于大语言模型(LLM)的城市时间、空间与情感交织分析:面向智能城市的情感动态预测与空间优化
人工智能·语言模型·自然语言处理