类型(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. 【RocketMQ】事务的实现原理

    事务的使用 RocketMQ事务的使用场景 单体架构下的事务 在单体系统的开发过程中,假如某个场景下需要对数据库的多张表进行操作,为了保证数据的一致性,一般会使用事务,将所有的操作全部提交或者在出错的 ...

  2. 必应每日壁纸API

    必应官网每天会更新不同的高质量背景图 我们可以通过 F12 开发者工具手动下载当天的壁纸,网上也有很多网站提供必应壁纸下载.如果我们想每天应用最新的壁纸到我们的网站上就需要手动下载再设置壁纸,比较麻烦 ...

  3. 给ShardingSphere提了个PR,不知道是不是嫌弃我

    说来惭愧,干了 10 来年程序员,还没有给开源做过任何贡献,以前只知道嘎嘎写,出了问题嘎嘎改,从来没想过提个 PR 去修复他,最近碰到个问题,发现挺简单的,就随手提了个 PR 过去. 问题 问题挺简单 ...

  4. 【AGC】使用云调试优惠扣费、华为设备上触发崩溃、无法下载华为应用市场问题小结

    ​ 1.使用云调试剩余优惠时长还剩300分钟,但还会扣费的情况. 问题描述:用户的云调试账户显示剩余优惠时长还有300分钟,但在使用云调试的过程中,还是产生了扣费的情况. ​ 解决方案: 当开发者成功 ...

  5. vscode 快速注释和撤回快捷键

    好家伙,天天忘,建议先练个十遍上手 1.快捷行注释 Ctrl + / 2.快捷块注释 Alt + Shift + A 3.撤回 Ctrl + Z 4.恢复撤回(撤回你的撤回) Ctrl + Shift ...

  6. CPU流水线与指令乱序执行

    青蛙见了蜈蚣,好奇地问:"蜈蚣大哥,我很好奇,你那么多条腿,走路的时候先迈哪一条啊?" 蜈蚣听后说:"青蛙老弟,我一直就这么走路,从没想过先迈哪一条腿,等我想一想再回答你 ...

  7. 【ASP.NET Core】自定义Session的存储方式

    在开始今天的表演之前,老周先跟大伙伴们说一句:"中秋节快乐". 今天咱们来聊一下如何自己动手,实现会话(Session)的存储方式.默认是存放在分布式内存中.由于HTTP消息是无状 ...

  8. C#/VB.NET 如何在Excel中使用条件格式设置交替行颜色

    说起高亮数据行,不让人想起了交替颜色行,有的人把交替颜色行也都设置成高亮,不仅不美观,而且对阅读还是个干扰.隔行交替的颜色是为了阅读不串行,这些行只是环境,数据才是主体.那么如何通过C#/VB.NET ...

  9. SQL语句中过滤条件放在on和where子句中的区别和联系

    摘要: 介绍在多表关联SQL语句中,过滤条件放在on和where子句中的区别--inner join中没区别,外连接就不一样. 综述   蚂蚁金服的一道SQL面试题如下:SQL语句中,过滤条件放在on ...

  10. Beats:如何安装Packetbeat