陕西手机网站建设,设计书籍频道开放说明,网站建设的目标,福田网站建设推荐查看张量形状
有两种方法查看张量形状:
通过属性查看 Tensor.shape通过方法查看 Tensor.size()
两种方式的结果都是一个 torch.Size 类型(元组的子类)的对象 t torch.empty(3, 4)t.size()
torch.Size([3, 4])
# 获取 dim1 维度的 sizet torch.empty(3, 4)t.size()
torch.Size([3, 4])
# 获取 dim1 维度的 sizet.size(dim1)
4t.shape
torch.Size([3, 4])isinstance(t.shape, tuple)
True改变张量形状
返回新的张量
以下方法均要求: 形状改变前后元素的数量一致, 名称以 as 结尾的方法表示参考其他张量的形状
Tensor.view(*shape)Tensor.view_as(other) Tensor.reshape(*shape)Tensor.reshape_as(other)
在张量数据连续(contiguous)的情况下, 这两种方式均共享底层数据 t torch.arange(6)t_1 t.view(2, 3)t_1
tensor([[0, 1, 2],[3, 4, 5]])
# 可以有一个维度是 -1, PyTorch 根据其余维度推理该维度的大小t_2 t.reshape(3, -1)t_2
tensor([[0, 1],[2, 3],[4, 5]])
# 获取底层数据地址p t.storage().data_ptr()p_1 t_1.storage().data_ptr()p_2 t_2.storage().data_ptr()
# 底层数据地址相等p p_1 p_2
True当张量数据不连续时(例如改变张量的维度顺序), 无法使用 Tensor.view() 方法, 而 Tensor.reshape() 方法会拷贝底层数据 a torch.tensor([[0, 1],[2, 3]])a
tensor([[0, 1],[2, 3]])
# 转置操作t a.t()t
tensor([[0, 2],[1, 3]])
# t 已经不连续了, 当然 a 还是连续的t.is_contiguous()
False# t.view 抛出异常t.view(1, 4)
-----------------------------------------------------------
RuntimeError Traceback (most recent call last)
Input In [10], in cell line: 1()
---- 1 t.view(1, 4)RuntimeError: view size is not compatible with input tensors size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.# t.reshape 会拷贝数据t_2 t.reshape(1, 4)t_2
tensor([[0, 2, 1, 3]])t.storage().data_ptr() t_2.storage().data_ptr()
False就地修改张量
这是一个比较底层的方法, 以 _ 为结尾表示直接修改张量本身, 不会创建新的张量, 并且不要求前后元素的数量一致
Tensor.resize_(*sizes)Tensor.resize_as_(other)
代码示例: t torch.arange(5)t
tensor([0, 1, 2, 3, 4])t.resize_(2, 2)
tensor([[0, 1],[2, 3]])
# 原张量已被修改t
tensor([[0, 1],[2, 3]])调整维度顺序
矩阵转置
Tensor.t()
仅对 2 维张量有效, 0 维和 1 维张量原样返回 t torch.tensor([[1, 2, 3]])t
tensor([[1, 2, 3]])
# 或者 t.Tt.t()
tensor([[1],[2],[3]])交换两个维度
Tensor.transpose(dim0, dim1)
交换 Tensor 的 dim0 与 dim1 维度, Tensor.t() 就相当于 Tensor.transpose(0, 1) 调整维度顺序
Tensor.permute(*dims)
指定 Tensor 维度的顺序, 将 HxWxC 顺序的图片张量转为 CxHxW 顺序, 可以调用: img.permute(2, 0, 1) 维度逆序
Tensor.T(deprecated)
相当于 Tensor.permute(n-1, n-2, …, 0), 在未来的版本 Tensor.T 仅对二维张量有效, 即矩阵转置
Tensor.mT
末尾两个维度表示矩阵, 前面的所有维度表示 mini-batch, 仅对末尾两个维度逆序, 其余维度的顺序保持不变 t torch.arange(6).view(1,2,3)t.shape
torch.Size([1, 2, 3])t.permute(2, 1, 0).shape
torch.Size([3, 2, 1])t.T.shape
torch.Size([3, 2, 1])插入或移除维度 插入大小为 1 的维度 Tensor.unsqueeze(dim) 删除大小为 1 的维度 Tensor.squeeze(dimNone) dim 为 None 时删除所有的 1-dim
dim 参数指定插入或删除的索引位 t torch.arange(4).view(2, -1)t.shape
torch.Size([2, 2])# 在开头插入 1-dimt_2 t.unsqueeze(0)t_2.shape
torch.Size([1, 2, 2])# 在末尾插入 1-dimt_3 t_2.unsqueeze(-1)t_3.shape
torch.Size([1, 2, 2, 1])# squeeze 默认移除所有的 1-dimt_3.squeeze().shape
torch.Size([2, 2])维度数据重复
Tensor.expand(*sizes)
*sizes 参数指定扩张后各个维度的大小, -1 表示表示该维度不扩张, 仅能对 1-dim 进行数据扩张
该方法不会额外分配空间, 与原张量共享底层数据, 可以在左侧新增维度 t torch.tensor([1, 2]).view(2, 1)t
tensor([[1],[2]])t.shape
torch.Size([2, 1])# 将维度 (2, 1) 扩张为 (2, 3),
# 原维度 (2, 1) 中的 2 保持不变, 1 扩为 2e t.expand(-1, 2)e
tensor([[1, 1, 1],[2, 2, 2]])e.shape
torch.Size([2, 3])# 修改原张量会影响到扩张后的张量t[0, 0] 0e
tensor([[0, 0, 0],[2, 2, 2]])Tensor.repeat(*sizes)
*sizes 参数指定各个维度的重复次数, 重复 1 次等同于维度保持不变
该方法复制底层数据, 与 Tensor.expand() 类似也可以在左侧新增维度 t torch.tensor([1, 2]).view(2, 1)t
tensor([[1],[2]])t.shape
torch.Size([2, 1])# # 原维度 (2, 1) 中的 2 和 1 均重复 2 次r t.repeat(2, 2)r.shape
torch.Size([4, 2])r
tensor([[1, 1],[2, 2],[1, 1],[2, 2]])拼接
torch.cat(tensors, dim0) 拼接多个张量, 除了 dim 参数指定的拼接维度, 其他维度必须一致 a torch.arange(3, 9).view(3, 2).t()a
tensor([[3, 5, 7],[4, 6, 8]])b torch.tensor([[1], [2]])b
tensor([[1],[2]])b.shape
torch.Size([2, 1])
# 维度(2, 3)与维度(2, 1)拼接
# 第 1 个维度相同(都是 2), 第 2 个维度不同(在此维度上拼接)torch.cat([b, a], dim1)
tensor([[1, 3, 5, 7],[2, 4, 6, 8]])叠加
torch.stack(tensors, dim0) 叠加多个张量, 所有张量的维度必须一致, dim 指定叠加后新增的维度 t torch.arange(4).view(2, 2)t
tensor([[0, 1],[2, 3]])
# 叠加后变成三维张量n torch.stack([t, t, t], dim2)n
tensor([[[0, 0, 0],[1, 1, 1]],[[2, 2, 2],[3, 3, 3]]])n.shape
torch.Size([2, 2, 3])与 torch.cat 保持原有的维度数量不同, torch.stack 会新增一个维度 分割
沿着指定的维度分割张量
Tensor.split(split_size, dim0)torch.split(tensor, split_size, dim0)
参数 split_size:
整数: 表示分割后每一块的大小(最后一块可能略小)列表: 具体指定每一个块的大小 t torch.arange(10).view(2, 5)t
tensor([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]])
# 按列分割, 每一个张量均为 2 列, 一共有 5 列, 最后一个张量不够分, 仅有 1 列t.split(2, dim1)
(tensor([[0, 1],[5, 6]]),tensor([[2, 3],[7, 8]]),tensor([[4],[9]]))
# 3 个张量分别为 1, 3, 1 列t.split([1, 3, 1], dim1)
(tensor([[0],[5]]),tensor([[1, 2, 3],[6, 7, 8]]),tensor([[4],[9]]))分割后的张量是原张量的视图 分块
chunk 与 split 功能基本相同, 不同之处在于: chunk 的参数指定了分块的数量, 而 split 的参数指定每一个分块的大小
Tensor.chunk(chunks, dim0)torch.chunk(input, chunks, dim0) t torch.arange(10).view(2, 5)t
tensor([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]])
# 将 5 列分为 3 块, 没法均分, 最后一块略小t.chunk(3, dim1)
(tensor([[0, 1],[5, 6]]),tensor([[2, 3],[7, 8]]),tensor([[4],[9]]))分块后的张量也是原张量的视图