当前位置: 首页 > news >正文

pytorch常用API

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

import torch
import numpy as npa = [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) # Flaseb = torch.from_numpy(a) #此时a和b共享内存

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

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、

torch.manual_seed(28) # 设置随机数种子torch.normal(0., 1.0, (2, 3)) # 产生一个[2,3]大小的tensor,tensor中值满足均值为0,标准差为1

2、

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

3、

print(np.random.permutation(10))
print(torch.randperm(10))

四、索引相关API

1、

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、

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

3、

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

4、

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

4、

x = torch.rand(2, 3)
print(x)
print(torch.transpose(x, dim0=1, dim1=0))

5、

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、

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、

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、

x = torch.randn(2,3)
print(x)
print(torch.abs(x))
print(x.abs())

2、

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、

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、

x = torch.randn(3,4) * 2
print(x)
print(torch.sigmoid(x))
import torch.nn.functional as F
print(F.sigmoid(x))

http://www.dtcms.com/a/271524.html

相关文章:

  • [特殊字符] 突破小样本瓶颈:DataDream——用Stable Diffusion生成高质量分类数据集
  • 认证鉴权技术解析:COOKIE | SESSION | TOKEN | JWT | SSO
  • `fatal: bad config value for ‘color.ui‘`错误解决方案
  • 前端UI逻辑复杂可以用什么设计模式
  • 卫星通信终端天线的5种对星模式之二:功率检测型载波跟踪
  • 在Excel用公式计算周次
  • NumPy-梯度与导数计算详解
  • 用一个代码案例详解介绍vmalloc函数的功能和作用
  • 权限分级看板管理:实时数据驱动决策的关键安全基石
  • 奇异值分解(singular value decomposition,SVD)
  • 笔试——Day2
  • 单细胞入门(2)-经典案例分析
  • EPLAN 电气制图(六):结构盒与设备管理器核心概念(基础知识选看)
  • 脑电分析入门指南:信号处理、特征提取与机器学习
  • python 在运行时没有加载修改后的版本
  • windows server2019安全修复
  • 数据结构——深度优先搜索与广度优先搜索的实现
  • STM32-待机唤醒实验
  • 学习笔记(30):matplotlib绘制简单图表-绘制正弦波
  • Python的标准库之时间库(小白五分钟从入门到精通)
  • 【Netty+WebSocket详解】WebSocket全双工通信与Netty的高效结合与实战
  • 循环神经网络详解
  • cherryStudio electron因为环境问题无法安装解决方法或打包失败解决方法
  • NLP自然语言处理04 transformer架构模拟实现
  • Git版本控制完全指南:从入门到实战(简单版)
  • 【02】MFC入门到精通——MFC 手动添加创建新的对话框模板
  • 【PyTorch】PyTorch中torch.nn模块的全连接层
  • C++每日刷题 day2025.7.09
  • 备受期待的 MMORPG 游戏《侍魂R》移动端现已上线 Sui
  • RK3588 buildroot 解决软件包无法下载