类型(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. 该方法不会修改原值,返回扰乱后的值

numpy(ndarray)和tensor(GPU上的numpy)速查的更多相关文章

  1. torch.Tensor和numpy.ndarray

    1. torch.Tensor和numpy.ndarray相互转换 import torch import numpy as np # <class 'numpy.ndarray'> np ...

  2. Python中 list, numpy.array, torch.Tensor 格式相互转化

    1.1 list 转 numpy ndarray = np.array(list) 1.2 numpy 转 list list = ndarray.tolist() 2.1 list 转 torch. ...

  3. 解决Tensorflow ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)

    问题描述 在将一个数组送入tensorflow训练时,报错如下: ValueError: Failed to convert a NumPy array to a Tensor (Unsupporte ...

  4. has invalid type <class 'numpy.ndarray'>, must be a string or Tensor

    转自: https://blog.csdn.net/jacke121/article/details/78833922 has invalid type <class 'numpy.ndarra ...

  5. 关于类型为numpy,TensorFlow.tensor,torch.tensor的shape变化以及相互转化

    https://blog.csdn.net/zz2230633069/article/details/82669546 2018年09月12日 22:56:50 一只tobey 阅读数:727   1 ...

  6. tensorflow2.0 numpy.ndarray 与tenor直接互转

    1.代码参考 import numpy as npimport tensorflow as tf a = np.random.random((5,3)) b = np.random.randint(0 ...

  7. Numpy - 多维数组(上)

    一.实验说明 numpy 包为 Python 提供了高性能的向量,矩阵以及高阶数据结构.由于它们是由 C 和 Fortran 实现的,所以在操作向量与矩阵时性能非常优越. 1. 环境登录 无需密码自动 ...

  8. python中numpy.ndarray.shape的用法

    今天用到了shape,就顺便学习一下,这个shape的作用就是要把矩阵进行行列转换,请看下面的几个例子就明白了: >>> import numpy as np >>> ...

  9. NumPy Ndarray 对象

    NumPy Ndarray 对象 NumPy 最重要的一个特点是其 N 维数组对象 ndarray,它是一系列同类型数据的集合,以 0 下标为开始进行集合中元素的索引. ndarray 对象是用于存放 ...

随机推荐

  1. Dynamic CRM使用FetchXML在js中查询与调用传递编码问题

    在页面交互脚本js中实现窗体交互逻辑是很常见的crm场景,一般情况下使用拓展工具RESTBuilder编辑器,可以很方便的进行操作,增删改查均能实现,但在某些较为特殊的场景下,需要根据条件去拼接查询过 ...

  2. systemd之导致内核 crash

    本文主要讲解linux kernel panic系列其中一种情况: Attempted to kill init! exitcode=0x0000000b 背景:linux kernel 的panic ...

  3. Python自学教程2:大牛们怎么写注释

    在还没开始学代码前,就要先学会写注释.不会写注释的程序员会遭到鄙视和唾弃,甚至在工作中会被人穿小鞋.注释也不是随便写一下就行,用好注释还是有点讲究的. 注释有什么用? 注释(Comments)主要是向 ...

  4. Jamie and Tree (dfs序 + 最近公共祖先LCA)

    题面 题解 我们求它子树的权值和,一般用dfs序把树拍到线段树上做. 当它换根时,我们就直接把root赋值就行了,树的结构不去动它. 对于第二个操作,我们得到的链和根的相对位置有三种情况: 设两点为A ...

  5. 【c#】仅1600行代码 2D魔方游戏源码-纯WinForm

    想起以前高三的时候写过一个很无脑的程序,那个时候.net5.0都还没影儿呢,,现在分享一下.一个平面展开的魔方游戏. 这个是1.0版本,有些许bug. 比如左边的格子操作不了. 「2d cube.ex ...

  6. 面试现场!月薪3w+的这些数据挖掘SQL面试题你都掌握了吗? ⛵

    作者:韩信子@ShowMeAI 数据分析实战系列:https://www.showmeai.tech/tutorials/40 AI 面试题库系列:https://www.showmeai.tech/ ...

  7. Spring MVC组件之HandlerAdapter

    Spring MVC组件之HandlerAdapter HandlerAdapter概述 HandlerAdapter组件是一个处理器Handler的适配器.HandlerAdapter组件的主要作用 ...

  8. 【必知必会】手把手教你配置MySQL环境变量——图文详解

    一.先决条件 假设我们已经成功安装MySQL数据库.如果还有小伙伴不知道如何安装MySQL数据库,可以在本文下留言,留言数超20,则出一期"手把手教你安装MySQL数据库--图文详解&quo ...

  9. KingbaseES 两表关联Update的两种写法与性能

    熟悉oracle 的人都知道,对于两表的关联更新,其执行计划主要有 Filter 和 Outer Join 两种方式.对于大批量数据的update,Join方式明显是更优的选择.KingbaseES ...

  10. KingbaseES R3 受限dba影响集群切换

    ​ 一.受限dba功能说明(参考自官方文档) 受限DBA 受限DBA可以对当前DBA的权限进行一定限制.当功能开启后DBA将不能更改以下对象: Table Database Function(by n ...