Pytorch常用函数(1)——创建、索引、切片、连接、压缩

🍅 写在前面

👨‍🎓 博主介绍:大家好,这里是hyk写算法了吗,一枚致力于学习算法和人工智能领域的小菜鸟。

🔎个人主页:主页链接(欢迎各位大佬光临指导)

⭐️近期专栏:机器学习与深度学习

LeetCode算法实例

张量分解

目录

(0)创建操作

0.1 torch.eye

bash 复制代码
torch.eye(n, m=None, out=None)

返回一个2维张量,对角线位置全1,其它位置全0

参数:
n (int ) -- 行数
m (int, optional) -- 列数.如果为None,则默认为n
out (Tensor, optinal) - Output tensor
返回值: 对角线位置全1,其它位置全0的2维张量
返回值类型: Tensor

例子:

bash 复制代码
>>> torch.eye(3)
 1  0  0
 0  1  0
 0  0  1
[torch.FloatTensor of size 3x3]

0.2 torch.linspace

bash 复制代码
torch.linspace(start, end, steps=100, out=None) → Tensor

返回一个1维张量,包含在区间start 和 end 上均匀间隔的steps个点。 输出1维张量的长度为steps。

参数:
start (float) -- 序列的起始点
end (float) -- 序列的最终值
steps (int) -- 在start 和 end间生成的样本数
out (Tensor, optional) -- 结果张量

例子:

bash 复制代码
>>> torch.linspace(3, 10, steps=5)

  3.0000
  4.7500
  6.5000
  8.2500
 10.0000
[torch.FloatTensor of size 5]

>>> torch.linspace(-10, 10, steps=5)

-10
 -5
  0
  5
 10
[torch.FloatTensor of size 5]

>>> torch.linspace(start=-10, end=10, steps=5)

-10
 -5
  0
  5
 10
[torch.FloatTensor of size 5]

0.3 torch.ones

bash 复制代码
torch.ones(*sizes, out=None) → Tensor

返回一个全为1 的张量,形状由可变参数sizes定义。

参数:
sizes (int...) -- 整数序列,定义了输出形状
out (Tensor, optional) -- 结果张量

bash 复制代码
 例子:
>>> torch.ones(2, 3)

 1  1  1
 1  1  1
[torch.FloatTensor of size 2x3]

>>> torch.ones(5)

 1
 1
 1
 1
 1
[torch.FloatTensor of size 5]

0.4 torch.rand

bash 复制代码
torch.rand(*sizes, out=None) → Tensor

返回一个张量,包含了从区间[0,1)的均匀分布中抽取的一组随机数,形状由可变参数sizes 定义。

参数:
sizes (int...) -- 整数序列,定义了输出形状
out (Tensor, optinal) - 结果张量

例子:

bash 复制代码
>>> torch.rand(4)

 0.9193
 0.3347
 0.3232
 0.7715
[torch.FloatTensor of size 4]

>>> torch.rand(2, 3)

 0.5010  0.5140  0.0719
 0.1435  0.5636  0.0538
[torch.FloatTensor of size 2x3]

0.5 torch.randn

bash 复制代码
torch.randn(*sizes, out=None) → Tensor

返回一个张量,包含了从标准正态分布(均值为0,方差为 1,即高斯白噪声)中抽取一组随机数,形状由可变参数sizes定义。

参数:
sizes (int...) -- 整数序列,定义了输出形状
out (Tensor, optinal) - 结果张量

例子:

bash 复制代码
>>> torch.randn(4)

-0.1145
 0.0094
-1.1717
 0.9846
[torch.FloatTensor of size 4]

>>> torch.randn(2, 3)

 1.4339  0.3351 -1.0999
 1.5458 -0.9643 -0.3558
[torch.FloatTensor of size 2x3]

0.6 torch.arange

bash 复制代码
torch.arange(start, end, step=1, out=None) → Tensor

返回一个1维张量,长度为 floor((end−start)/step)。包含从start到end,以step为步长的一组序列值(默认步长为1)。

参数:
start (float) -- 序列的起始点
end (float) -- 序列的终止点
step (float) -- 相邻点的间隔大小
out (Tensor, optional) -- 结果张量

例子:

bash 复制代码
>>> torch.arange(1, 4)

 1
 2
 3
[torch.FloatTensor of size 3]

>>> torch.arange(1, 2.5, 0.5)

 1.0000
 1.5000
 2.0000
[torch.FloatTensor of size 3]

0.7 torch.zeros

bash 复制代码
torch.zeros(*sizes, out=None) → Tensor

返回一个全为标量 0 的张量,形状由可变参数sizes 定义。

参数:
sizes (int...) -- 整数序列,定义了输出形状
out (Tensor, optional) -- 结果张量

例子:

bash 复制代码
>>> torch.zeros(2, 3)

 0  0  0
 0  0  0
[torch.FloatTensor of size 2x3]

>>> torch.zeros(5)

 0
 0
 0
 0
 0
[torch.FloatTensor of size 5]

(1)索引,切片,连接,换位

1.1 torch.cat

bash 复制代码
torch.cat(inputs, dimension=0) → Tensor

在给定维度上对输入的张量序列seq 进行连接操作。

参数:

inputs (sequence of Tensors) -- 可以是任意相同Tensor 类型的python 序列

dimension (int, optional) -- 沿着此维连接张量序列。

例子:

bash 复制代码
>>> x = torch.randn(2, 3)
>>> x

 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
[torch.FloatTensor of size 2x3]

>>> torch.cat((x, x, x), 0)

 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
[torch.FloatTensor of size 6x3]

>>> torch.cat((x, x, x), 1)

 0.5983 -0.0341  2.4918  0.5983 -0.0341  2.4918  0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735  1.5981 -0.5265 -0.8735  1.5981 -0.5265 -0.8735
[torch.FloatTensor of size 2x9]

1.2 torch.stack

bash 复制代码
torch.stack(sequence, dim=0)

沿着一个新维度对输入张量序列进行连接。 序列中所有的张量都应该为相同形状。

参数:
sqequence (Sequence) -- 待连接的张量序列
dim (int) -- 插入的维度。必须介于 0 与 待连接的张量序列数之间。

1.3 torch.chunk

bash 复制代码
torch.chunk(tensor, chunks, dim=0)

在给定维度(轴)上将输入张量进行分块儿。

参数:
tensor (Tensor) -- 待分块的输入张量
chunks (int) -- 分块的个数
dim (int) -- 沿着此维度进行分块

1.4 torch.split

bash 复制代码
torch.split(tensor, split_size, dim=0)

将输入张量分割成相等形状的chunks(如果可分)。 如果沿指定维的张量形状大小不能被split_size 整分, 则最后一个分块会小于其它分块。

参数:
tensor (Tensor) -- 待分割张量
split_size (int) -- 单个分块的形状大小
dim (int) -- 沿着此维进行分割

1.5 torch.squeeze

bash 复制代码
torch.squeeze(input, dim=None, out=None)

将输入张量形状中的1 去除并返回。 如果输入是形如(A×1×B×1×C×1×D)

,那么输出形状就为: (A×B×C×D)当给定dim时,那么挤压操作只在给定维度上。例如,输入形状为: (A×1×B), squeeze(input, 0) 将会保持张量不变,只有用 squeeze(input, 1),形状会变成 (A×B)。

注意: 返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个。

参数:
input (Tensor) -- 输入张量
dim (int, optional) -- 如果给定,则input只会在给定维度挤压
out (Tensor, optional) -- 输出张量

例子:

bash 复制代码
>>> x = torch.zeros(2,1,2,1,2)
>>> x.size()
(2L, 1L, 2L, 1L, 2L)
>>> y = torch.squeeze(x)
>>> y.size()
(2L, 2L, 2L)
>>> y = torch.squeeze(x, 0)
>>> y.size()
(2L, 1L, 2L, 1L, 2L)
>>> y = torch.squeeze(x, 1)
>>> y.size()
(2L, 2L, 1L, 2L)

1.6 torch.unsqueeze

bash 复制代码
torch.unsqueeze(input, dim, out=None)

返回一个新的张量,对输入的制定位置插入维度 1

注意: 返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个。

如果dim为负,则将会被转化dim+input.dim()+1

参数:
tensor (Tensor) -- 输入张量
dim (int) -- 插入维度的索引
out (Tensor, optional) -- 结果张量

bash 复制代码
>>> x = torch.Tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
 1  2  3  4
[torch.FloatTensor of size 1x4]
>>> torch.unsqueeze(x, 1)
 1
 2
 3
 4
[torch.FloatTensor of size 4x1]

1.7 torch.transpose

bash 复制代码
torch.transpose(input, dim0, dim1, out=None) → Tensor

返回输入矩阵input的转置。交换维度dim0和dim1。 输出张量与输入张量共享内存,所以改变其中一个会导致另外一个也被修改。

参数:
input (Tensor) -- 输入张量
dim0 (int) -- 转置的第一维
dim1 (int) -- 转置的第二维

bash 复制代码
>>> x = torch.randn(2, 3)
>>> x

 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
[torch.FloatTensor of size 2x3]

>>> torch.transpose(x, 0, 1)

 0.5983  1.5981
-0.0341 -0.5265
 2.4918 -0.8735
[torch.FloatTensor of size 3x2]

1.8 torch.index_select

bash 复制代码
torch.index_select(input, dim, index, out=None) → Tensor

沿着指定维度对输入进行切片,取index中指定的相应项(index为一个LongTensor),然后返回到一个新的张量, 返回的张量与原始张量_Tensor_有相同的维度(在指定轴上)。

注意: 返回的张量不与原始张量共享内存空间。

参数:
input (Tensor) -- 输入张量
dim (int) -- 索引的轴
index (LongTensor) -- 包含索引下标的一维张量
out (Tensor, optional) -- 目标张量

例子:

bash 复制代码
>>> x = torch.randn(3, 4)
>>> x

 1.2045  2.4084  0.4001  1.1372
 0.5596  1.5677  0.6219 -0.7954
 1.3635 -1.2313 -0.5414 -1.8478
[torch.FloatTensor of size 3x4]

>>> indices = torch.LongTensor([0, 2])
>>> torch.index_select(x, 0, indices)

 1.2045  2.4084  0.4001  1.1372
 1.3635 -1.2313 -0.5414 -1.8478
[torch.FloatTensor of size 2x4]

>>> torch.index_select(x, 1, indices)

 1.2045  0.4001
 0.5596  0.6219
 1.3635 -0.5414
[torch.FloatTensor of size 3x2]

1.9 torch.gather

bash 复制代码
torch.gather(input, dim, index, out=None) → Tensor

沿给定轴dim,将输入索引张量index指定位置的值进行聚合。

对一个3维张量,输出可以定义为:

out[i][j][k] = tensor[index[i][j][k]][j][k] # dim=0

out[i][j][k] = tensor[i][index[i][j][k]][k] # dim=1

out[i][j][k] = tensor[i][j][index[i][j][k]] # dim=3

参数:
input (Tensor) -- 源张量
dim (int) -- 索引的轴
index (LongTensor) -- 聚合元素的下标
out (Tensor, optional) -- 目标张量

例子:

bash 复制代码
>>> t = torch.Tensor([[1,2],[3,4]])
>>> torch.gather(t, 1, torch.LongTensor([[0,0],[1,0]]))
 1  1
 4  3
[torch.FloatTensor of size 2x2]
相关推荐
AngelPP3 小时前
OpenClaw 架构深度解析:如何把 AI 助手搬到你的个人设备上
人工智能
宅小年3 小时前
Claude Code 换成了Kimi K2.5后,我再也回不去了
人工智能·ai编程·claude
AI探索者3 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者3 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
九狼3 小时前
Flutter URL Scheme 跨平台跳转
人工智能·flutter·github
ZFSS3 小时前
Kimi Chat Completion API 申请及使用
前端·人工智能
天翼云开发者社区4 小时前
春节复工福利就位!天翼云息壤2500万Tokens免费送,全品类大模型一键畅玩!
人工智能·算力服务·息壤
知识浅谈4 小时前
教你如何用 Gemini 将课本图片一键转为精美 PPT
人工智能
FishCoderh4 小时前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅4 小时前
Python函数入门详解(定义+调用+参数)
python