pytorch常用API

一、torch.tensor()和torch.from_numpy()

python 复制代码
import torch
import numpy as np


a = [1, 2, 3.0]
a = np.asarray([1, 2, 3.0])
b = torch.tensor(a) # 此时a和b不共享内存

print(b) # tensor([1., 2., 3.], dtype=torch.float64)
print(b.dtype) # torch.float64
print(b.device) # CPU
print(b.requires_grad) # Flase


b = torch.from_numpy(a) #此时a和b共享内存

二、将tensor对象转换为numpy对象

python 复制代码
a = [1, 2, 3, 4.0]
b = torch.tensor(a)
print(b) # tensor([1., 2., 3., 4.])
print(b.device, b.dtype, b.requires_grad, b.shape) # cpu torch.float32 False torch.Size([4])


# 将Tensor对象转换为Numpy对象 --> 要求tensor对象必须在cpu上
# cpu: 将tensor数据复制到cpu上,如果数据本身就在cpu上,就直接返回原始数据;否则copy一份数据到cpu
# detach: 梯度反向传播路径的截断,表示产生一个新节点,基于该新节点的后续所有操作,不往前传递梯度
# c = b.cpu().numpy()
c = b.cpu().detach().numpy()
print(c) # [1. 2. 3. 4.]
c[0] = 100
print(c) # [100.   2.   3.   4.]
print(b) # tensor([100.,   2.,   3.,   4.])
print(list(c)) # [100.0, 2.0, 3.0, 4.0]

三、随机数相关API

1、

python 复制代码
torch.manual_seed(28) # 设置随机数种子

torch.normal(0., 1.0, (2, 3)) # 产生一个[2,3]大小的tensor,tensor中值满足均值为0,标准差为1

2、

python 复制代码
torch.rand((2,3)) # 产生一个[2,3]大小的tensor,tensor中值满足[0,1)的均匀分布

3、

python 复制代码
print(np.random.permutation(10))
print(torch.randperm(10))

四、索引相关API

1、

python 复制代码
a = torch.rand((2,3)) # a是一个两维的矩阵 shape为[2,3]
print(a)

# 按照第0维进行数据的合并(行) --> [6,3]
b = torch.cat((a,a,a), dim=0)
print(b, "\n", b.shape)
# 按照第1维进行数据的合并(列) --> [2,9]
c = torch.cat((a,a,a), dim=1)
print(c, "\n", c.shape)

2、

python 复制代码
# 这个2表示期望输出的tensor数目最多是2个,每个的大小其实 [total/2] 向上取整
torch.chunk(c, 2, dim=1) # 这个2表示期望的输出结果是2个tensor

3、

python 复制代码
# 这个4表示期望每个输出的大小最多是4
torch.split(c, 4, dim=1) # 输出的tensor中,每个tensor在dim=1这个维度最多是两维

4、

python 复制代码
torch.split(c, (5,2,2), dim=1)  # 期望输出3个tensor,每个tensor在第1维上的大小分别为5 、2 、2

4、

python 复制代码
x = torch.rand(2, 3)
print(x)
print(torch.transpose(x, dim0=1, dim1=0))

5、

python 复制代码
x = torch.rand(2, 3, 4)
print(x, "\n", x.shape)

# 维度转置交换
y = torch.transpose(x, dim0=2, dim1=0)
print(y, "\n", y.shape)

6、

python 复制代码
a = torch.rand(5, 3, 4)
print(a.shape)

# 将原来的第1维作为现在的第0维、2->1、0->2
b = torch.permute(a, dims=(1, 2, 0))
print(b.shape)

7、

python 复制代码
a = torch.rand(2, 3)
b = torch.reshape(a, (3,2))
# view这个reshape操作,要求tensor对象在内存中必须是连续的
# contiguous: 将对象内存重新排序 --> 内存数据变成连续
c = b.view((1, 6))
#c = b.T.contiguous().view((1, 6))
# c = b.T.reshape((1, 6))
print(a)
print(b)
print(c)

五、Tensor数值运算相关API

1、

python 复制代码
x = torch.randn(2,3)
print(x)
print(torch.abs(x))
print(x.abs())

2、

python 复制代码
x = torch.rand(2,3)
y = torch.rand(3,2)
print(x)
print(y)

# 多维举证乘法,只需要满足最后两个维度是否满足矩阵乘法要求:[2,3] @ [3,2]=[2,2]
# 其他维度要求满足广播机制
c = torch.matmul(x, y)
print(c)

3、

python 复制代码
x = torch.rand(4, 1, 2, 3)
y = torch.rand(1, 3, 3, 2)
print(x.shape)
print(y.shape)
# 多维矩阵乘法:仅需要考虑最后两个维度是否满足矩阵乘法的要求 [m,k] * [k,n] -> [m,n]
# PS: 其它维度要求满足广播的机制
# [4,2,3]*[4,3,2] -> [4,2,2]
# [4,2,3]*[3,2] -> [4,2,2]
c = torch.matmul(x, y)
print(c.shape)

4、

python 复制代码
x = torch.randn(3,4) * 2
print(x)
print(torch.sigmoid(x))
import torch.nn.functional as F
print(F.sigmoid(x))
相关推荐
aigcapi17 小时前
RAG 系统的黑盒测试:从算法对齐视角解析 GEO 优化的技术指标体系
大数据·人工智能·算法
上进小菜猪18 小时前
基于深度学习的河道垃圾检测系统设计(YOLOv8)
人工智能
知远同学18 小时前
Anaconda的安装使用(为python管理虚拟环境)
开发语言·python
上天夭18 小时前
模型训练篇
人工智能·深度学习·机器学习
小徐Chao努力18 小时前
【Langchain4j-Java AI开发】09-Agent智能体工作流
java·开发语言·人工智能
Blossom.11818 小时前
AI编译器实战:从零手写算子融合与自动调度系统
人工智能·python·深度学习·机器学习·flask·transformer·tornado
Coder_Boy_18 小时前
SpringAI与LangChain4j的智能应用-(理论篇2)
人工智能·spring boot·langchain·springai
却道天凉_好个秋18 小时前
OpenCV(四十八):图像查找
人工智能·opencv·计算机视觉
Coder_Boy_18 小时前
SpringAI与LangChain4j的智能应用-(理论篇3)
java·人工智能·spring boot·langchain
GetcharZp19 小时前
工地“火眼金睛”!手把手带你用 YOLO11 实现安全帽佩戴检测
人工智能·计算机视觉