当前位置:网站首页>Pytorch replaces some components in numpy. / / please indicate the source of the reprint
Pytorch replaces some components in numpy. / / please indicate the source of the reprint
2022-07-27 15:32:00 【Thinking and Practice】
numpy It can process two kinds of data at the same time :list and numpy.ndarray.
and torch Can only deal with tensors (Tensor) data .
1. Replace np.asarray()
import torch
data=[[1,2],[3,4]]
data = torch.Tensor(data)
# data = np.asarray(data)
print(data.shape)
2. Replace nn.prod()
# Use torch.prod() Replace nn.prod(), among torch.prod() The parameters inside must be tensor.
import torch
data=[1,2]
data = torch.Tensor(data)
print(data.shape)
c = torch.prod(data)
c
tensor(2.)
Python torch.prod Instance to explain - A course for farmers
3. Replace np.max()
# Use torch.max() Replace np.max(), among torch.max() The parameters inside must be tensor.
import torch
data=[1,2,3,9,4]
data = torch.Tensor(data)
a = torch.max(data)
a
tensor(9.)
4.torch.Size Convert to np.arrary
import torch
# data = np.asarray(data)
data=[[1,2],[3,4]]
data = torch.Tensor(data)
print(data.shape)
print(np.asarray(data.shape))
c = torch.prod(torch.Tensor(np.asarray(data.shape)))
c
torch.Size([2, 2])
[2 2]
tensor(4.)
5.numpy And torch Of ravel, They are used in the same way , Be careful tensor.ravel() The data after is still Tensor
import torch
# data = np.asarray(data)
data=[[1,2],[3,4]]
data = torch.Tensor(data)
data = data.ravel()
print("ravel:",data)
ravel: tensor([1., 2., 3., 4.])
6.numpy And torch Of ndim, They are used in the same way
import torch
data=[[1,2],[3,4]]
data_numpy = np.asarray(data).ravel()
data = torch.Tensor(data)
data = data.ravel()
print("ravel:",data)
print(data.shape)
print("tensor_ndim:",data.ndim)
print("data_numpy_ndim:",data_numpy.ndim)
ravel: tensor([1., 2., 3., 4.])
torch.Size([4])
tensor_ndim: 1
data_numpy_ndim: 1
7.numpy And torch.tensor Of data.T The effect is the same
import torch
data=[[1,2],[3,4]]
data_numpy = np.asarray(data)
data = torch.Tensor(data)
# data = data.ravel()
data_trans_tensor=data.T
print("data:",data)
print("data_trans:",data_trans_tensor)
data_trans_numpy=data_numpy.T
print("data_numpy:",data_numpy)
print("data_trans_numpy:",data_trans_numpy)
data: tensor([[1., 2.], [3., 4.]])
data_trans: tensor([[1., 3.], [2., 4.]])
data_numpy: [[1 2] [3 4]]
data_trans_numpy: [[1 3] [2 4]]
8.np.arange() And torch.arange() Use the same
timesteps=5
a = np.arange(timesteps)
b = torch.arange(timesteps)
print("a:",a)
print("b:",b)
a: [0 1 2 3 4]
b: tensor([0, 1, 2, 3, 4])
Python torch.arange Instance to explain - A course for farmers
9.np.ceil() And torch.ceil() Use the same , but torch.ceil() The input of must be tensor.
a=[0.1,1.1,2.1,3.1]
b=np.ceil(a)
c=torch.ceil(torch.Tensor(a))
print("b:",b)
print("c:",c)
b: [1. 2. 3. 4.]
c: tensor([1., 2., 3., 4.])
10.torch.sort() Replace np.sort(),torch.sort() The index corresponding to the original sequence will also be returned
import numpy as np
import torch
a=[5,1.1,2.1,3.1]
b=np.sort(a)
value,indices=torch.sort(torch.Tensor(a))
print("b:",b)
print("c:",value,indices)
b: [1.1 2.1 3.1 5. ]
c: tensor([1.1000, 2.1000, 3.1000, 5.0000]) tensor([1, 2, 3, 0])
np.sort() Function function _wx5ba0c87f1984b Technology blog _51CTO Blog
torch.sort()_James_Bobo The blog of -CSDN Blog _torch.sort()
11.torch.abs() Replace np.abs()
data = [-1 , -2 , 1 , 2]
tensor = torch.FloatTensor(data)
print(
"abs in numpy:\n",np.abs(data),
"\nabs in torch:\n",torch.abs(tensor)
)
abs in numpy: [1 2 1 2] abs in torch: tensor([1., 2., 1., 2.])
边栏推荐
- STM32F10x_硬件I2C读写EEPROM(标准外设库版本)
- Kotlin的基础用法
- Overview of wechat public platform development
- Data warehouse project is never a technical project
- STM32学习之CAN控制器简介
- JUC(JMM、Volatile)
- Network equipment hard core technology insider router Chapter 4 Jia Baoyu sleepwalking in Taixu Fantasy (Part 2)
- The first common node of the two linked lists of "Jianzhi offer"
- 网络设备硬核技术内幕 路由器篇 5 汤普金森漫游网络世界(上)
- Network equipment hard core technology insider router Chapter 17 dpdk and its prequel (II)
猜你喜欢

Unity性能优化------渲染优化(GPU)之LOD(Level of detail)

Basic usage of kotlin

Digital storage oscilloscope based on FIFO idt7202-12

Unity 鼠标控制第一人称摄像机视角

LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard

适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制

LeetCode 240. 搜索二维矩阵 II medium

EMC design scheme of CAN bus

华云数据打造完善的信创人才培养体系 助力信创产业高质量发展

Code coverage statistical artifact -jacobo tool practice
随机推荐
USB2.0接口的EMC设计方案
Problem solving in magic tower project
修改frameworks资源文件如何单编
How to package AssetBundle
Tools - common methods of markdown editor
Sword finger offer cut rope
微信公众平台开发概述
Leetcode 456.132 mode monotone stack /medium
Method of removing top navigation bar in Huawei Hongmeng simulator
Digital storage oscilloscope based on FIFO idt7202-12
DIY ultra detailed tutorial on making oscilloscope: (1) I'm not trying to make an oscilloscope
多线程环境下CountDownLatch的用法
Wechat applet realizes music search page
网络设备硬核技术内幕 路由器篇 6 汤普金森漫游网络世界(中)
Network equipment hard core technology insider router Chapter 4 Jia Baoyu sleepwalking in Taixu Fantasy (Part 2)
Two stage submission and three stage submission
IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一
Internship: compilation of other configuration classes
EMC design scheme of CAN bus
Leetcode 244周赛-赛后补题题解【西兰花选手】