首页 > 其他分享 >numpy(ndarray)和tensor(GPU上的numpy)速查

numpy(ndarray)和tensor(GPU上的numpy)速查

时间:2022-10-28 20:56:29浏览次数:78  
标签:tensor Numpy torch tf np GPU 速查 numpy axis

类型(Types)

Numpy PyTorch
np.ndarray torch.Tensor
np.float32 torch.float32; torch.float
np.float64 torch.float64; torch.double
np.float torch.float16; torch.half
np.int8 torch.int8
np.uint8 torch.uint8
np.int16 torch.int16; torch.short
np.int32 torch.int32; torch.int
np.int64 torch.int64; torch.long

构造器(Constructor)

零和一(Ones and zeros)

Numpy PyTorch
np.empty((2, 3)) torch.empty(2, 3)
np.empty_like(x) torch.empty_like(x)
np.eye torch.eye
np.identity torch.eye
np.ones torch.ones
np.ones_like torch.ones_like
np.zeros torch.zeros
np.zeros_like torch.zeros_like

从已知数据构造

Numpy PyTorch
np.array([[1, 2], [3, 4]]) torch.tensor([[1, 2], [3, 4]])
np.array([3.2, 4.3], dtype=np.float16)np.float16([3.2, 4.3]) torch.tensor([3.2, 4.3], dtype=torch.float16)
x.copy() x.clone()
np.fromfile(file) torch.tensor(torch.Storage(file))
np.frombuffer
np.fromfunction
np.fromiter
np.fromstring
np.load torch.load
np.loadtxt
np.concatenate torch.cat

数值范围

Numpy PyTorch
np.arange(10) torch.arange(10)
np.arange(2, 3, 0.1) torch.arange(2, 3, 0.1)
np.linspace torch.linspace
np.logspace torch.logspace

构造矩阵

Numpy PyTorch
np.diag torch.diag
np.tril torch.tril
np.triu torch.triu

参数

Numpy PyTorch
x.shape x.shape
x.strides x.stride()
x.ndim x.dim()
x.data x.data
x.size x.nelement()
x.dtype x.dtype

索引

Numpy PyTorch
x[0] x[0]
x[:, 0] x[:, 0]
x[indices] x[indices]
np.take(x, indices) torch.take(x, torch.LongTensor(indices))
x[x != 0] x[x != 0]

形状(Shape)变换

Numpy PyTorch
x.reshape x.reshape; x.view
x.resize() x.resize_
null x.resize_as_
x.transpose x.transpose or x.permute
x.flatten x.view(-1)
x.squeeze() x.squeeze()
x[:, np.newaxis]; np.expand_dims(x, 1) x.unsqueeze(1)

数据选择

Numpy PyTorch
np.put
x.put x.put_
x = np.array([1, 2, 3])x.repeat(2) # [1, 1, 2, 2, 3, 3] x = torch.tensor([1, 2, 3])x.repeat(2) # [1, 2, 3, 1, 2, 3]x.repeat(2).reshape(2, -1).transpose(1, 0).reshape(-1) # [1, 1, 2, 2, 3, 3]
np.tile(x, (3, 2)) x.repeat(3, 2)
np.choose
np.sort sorted, indices = torch.sort(x, [dim])
np.argsort sorted, indices = torch.sort(x, [dim])
np.nonzero torch.nonzero
np.where torch.where
x[::-1]

数值计算

Numpy PyTorch
x.min x.min
x.argmin x.argmin
x.max x.max
x.argmax x.argmax
x.clip x.clamp
x.round x.round
np.floor(x) torch.floor(x); x.floor()
np.ceil(x) torch.ceil(x); x.ceil()
x.trace x.trace
x.sum x.sum
x.cumsum x.cumsum
x.mean x.mean
x.std x.std
x.prod x.prod
x.cumprod x.cumprod
x.all (x == 1).sum() == x.nelement()
x.any (x == 1).sum() > 0

数值比较

Numpy PyTorch
np.less x.lt
np.less_equal x.le
np.greater x.gt
np.greater_equal x.ge
np.equal x.eq
np.not_equal x.ne

pytorch与tensorflow API速查表

方法名称 pytroch tensorflow numpy
裁剪 torch.clamp(x, min, max) tf.clip_by_value(x, min, max) np.clip(x, min, max)
取最小值 torch.min(x, dim)[0] tf.min(x, axis) np.min(x , axis)
取两个tensor的最大值 torch.max(x, y) tf.maximum(x, y) np.maximum(x, y)
取两个tensor的最小值 torch.min(x, y) torch.minimum(x, y) np.minmum(x, y)
取最大值索引 torch.max(x, dim)[1] tf.argmax(x, axis) np.argmax(x, axis)
取最小值索引 torch.min(x, dim)[1] tf.argmin(x, axis) np.argmin(x, axis)
比较(x > y) torch.gt(x, y) tf.greater(x, y) np.greater(x, y)
比较(x < y) torch.le(x, y) tf.less(x, y) np.less(x, y)
比较(x==y) torch.eq(x, y) tf.equal(x, y) np.equal(x, y)
比较(x!=y) torch.ne(x, y) tf.not_equal(x, y) np.not_queal(x , y)
取符合条件值的索引 torch.nonzero(cond) tf.where(cond) np.where(cond)
多个tensor聚合 torch.cat([x, y], dim) tf.concat([x,y], axis) np.concatenate([x,y], axis)
堆叠成一个tensor torch.stack([x1, x2], dim) tf.stack([x1, x2], axis) np.stack([x, y], axis)
tensor切成多个tensor torch.split(x1, split_size_or_sections, dim) tf.split(x1, num_or_size_splits, axis) np.split(x1, indices_or_sections, axis)
` torch.unbind(x1, dim) tf.unstack(x1,axis) NULL
随机扰乱 torch.randperm(n) 1 tf.random_shuffle(x) np.random.shuffle(x) 2 np.random.permutation(x ) 3
前k个值 torch.topk(x, n, sorted, dim) tf.nn.top_k(x, n, sorted) NULL
  1. 该方法只能对0~n-1自然数随机扰乱,所以先对索引随机扰乱,然后再根据扰乱后的索引取相应的数据得到扰乱后的数据
  2. 该方法会修改原值,没有返回值
  3. 该方法不会修改原值,返回扰乱后的值

标签:tensor,Numpy,torch,tf,np,GPU,速查,numpy,axis
From: https://www.cnblogs.com/mfmufeng/p/numpy-tensor.html

相关文章

  • 如何查看你使用的pytorch是否为GPU版本
    >>>importtorch>>>torch.cuda.is_available()若返回为True,则使用的是GPU版本的torch,若为False,则为CPU版本 >>>importtorch>>>print(torch.cuda.get_device_nam......
  • numpy和 pandas学习
    这是我自己的学习笔记,就不要看了。##技术篇###numpy基础-numpy生成随机数据np.random.normal(0,10,1024)标准正态分布平均数0,标准差10,1024个数据,正态分布也......
  • Numpy
    ndarray对象基本应用标准的Python中用列表(list)保存一组值,可以当作数组使用。但由于列表的元素可以是任何对象,因此列表中保存的是对象的指针。对于数值运算来说,这种结构......
  • 用numpy实现最简单的前馈神经网络——正向网络建立篇
    目录mnist分析(输入分析)下载简要说明加载显示输出分析拟合函数建立激活函数拟合函数正向计算梯度梯度下降最终代码根据上一篇文章,来构建神经网络吧明确输入和输出选择合......
  • 用numpy实现最简单的前馈神经网络——神经网络架构篇
    目录神经网络架构矩阵运算拟合——深度学习的目的最简单的拟合——线性回归深度学习中的拟合平均损失最小——梯度下降法反向传播和链式法则激活函数和损失函数的选择总结......
  • Numpy温习函数方法
    一、NumpyPandas1.1简介方便数组矩阵运算1.2优势运算速度快:numpy和pandas都是采用C语言编写,pandas又是基于numpy,是numpy的升级版本。消耗资源少:采用的是......
  • STM32HAL库常用函数速查手册
    STM32HAL库常用函数速查手册(V1.0.0.20221019_BETA)前言写程序时想不起来函数很麻烦,于是做了这么一个手册,常用的一些部分都加了注释标注了中文,搜集资料主要来源于CSDN和官......
  • PyTorch中的多GPU训练:DistributedDataParallel
    在pytorch中的多GPU训练一般有2种DataParallel(DP)和DistributedDataParallel(DDP),DataParallel是最简单的的单机多卡实现,但是它使用多线程模型,并不能够在多机多卡的环境下使......
  • windows使用nvidia-smi查看gpu信息
    需要在​​path​​添加如下路径才可以直接在cmd中使用nvidia-smi命令等。C:\ProgramFiles\NVIDIACorporation\NVSMIFan:显示风扇转速,数值在0到100%之间,是计算机的期望转......
  • python numpy 基础科学计算包,数学函数库
    pipinstallnumpynumpy.array()函数,强大的N维数组对象ndarrayimportnumpyasnpa=np.array([1,2,3])print(a)[123]#多于一个维度importnumpyasnpa......