类型(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. [BJDCTF2020]Mark loves cat-1|源代码泄露|变量覆盖

    主要考察了:源代码泄露.变量覆盖 共展示了三种获取flag的方式 1.打开题目查看未发现有效信息,查看源代码信息,发现返回的dog信息,结果如下: 2.使用dirmap进行目录扫描,发现了.git/c ...

  2. 从C过渡到C++(1)——GNU/Linux

    从C过渡到C++(1)--GNU/Linux 目录 从C过渡到C++(1)--GNU/Linux 大名鼎鼎的GNU/Linux GNU GNU的组成 一点补充 MinGW 运行时库 额外的内容 Min ...

  3. centos 安装ftp服务BUG

    安装完成之后匿名可登录,但是先创建的用户名和密码无法登录,最后排查原因是/etc/pam.d/vsftpd 文件注释掉第四行 auth required pam_shells.so

  4. C#基础_理解类

    构造函数主要是用来创建对象时为对象赋初值来初始化对象.总与new运算符一起使用在创建对象的语句中 .A a=new A(); 构造函数具有和类一样的名称:但它是一个函数具有函数的所有特性,同一个类里面 ...

  5. UE 实现镜头平移,旋转和缩放

    0x00 引 在数字孪生三维场景中,通过键盘和鼠标来控制镜头的移动,缩放是很常见的行为,也是很必要的行为,用户正是通过这些操作,达到对整个三维场景的观看和控制. 0x01 键盘控制镜头前后左右移动 通 ...

  6. openstack中Neutron组件简解

    一.Neutron概述 Neutron 的设计目标是实现"网络即服务(Networking as a Service)".为了达到这一目标,在设计上遵循了基于 SDN 实现网络虚拟 ...

  7. KingbaseES blob 类型数据导入导出

    KingbaseES兼容了oracle的blob数据类型.通常是用来保存二进制形式的大数据,也可以用来保存其他类型的数据. 下面来验证一下各种数据存储在数据库中形式. 建表 create table ...

  8. KingbaseES R3 集群pcp_attach_node 更新show pool_nodes中节点状态

    系统环境: 操作系统: [kingbase@node2 bin]$ cat /etc/centos-release CentOS Linux release 7.2.1511 (Core) 数据库: ...

  9. JTS TopologyException 问题

    计算2个几何相交结果时候,报错了: val geometry = polygon.intersection(lineString) 日志如下 org.locationtech.jts.geom.Top ...

  10. Ladon简单使用例子

    Socks5代理扫描 例子:扫描目标10.1.2段是否存在MS17010漏洞(必须加noping) Ladon noping 10.1.2.8/24 MS17010 详见:http://k8gege. ...