numpy(ndarray)和tensor(GPU上的numpy)速查
类型(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 |
- 该方法只能对0~n-1自然数随机扰乱,所以先对索引随机扰乱,然后再根据扰乱后的索引取相应的数据得到扰乱后的数据
- 该方法会修改原值,没有返回值
- 该方法不会修改原值,返回扰乱后的值
numpy(ndarray)和tensor(GPU上的numpy)速查的更多相关文章
- torch.Tensor和numpy.ndarray
1. torch.Tensor和numpy.ndarray相互转换 import torch import numpy as np # <class 'numpy.ndarray'> np ...
- 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. ...
- 解决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 ...
- 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 ...
- 关于类型为numpy,TensorFlow.tensor,torch.tensor的shape变化以及相互转化
https://blog.csdn.net/zz2230633069/article/details/82669546 2018年09月12日 22:56:50 一只tobey 阅读数:727 1 ...
- tensorflow2.0 numpy.ndarray 与tenor直接互转
1.代码参考 import numpy as npimport tensorflow as tf a = np.random.random((5,3)) b = np.random.randint(0 ...
- Numpy - 多维数组(上)
一.实验说明 numpy 包为 Python 提供了高性能的向量,矩阵以及高阶数据结构.由于它们是由 C 和 Fortran 实现的,所以在操作向量与矩阵时性能非常优越. 1. 环境登录 无需密码自动 ...
- python中numpy.ndarray.shape的用法
今天用到了shape,就顺便学习一下,这个shape的作用就是要把矩阵进行行列转换,请看下面的几个例子就明白了: >>> import numpy as np >>> ...
- NumPy Ndarray 对象
NumPy Ndarray 对象 NumPy 最重要的一个特点是其 N 维数组对象 ndarray,它是一系列同类型数据的集合,以 0 下标为开始进行集合中元素的索引. ndarray 对象是用于存放 ...
随机推荐
- React报错之Cannot find namespace context
正文从这开始~ 总览 在React中,为了解决"Cannot find namespace context"错误,在你使用JSX的文件中使用.tsx扩展名,在你的tsconfig. ...
- Flutter 开启 Windows、macOS 平台支持的命令
Flutter 的多平台支持除了 Android 和 iOS 是默认开启的以外,比如 Windows.Linux 平台的支持需要手动开启. Flutter config 命令集中,有以下参数是对于平台 ...
- (已解决)Adobe Creative Cloud 安装 Acrobat PDF 报错 DW071 DW003
今天安装 Adobe Acrobat pdf 阅读器报错了,错误为 Exit Code: 7 Please see specific errors below for troubleshooting. ...
- P4035 [JSOI2008]球形空间产生器 (向量,高斯消元)
题面 有一个 n n n 维球,给定 n + 1 n+1 n+1 个在球面上的点,求球心坐标. n ≤ 10 n\leq 10 n≤10 . 题解 好久以前的题了,昨天首 A . n n n 太小了! ...
- [Golang] cgo 调用 .so 捕获异常问题
最近需要在 go 中去调用 .so 库去完成一些事情,go 方面,利用 cgo 可以顺利的调用 .so 中的方法,但是有个问题是 go 没法捕获 .so 那边出现的异常.如果 .so 那边异常了,那么 ...
- CentOS7_SSH_安装总结
在使用ssh 连接自己的centos 虚拟机时,发现连接不上,于是有了这个安装过程 (以下是在root用户下执行的,没权限的话就sudo) 1.首先判断是否有这个服务 systemctl list-u ...
- linux 运维有趣的实用工具
1.实时监控磁盘 IO-IOTop IOTop 命令是专门显示硬盘 IO 的命令, 界面风格类似 top 命令. [root@localhost ~]# yum -y install iotop` 2 ...
- winform,隐藏窗体
public Form1() { InitializeComponent(); this.WindowState = FormWindowSt ...
- 腾讯云即时通信 IM 服务 实例项目
腾讯云即时通信 IM 服务 https://github.com/tencentyun/TIMSDK
- java项目中VO、DTO以及Entity,各自是在什么情况下应用的
按照标准来说: entity里的每一个字段,与数据库相对应 vo里的每一个字段,是和你前台页面相对应 dto,这是用来转换从entity到dto,或者从dto到entity的中间的东西 举个例子: h ...