当前位置:网站首页>Pytorch/TensorFlow/Numpy常用函数汇总
Pytorch/TensorFlow/Numpy常用函数汇总
2022-08-11 01:44:00 【NorthSmile】
常用函数
一、Pytorch
1.枚举:enumerate
基本语法:enumerate(iterable,start=0
)
返回结果:enumerate is useful for obtaining an indexed list:
(0, seq[0]), (1, seq[1]), (2, seq[2]), …
2.维度交换:permute
基本语法:tensor.permute(arr,dims)
举例:
arr=torch.randint(10,size=(2,3,4))
print(arr)
arr=torch.permute(arr,dims=(0,2,1))
print(arr.size())
print(arr)
tensor([[[7, 1, 7, 9],
[6, 4, 4, 2],
[9, 0, 7, 7]],
[[0, 5, 3, 1],
[3, 0, 6, 0],
[1, 3, 4, 5]]])
torch.Size([2, 4, 3])
tensor([[[7, 6, 9],
[1, 4, 0],
[7, 4, 7],
[9, 2, 7]],
[[0, 3, 1],
[5, 0, 3],
[3, 6, 4],
[1, 0, 5]]])
3.尺寸形状:size/shape
基本语法:
(1)tensor.size()
(2)tensor.shape
举例:
arr=torch.ones(size=(2,2,3,3))
print(arr.size()) # torch.Size([2, 2, 3, 3])
print(arr.size(0)) # 2
print(arr.shape) # torch.Size([2, 2, 3, 3])
4.数据填充:full/fill_
1)创建指定形状的张量,并使用特定值填充该张量
基本语法:torch.full(size, fill_value)
举例:
arr=torch.full(size=(2,3),fill_value=2.0)
生成用2.0填充,形状为2*3的Tensor:
tensor([[2., 2., 2.],
[2., 2., 2.]])
2)使用特定值填充指定向量
基本语法:tensor.fill_(value)
举例:
arr=torch.full(size=(2,3),fill_value=2.0)
print(arr)
print('--------------')
arr.fill_(value=3.0)
print(arr)
tensor([[2., 2., 2.],
[2., 2., 2.]])
--------------
tensor([[3., 3., 3.],
[3., 3., 3.]])
5.阻断反向梯度传播:detach
基本语法:tensor.detach()
举例:
arr=torch.full(size=(2,3),fill_value=2.0,requires_grad=True)
print(arr.requires_grad)
arr2=arr.detach()
print(arr2.requires_grad)
True
False
6.形状调整:view
基本语法:tensor.view(shape)
举例:
value=torch.arange(6)
print(value)
arr=value.view((-1,2))
print(arr)
tensor([0, 1, 2, 3, 4, 5])
tensor([[0, 1],
[2, 3],
[4, 5]])
7.维度扩张/减少:unsqueeze/squeeze
基本语法:
(1)torch.unsqueeze(tensor,dim)
(2)torch.squeeze(tensor,dim)
举例:
arr=torch.ones(size=(5,6,3))
print(arr.size())
arr=torch.unsqueeze(arr,dim=0)
print(arr.size())
arr=torch.squeeze(arr,dim=0)
print(arr.size())
torch.Size([5, 6, 3])
torch.Size([1, 5, 6, 3])
torch.Size([5, 6, 3])
二、Numpy
1.维度交换:transpose
基本语法:arr.transpose(axis)
举例:
arr=np.ones(shape=(2,3,4))
print(arr.transpose(2,1,0).shape) # 其中数字对应坐标轴,如2表示4、1表示3、0表示2
(4, 3, 2)
边栏推荐
- Summarize the acquisition of commonly used file information QFileInfo in Qt: suffix, name, path, link
- Still using Xshell?You are out, recommend a more modern terminal connection tool, easy to use!
- uni-app实现app和H5调用手机拨号功能
- 软件测试面试题:Web服务器指标指标?
- 迭代器和生成器
- 基于 HPSO 与多核 LSSVM 的网络入侵检测
- Flink二阶段提交
- 【websocket】
- 连流量染色都没有,你说要搞微服务?
- Successfully resolved raise TypeError('Unexpected feature_names type')TypeError: Unexpected feature_names type
猜你喜欢
随机推荐
Vim take on a window.
88Q2110 通过C22方式访问C45 phy地址
如何实现FPGA的可重复性设计
Deep Learning [Chapter 2]
22、库存服务
apache+PHP+MySQL+word press, page error when installing word press?
基于 HPSO 与多核 LSSVM 的网络入侵检测
软件测试面试题:单元测试的策略有哪些?
MySQL advanced query
88Q2110 access C45 phy address through C22
英伟达 GPU 架构简史
【websocket】
如何开展性能测试,你知道吗?
C# WebBrower1控件可编辑模式保存时会提示“该文档已被修改,是否保存修改结果”
nvidia-smi详解
备战“金九银十”,软件测试功能 / 数据库 /linux/ 接口 / 自动化 / 测试开发面试真题解析
Tomca启动闪退问题如何解决
想进阿里?先来搞懂一下分布式事务
This Thursday evening at 19:00, Lesson 5 of the sixth phase of knowledge empowerment丨OpenHarmony WiFi subsystem
两日总结九