深度学习Pytorch框架Tensor张量

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
大数据开发治理平台 DataWorks,不限时长
简介: 深度学习Pytorch框架Tensor张量

Tensor的裁剪运算

  • 对Tensor中的元素进行范围过滤
  • 常用于梯度裁剪(gradient clipping),即在发生梯度离散或者梯度爆炸时对梯度的处理
  • torch.clamp(input, min, max, out=None) → Tensor:将输入input张量每个元素的夹紧到区间 [min,max],并返回结果到一个新张量。

1698843602982.jpg

Tensor的索引与数据筛选

  • torch.where(codition,x,y):按照条件从x和y中选出满足条件的元素组成新的tensor,输入参数condition:条件限制,如果满足条件,则选择a,否则选择b作为输出。
  • torch.gather(input,dim,index,out=None):在指定维度上按照索引赋值输出tensor
  • torch.inex_select(input,dim,index,out=None):按照指定索引赋值输出tensor
  • torch.masked_select(input,mask,out=None):按照mask输出tensor,输出为向量
  • torch.take(input,indices):将输入看成1D-tensor,按照索引得到输出tensor
  • torch.nonzero(input,out=None):输出非0元素的坐标
import torch
#torch.where
a = torch.rand(4, 4)
b = torch.rand(4, 4)
print(a)
print(b)
out = torch.where(a > 0.5, a, b)
print(out)

1698843618523.jpg

print("torch.index_select")
a = torch.rand(4, 4)
print(a)
out = torch.index_select(a, dim=0,
                   index=torch.tensor([0, 3, 2]))
#dim=0按列,index取的是行
print(out, out.shape)

1698843638842.jpg

print("torch.gather")
a = torch.linspace(1, 16, 16).view(4, 4)
print(a)
out = torch.gather(a, dim=0,
             index=torch.tensor([[0, 1, 1, 1],
                                 [0, 1, 2, 2],
                                 [0, 1, 3, 3]]))
print(out)
print(out.shape)
#注:从0开始,第0列的第0个,第一列的第1个,第二列的第1个,第三列的第1个,,,以此类推
#dim=0, out[i, j, k] = input[index[i, j, k], j, k]
#dim=1, out[i, j, k] = input[i, index[i, j, k], k]
#dim=2, out[i, j, k] = input[i, j, index[i, j, k]]

1698843649316.jpg

print("torch.masked_index")
a = torch.linspace(1, 16, 16).view(4, 4)
mask = torch.gt(a, 8)
print(a)
print(mask)
out = torch.masked_select(a, mask)
print(out)

1698843658810.jpg

print("torch.take")
a = torch.linspace(1, 16, 16).view(4, 4)
b = torch.take(a, index=torch.tensor([0, 15, 13, 10]))
print(b)

1698843681936.jpg

#torch.nonzero
print("torch.take")
a = torch.tensor([[0, 1, 2, 0], [2, 3, 0, 1]])
out = torch.nonzero(a)
print(out)
#稀疏表示

1698843691937.jpg


Tensor的组合/拼接

  • torch.cat(seq,dim=0,out=None):按照已经存在的维度进行拼接
  • torch.stack(seq,dim=0,out=None):沿着一个新维度对输入张量序列进行连接。 序列中所有的张量都应该为相同形状。
print("torch.stack")
a = torch.linspace(1, 6, 6).view(2, 3)
b = torch.linspace(7, 12, 6).view(2, 3)
print(a, b)
out = torch.stack((a, b), dim=2)
print(out)
print(out.shape)
print(out[:, :, 0])
print(out[:, :, 1])

1698843702443.jpg

Tensor的切片

  • torch.chunk(tensor,chunks,dim=0):按照某个维度平均分块(最后一个可能小于平均值)
  • torch.split(tensor,split_size_or_sections,dim=0):按照某个维度依照第二个参数给出的list或者int进行分割tensor

Tensor的变形操作

  • torch().reshape(input,shape)
  • torch().t(input):只针对2D tensor转置
  • torch().transpose(input,dim0,dim1):交换两个维度
  • torch().squeeze(input,dim=None,out=None):去除那些维度大小为1的维度
  • torch().unbind(tensor,dim=0):去除某个维度
  • torch().unsqueeze(input,dim,out=None):在指定位置添加维度,dim=-1在最后添加
  • torch().flip(input,dims):按照给定维度翻转张量
  • torch().rot90(input,k,dims):按照指定维度和旋转次数进行张量旋转
import torch
a = torch.rand(2, 3)
print(a)
out = torch.reshape(a, (3, 2))
print(out)

1698843714315.jpg


print(a)
print(torch.flip(a, dims=[2, 1]))
print(a)
print(a.shape)
out = torch.rot90(a, -1, dims=[0, 2]) #顺时针旋转90°  
print(out)
print(out.shape)

1698843726119.jpg

Tensor的填充操作

  • torch.full((2,3),3.14)

Tensor的频谱操作(傅里叶变换)

1698843736021.jpg

相关文章
|
10天前
|
机器学习/深度学习 PyTorch 测试技术
PyTorch 深度学习(GPT 重译)(四)(1)
PyTorch 深度学习(GPT 重译)(四)
29 2
|
10天前
|
机器学习/深度学习 PyTorch API
PyTorch 深度学习(GPT 重译)(三)(4)
PyTorch 深度学习(GPT 重译)(三)
40 3
|
10天前
|
机器学习/深度学习 存储 PyTorch
PyTorch 深度学习(GPT 重译)(三)(3)
PyTorch 深度学习(GPT 重译)(三)
28 2
|
10天前
|
机器学习/深度学习 PyTorch 算法框架/工具
PyTorch 深度学习(GPT 重译)(二)(3)
PyTorch 深度学习(GPT 重译)(二)
28 1
|
10天前
|
机器学习/深度学习 算法 PyTorch
PyTorch 深度学习(GPT 重译)(二)(2)
PyTorch 深度学习(GPT 重译)(二)
35 2
|
10天前
|
机器学习/深度学习 PyTorch 算法框架/工具
PyTorch 深度学习(GPT 重译)(二)(1)
PyTorch 深度学习(GPT 重译)(二)
29 2
|
10天前
|
存储 PyTorch 算法框架/工具
PyTorch 深度学习(GPT 重译)(一)(3)
PyTorch 深度学习(GPT 重译)(一)
31 2
|
10天前
|
机器学习/深度学习 存储 PyTorch
PyTorch深度学习基础:张量(Tensor)详解
【4月更文挑战第17天】本文详细介绍了PyTorch中的张量,它是构建和操作深度学习数据的核心。张量是多维数组,用于存储和变换数据。PyTorch支持CPU和GPU张量,后者能加速大规模数据处理。创建张量可通过`torch.zeros()`、`torch.rand()`或直接从Python列表转换。张量操作包括数学运算、切片和拼接。在深度学习中,张量用于神经网络模型的构建和训练。理解张量对于掌握PyTorch至关重要。
|
2月前
|
机器学习/深度学习 编解码 PyTorch
Pytorch实现手写数字识别 | MNIST数据集(CNN卷积神经网络)
Pytorch实现手写数字识别 | MNIST数据集(CNN卷积神经网络)
|
1月前
|
机器学习/深度学习 算法 PyTorch
【PyTorch实战演练】深入剖析MTCNN(多任务级联卷积神经网络)并使用30行代码实现人脸识别
【PyTorch实战演练】深入剖析MTCNN(多任务级联卷积神经网络)并使用30行代码实现人脸识别
61 2

相关实验场景

更多