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

输出:

相关推荐
大龄程序员狗哥3 小时前
第47篇:使用Speech-to-Text API快速构建语音应用(操作教程)
人工智能
KKKlucifer3 小时前
数据安全合规自动化:策略落地、审计追溯与风险闭环技术解析
人工智能·安全
m0_748554814 小时前
golang如何实现用户订阅偏好管理_golang用户订阅偏好管理实现总结
jvm·数据库·python
RWKV元始智能4 小时前
RWKV超并发项目教程,RWKV-LM训练提速40%
人工智能·rnn·深度学习·自然语言处理·开源
dyj0954 小时前
Dify - (一)、本地部署Dify+聊天助手/Agent
人工智能·docker·容器
墨染天姬4 小时前
【AI】Hermes的GEPA算法
人工智能·算法
小超同学你好4 小时前
OpenClaw 深度解析系列 · 第8篇:Learning & Adaptation(学习与自适应)
人工智能·语言模型·chatgpt
紫微AI4 小时前
前端文本测量成了卡死一切创新的最后瓶颈,pretext实现突破了
前端·人工智能·typescript
码途漫谈4 小时前
Easy-Vibe开发篇阅读笔记(四)——前端开发之结合 Agent Skills 美化界面
人工智能·笔记·ai·开源·ai编程
smj2302_796826524 小时前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode