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())

输出:

相关推荐
深圳UMI7 分钟前
AI笔记在学习与工作中的高效运用
大数据·人工智能
大模型真好玩17 分钟前
深入浅出LangGraph AI Agent智能体开发教程(八)—LangGraph底层API实现ReACT智能体
人工智能·agent·deepseek
IT_陈寒32 分钟前
告别低效!用这5个Python技巧让你的数据处理速度提升300% 🚀
前端·人工智能·后端
北京耐用通信1 小时前
神秘魔法?耐达讯自动化Modbus TCP 转 Profibus 如何为光伏逆变器编织通信“天网”
网络·人工智能·网络协议·网络安全·自动化·信息与通信
居7然1 小时前
如何高效微调大模型?LLama-Factory一站式解决方案全解析
人工智能·大模型·llama·大模型训练·vllm
FullmetalCoder1 小时前
一文搞懂智能体
人工智能
zzywxc7871 小时前
AI 行业应用:AI 在金融、医疗、教育、制造业等领域的落地案例
人工智能·spring·金融·prompt·语音识别·xcode
六月的可乐1 小时前
Vue接入AI聊天助手实战
前端·vue.js·人工智能
赴3351 小时前
dlib库关键点定位和疲劳检测
人工智能·opencv·计算机视觉·关键点·疲劳检测·dlib
唐叔在学习2 小时前
pip安装太慢?一键切换国内镜像源,速度飞起!
后端·python