Pytorch中关于Tensor的操作

1.Tensor的创建

python 复制代码
import torch
import numpy as np
#使用list创建张量
a=torch.tensor([[1,2],[3,4]],dtype=torch.float64)
#使用array创建张量
b=torch.tensor(np.array([[1,2],[3,4]]),dtype=torch.uint8)
print(a)
print(b)

输出:

2.矩阵的叉乘和点乘

python 复制代码
import torch
import numpy as np
a=torch.tensor([[1,2],[3,4]])
b=torch.tensor([[1,2],[3,4]])
#逐元素相乘(点乘)
c=a*b
print(c)
#矩阵乘法(叉乘)
c=torch.mm(a,b)
print(c)

输出:

  1. 直接创建数据
python 复制代码
import torch
import numpy as np
#从range创建,规则与range相同
print(torch.arange(5))
print(torch.arange(1,7,2))
#linespace创建,前两个参数是起始与终止,最后一个是返回的个数(维度)
print(torch.linspace(0,5,10))
#创建全1矩阵,第一个是行,第二个是列
print(torch.ones(4,3))
#创建全0矩阵
print(torch.zeros(3,3))

输出:

4.一些特殊函数

python 复制代码
import torch
import numpy as np

a=torch.tensor([[1,2],[3,4]])
#分段函数,小的放大,大的放小
print(torch.clamp(a,min=2,max=3))


#化成整数
a=torch.tensor([1.01,2.5,-3.44,5.99])
print(torch.round(a))


#计算双曲正切
a=torch.Tensor([-3,-2,-1,-0.5,0,0.5,1,2,3])
print(torch.tanh(a))

输出:

5.随机创建

python 复制代码
import torch
import numpy as np

#从[0,1]中随机取
print(torch.rand(3,3))
#从正态分布中取
print(torch.randn(3,3))
#从指定范围内随机取(整数
print(torch.randint(0,9,[3,3]))

输出:

6.索引

python 复制代码
import torch
import numpy as np

a=torch.arange(9).view(3,3)
'''
[[0,1,2]
 [3,4,5]
 [6,7,8]]
'''
#直接索引
print(a[1,2])

#整数索引
row=[1,0]
col=[1,2]
print(a[row,col])

#布尔索引
index=a>3
print(index)
print(a[index])

输出:

7.切片

python 复制代码
import torch
import numpy as np

a=torch.arange(9).view(3,3)
'''
[[0,1,2]
 [3,4,5]
 [6,7,8]]
'''
#切片
print(a[1:,:2])
#带步长的切片(不支持负数)
print(a[::2])

输出:

8.搜索函数

python 复制代码
import torch
import numpy as np

#nonzero寻找非负元素(返回索引)
a=torch.randint(0,2,(3,3))
print(a)
print(torch.nonzero(a))

#where寻找条件
a=torch.randn(3,3)
b=torch.ones(3,3)
print(a)
#如果满足条件就返回a元素,否则返回b元素
print(torch.where(a>0,a,b))

输出:

9.Tensor的查询

python 复制代码
import torch
import numpy as np

a=torch.rand(1,2,3,4,5)
#元素个数=1*2*3*4*5=120
print(a.nelement())
#轴个数
print(a.ndimension())
#矩阵维度
print(a.size(),a.shape)

输出:

10.Tensor的变换

python 复制代码
import torch
import numpy as np

a=torch.rand(1,2,3,4,5)
#矩阵重塑
b=a.view(2*3,4*5)
print(b.shape)
c=a.reshape(-1)
print(c.shape)
d=a.reshape(2*3,-1)
print(d.shape)
print()
#维度增减
a=torch.rand(1,2,3,4,5)
b=torch.squeeze(a)
print(b.shape)
b=torch.unsqueeze(b,0)
print(b.shape)
print()
#矩阵转置(只接受二维tensor)
a=torch.tensor([[2]])
b=torch.tensor([[2,3]])
print(torch.transpose(a,1,0,))
print(torch.t(a))
print(torch.transpose(b,1,0,))
print(torch.t(b))
print()
#维度转换(调位置)
a=torch.rand((1,224,224,3))
print(a.shape)
b=a.permute(0,3,1,2)
print(b.shape)

输出:

11.拼接

python 复制代码
import torch
import numpy as np

a=torch.randn(2,3)
b=torch.randn(3,3)

#cat()
c=torch.cat((a,b))
d=torch.cat((b,b,b),dim=1)

print(c.shape)
print(d.shape)

#stack()
c=torch.stack((b,b),dim=1)
d=torch.stack((b,b),dim=0)
print(c.shape)
print(d.shape)

输出:

12.拆分

python 复制代码
import torch
import numpy as np

a=torch.randn(10,3)
for x in torch.split(a,[1,2,3,4],dim=0):
    print(x.shape)
print()
for x in torch.split(a,4,dim=0):
    print(x.shape)
print()
for x in torch.chunk(a,4,dim=0):
    print(x.shape)

输出:

13.Reduction

python 复制代码
import torch
import numpy as np

a=torch.tensor([[1,2],[3,4]])
#最大值
print(torch.max(a))

#横轴累加
print(torch.cumsum(a,dim=0))
#纵轴累乘
print(torch.cumprod(a,dim=1))

a=torch.Tensor([[1,2],[3,4]])
#均值
print(a.mean())
#中值
print(a.median())
#协方差
print(a.std())

输出:

相关推荐
AI Echoes3 分钟前
别再手工缝合API了!开源LLMOps神器LMForge,让你像搭积木一样玩转AI智能体!
人工智能·python·langchain·开源·agent
AI Echoes6 分钟前
从零构建企业级LLMOps平台:LMForge——支持多模型、可视化编排、知识库与安全审核的全栈解决方案
人工智能·python·langchain·开源·agent
Coovally AI模型快速验证6 分钟前
无人机小目标检测新SOTA:MASF-YOLO重磅开源,多模块协同助力精度飞跃
人工智能·yolo·目标检测·机器学习·计算机视觉·无人机
zskj_zhyl12 分钟前
七彩喜智慧养老:科技向善,让“养老”变“享老”的智慧之选
大数据·人工智能·科技·物联网·机器人
微盛企微增长小知识19 分钟前
企业微信AI怎么用才高效?3大功能+5个实操场景,实测效率提升50%
人工智能·企业微信
啦啦啦在冲冲冲26 分钟前
解释一下roberta,bert-chinese和bert-case有啥区别还有bert-large这些
人工智能·深度学习·bert
deepdata_cn27 分钟前
混合架构大型语言模型(Jamba)
人工智能·语言模型
居7然28 分钟前
从零开始学大模型之预训练语言模型
人工智能·语言模型·自然语言处理·大模型
2401_897930061 小时前
PyTorch 中训练语言模型过程
人工智能·pytorch·语言模型