张量操作

一、张量的拼接

  1. torch.cat()

    功能:将张量按维度dim进行拼接,且[不会扩张张量的维度]
  • tensors:张量序列
  • dim:要拼接的维度
torch.cat(tensors,
dim=0,
out=None)
flag = True
# flag = False
if flag:
t1 = torch.full((4, 4), 10)
t2 = torch.full((4, 4), 5)
print(t1)
print(t2)
t3 = torch.cat([t1, t2], dim=0,out=None)
print(t3)
print("t1.shape:{}\nt2.shape:{}\nt3.shape:{}".format(t1.shape, t2.shape, t3.shape))
tensor([[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10]])
tensor([[5, 5, 5, 5],
[5, 5, 5, 5],
[5, 5, 5, 5],
[5, 5, 5, 5]])
tensor([[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[ 5, 5, 5, 5],
[ 5, 5, 5, 5],
[ 5, 5, 5, 5],
[ 5, 5, 5, 5]])
t1.shape:torch.Size([4, 4])
t2.shape:torch.Size([4, 4])
t3.shape:torch.Size([8, 4])
  1. torch.stack()

    功能:在新创建的维度dim上进行拼接[会扩张张量的维度]
  • tensors:张量序列
  • dim:要拼接的维度
torch.stack(
tensors,
dim=0,
out=None)
flag = True
# flag = False
if flag:
t1 = torch.full((2, 4), 10)
t2 = torch.full((2, 4), 5)
print(t1)
print(t2)
t3 = torch.stack([t1, t1, t2], dim=1,out=None)
print(t3)
print("t1.shape:{}\nt2.shape:{}\nt3.shape:{}".format(t1.shape, t2.shape, t3.shape))
tensor([[10, 10, 10, 10],
[10, 10, 10, 10]])
tensor([[5, 5, 5, 5],
[5, 5, 5, 5]])
tensor([[[10, 10, 10, 10],
[10, 10, 10, 10],
[ 5, 5, 5, 5]], [[10, 10, 10, 10],
[10, 10, 10, 10],
[ 5, 5, 5, 5]]])
t1.shape:torch.Size([2, 4])
t2.shape:torch.Size([2, 4])
t3.shape:torch.Size([2, 3, 4])

二、张量的切分

  1. torch.chunk(input,chunks,dim=0)

    将tensor按照dim进行平均切分返回张量列表,若不能整除,最后一份tensor小于其他张量
  • input:需要切分的张量
  • chunks:要切分的份数
  • dim:要切分的维度
flag = True
# flag = False
if flag:
t1 = torch.full((6, 5), 10)
t3 = torch.chunk(t1, chunks=3, dim=0)
for num, t in enumerate(t3):
print(num, t, t.shape)
0 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
1 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
2 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
  1. torch.split(tensor,split_size_or_sections,dim)

    将张量按照dim进行切分,返回张量列表

    split_size_or_sections:为int时表示每一份的长度,为list时表示按list元素切分
flag = True
# flag = False
if flag:
t1 = torch.full((6, 5), 10)
t3 = torch.split(t1, split_size_or_sections=2, dim=0)
t4 = torch.split(t1, [2, 1, 1, 1, 1], dim=0)
for num, t in enumerate(t4):
print(num, t, t.shape)
for num, t in enumerate(t3):
print(num, t, t.shape)
0 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
1 tensor([[10, 10, 10, 10, 10]]) torch.Size([1, 5])
2 tensor([[10, 10, 10, 10, 10]]) torch.Size([1, 5])
3 tensor([[10, 10, 10, 10, 10]]) torch.Size([1, 5])
4 tensor([[10, 10, 10, 10, 10]]) torch.Size([1, 5])
0 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
1 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
2 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])

三、tensor索引

  1. torch.index_select(input,dim,index,out=None)

    按照index索引数据返回依索引数据拼接的张量
flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 5))
t_list = [0, 1]
t2 = torch.tensor(t_list,dtype=torch.long)
t4 = torch.index_select(t1, dim=0, index=t2, out=None)
print(t1)
print(t4)
tensor([[5, 6, 1, 6, 8],
[2, 3, 6, 9, 1],
[3, 4, 2, 9, 5],
[1, 4, 7, 3, 8],
[7, 7, 9, 8, 7],
[1, 8, 9, 9, 5]])
tensor([[5, 6, 1, 6, 8],
[2, 3, 6, 9, 1]])
  1. torch.masked_select(input,mask,out=None)

    按mask中的True进行索引,并返回一维张量,其中mask代表与input同形状的布尔类型张量

    eq,ne,gt,ge,lt,le
flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 5))
t1_mask = t1.ge(5)
t4 = torch.masked_select(t1,t1_mask)
print(t1)
print(t1_mask)
print(t4)
tensor([[5, 6, 1, 6, 8],
[2, 3, 6, 9, 1],
[3, 4, 2, 9, 5],
[1, 4, 7, 3, 8],
[7, 7, 9, 8, 7],
[1, 8, 9, 9, 5]])
tensor([[ True, True, False, True, True],
[False, False, True, True, False],
[False, False, False, True, True],
[False, False, True, False, True],
[ True, True, True, True, True],
[False, True, True, True, True]])
tensor([5, 6, 6, 8, 6, 9, 9, 5, 7, 8, 7, 7, 9, 8, 7, 8, 9, 9, 5])

四、张量变换

3.1 torch.reshape(input,shape)

变换张量形状,当张量在内存中是连续时,新张量与input共享数据内存

flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 5))
t2 = torch.reshape(t1, (-1,3,5))
t1[0,0] = 33
print(t1, id(t1.data))
print(t2, id(t2.data))
tensor([[33,  6,  1,  6,  8],
[ 2, 3, 6, 9, 1],
[ 3, 4, 2, 9, 5],
[ 1, 4, 7, 3, 8],
[ 7, 7, 9, 8, 7],
[ 1, 8, 9, 9, 5]]) 1778886266112
tensor([[[33, 6, 1, 6, 8],
[ 2, 3, 6, 9, 1],
[ 3, 4, 2, 9, 5]], [[ 1, 4, 7, 3, 8],
[ 7, 7, 9, 8, 7],
[ 1, 8, 9, 9, 5]]]) 1778886266112

3.2 torch.transpose(input,dim0,dim1)

交换两个维度,即转置.

在图像预处理部分会用到,比如我们的输入图像是 c*h*w,维数乘高乘宽,可以使用两次变换得到h*w*c

flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(2, 6, 3)) t2 = torch.transpose(t1,1,2)
print(t1,t1.shape)
print(t2,t2.shape)
tensor([[[5, 6, 1],
[6, 8, 2],
[3, 6, 9],
[1, 3, 4],
[2, 9, 5],
[1, 4, 7]], [[3, 8, 7],
[7, 9, 8],
[7, 1, 8],
[9, 9, 5],
[6, 3, 7],
[7, 8, 7]]]) torch.Size([2, 6, 3])
tensor([[[5, 6, 3, 1, 2, 1],
[6, 8, 6, 3, 9, 4],
[1, 2, 9, 4, 5, 7]], [[3, 7, 7, 9, 6, 7],
[8, 9, 1, 9, 3, 8],
[7, 8, 8, 5, 7, 7]]]) torch.Size([2, 3, 6])

3.3 torch.t(input)

二维张量转置,相当于对二维矩阵作torch.transpose(input,0,1)

flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 3))
t2 = torch.t(t1)
print(t1,t1.shape)
print(t2,t2.shape)
tensor([[5, 6, 1],
[6, 8, 2],
[3, 6, 9],
[1, 3, 4],
[2, 9, 5],
[1, 4, 7]]) torch.Size([6, 3])
tensor([[5, 6, 3, 1, 2, 1],
[6, 8, 6, 3, 9, 4],
[1, 2, 9, 4, 5, 7]]) torch.Size([3, 6])

3.4 torch.squeeze(input,dim=None,out=None)

压缩长度为1的维度(轴),若指定维度,当且仅当该轴长度为1时,可以被移除

flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 3, 1))
t2 = torch.squeeze(t1)
print(t1,t1.shape)
print(t2,t2.shape)
tensor([[[5],
[6],
[1]], [[6],
[8],
[2]], [[3],
[6],
[9]], [[1],
[3],
[4]], [[2],
[9],
[5]], [[1],
[4],
[7]]]) torch.Size([6, 3, 1])
tensor([[5, 6, 1],
[6, 8, 2],
[3, 6, 9],
[1, 3, 4],
[2, 9, 5],
[1, 4, 7]]) torch.Size([6, 3])

3.5 torch.usqueeze(input,dim,out=None)

依据dim扩展维度

flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 3))
t2 = torch.unsqueeze(t1,dim=2)
print(t1,t1.shape)
print(t2,t2.shape)
tensor([[5, 6, 1],
[6, 8, 2],
[3, 6, 9],
[1, 3, 4],
[2, 9, 5],
[1, 4, 7]]) torch.Size([6, 3])
tensor([[[5],
[6],
[1]], [[6],
[8],
[2]], [[3],
[6],
[9]], [[1],
[3],
[4]], [[2],
[9],
[5]], [[1],
[4],
[7]]]) torch.Size([6, 3, 1])

张量的数学运算

张量的加减乘除,对数指数幂函数,三角函数

  1. torch.add(input,alpha=1,outher,out=None)

    实现逐元素计算input+alpha*other,input是第一个张量,alpha是乘项因子,other是第二个张量。

    torch.addcdiv()
\[out_i = input_i+value \times \frac{tensor1_i}{tensor2_i}
\]

torch.addcmul(input,value=1,tensor1,tensor2,out=None)

\[out_i = input_i + value \times tensor1_i \times tensor2_i
\]
flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 3))
t2 = torch.ones_like(t1)
t3 = torch.add(t1,t2,alpha=2)
print(t1,t1.shape)
print(t2,t2.shape)
print(t3,t3.shape)
tensor([[5, 6, 1],
[6, 8, 2],
[3, 6, 9],
[1, 3, 4],
[2, 9, 5],
[1, 4, 7]]) torch.Size([6, 3])
tensor([[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]) torch.Size([6, 3])
tensor([[ 7, 8, 3],
[ 8, 10, 4],
[ 5, 8, 11],
[ 3, 5, 6],
[ 4, 11, 7],
[ 3, 6, 9]]) torch.Size([6, 3])

线性回归

线性回归是分析一个变量与另外一个变量之间关系的方法

因变量:y 自变量:x 关系:线性

y = wx+b

分析:求解w,b

求解步骤:

  1. 确定模型,Model:y = wx+b
  2. 选择损失函数,MSE:
\[\frac{1}{m}\sum^{m}_{i=1}(y_i-\hat{y_i})
\]
  1. 求解梯度并更新w,b

    w = w - LR* w.grad

    b = b -LR* b.grad

pytorch(03)tensor的操作的更多相关文章

  1. Pytorch Tensor 常用操作

    https://pytorch.org/docs/stable/tensors.html dtype: tessor的数据类型,总共有8种数据类型,其中默认的类型是torch.FloatTensor, ...

  2. 对pytorch中Tensor的剖析

    不是python层面Tensor的剖析,是C层面的剖析. 看pytorch下lib库中的TH好一阵子了,TH也是torch7下面的一个重要的库. 可以在torch的github上看到相关文档.看了半天 ...

  3. pytorch之Tensor

    #tensor和numpy import torch import numpy as np numpy_tensor = np.random.randn(3,4) print(numpy_tensor ...

  4. Tensor索引操作

    #Tensor索引操作 ''''' Tensor支持与numpy.ndarray类似的索引操作,语法上也类似 如无特殊说明,索引出来的结果与原tensor共享内存,即修改一个,另一个会跟着修改 ''' ...

  5. pytorch中tensor数据和numpy数据转换中注意的一个问题

    转载自:(pytorch中tensor数据和numpy数据转换中注意的一个问题)[https://blog.csdn.net/nihate/article/details/82791277] 在pyt ...

  6. [Pytorch]Pytorch中tensor常用语法

    原文地址:https://zhuanlan.zhihu.com/p/31494491 上次我总结了在PyTorch中建立随机数Tensor的多种方法的区别. 这次我把常用的Tensor的数学运算总结到 ...

  7. PyTorch中的CUDA操作

      CUDA(Compute Unified Device Architecture)是NVIDIA推出的异构计算平台,PyTorch中有专门的模块torch.cuda来设置和运行CUDA相关操作.本 ...

  8. [Pytorch]Pytorch的tensor变量类型转换

    原文:https://blog.csdn.net/hustchenze/article/details/79154139 Pytorch的数据类型为各式各样的Tensor,Tensor可以理解为高维矩 ...

  9. pytorch中Math operation操作:torch.ger()

    torch.ger(vec1, vec2, out=None) → Tensor Outer product of vec1 and vec2. If vec1 is a vector of size ...

随机推荐

  1. Python3.7.9+Locust1.4.3版本性能测试工具案例分享

    一.Locust工具介绍 1.概述 Locust是一款易于使用的分布式负载测试工具,完全基于事件,使用python开发,即一个locust节点也可以在一个进程中支持数千并发用户,不使用回调,通过gev ...

  2. 《软件建模与分析》——UML基本概念

    UML-基本概念 UML本质上是一种语言,语言的学习离不开基本的单词(元素)和语法(视图.模型)的学习,今天我们就从它们开始. 元素 类图中的关系 控制权限 继承 实现 依赖:一个类A使用到了另一个类 ...

  3. [Angular] 删除旧版本,升级安装最新版本

    目录 删除旧版本 清除未卸载干净的angular-cli缓存 对于Linux 对于Windows 安装最新版本 查看安装版本 创建新项目 删除旧版本 npm uninstall -g angular- ...

  4. 牛客网多校第4场 J Hash Function 【思维+并查集建边】

    题目链接:戳这里 学习博客:戳这里 题意: 有n个空位,给一个数x,如果x%n位数空的,就把x放上去,如果不是空的,就看(x+1)%n是不是空的. 现在给一个已经放过数的状态,求放数字的顺序.(要求字 ...

  5. 康托展开:对全排列的HASH和还原,判断搜索中的某个排列是否出现过

    题目:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2297 前置技能:(千万注意是 ...

  6. Springboot如何启用文件上传功能

    网上的文章在写 "springboot文件上传" 时,都让你加上模版引擎,我只想说,我用不上,加模版引擎,你是觉得我脑子坏了,还是觉得我拿不动刀了. springboot如何启用文 ...

  7. Linux 驱动框架---i2c驱动框架

    i2c驱动在Linux通过一个周的学习后发现i2c总线的驱动框架还是和Linux整体的驱动框架是相同的,思想并不特殊比较复杂的内容如i2c核心的内容都是内核驱动框架实现完成的,今天我们暂时只分析驱动开 ...

  8. CDD All In One

    CDD All In One 组件驱动开发 (CDD) refs https://www.componentdriven.org/ https://www.learnstorybook.com/int ...

  9. SSH Keys vs GPG Keys

    SSH Keys vs GPG Keys SSH Keys SSH keys allow you to establish a secure connection between your compu ...

  10. CSS will-change All In One

    CSS will-change All In One CSS animation effect live demo https://nextjs.org/conf/ https://nextjs.or ...