博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PyTorch的学习笔记01-基础中的基础
阅读量:2134 次
发布时间:2019-04-30

本文共 2422 字,大约阅读时间需要 8 分钟。

主要内容源于对PyTorch的doc的阅读:

Tensor的element-wise的乘法是直接使用 * 。而Tensor之间的矩阵乘法使用Tensor.mm()方法。

仅记录了我认为比较常用和有用的API。

torch.is_tensor(obj),若obj为Tensor类型,那么返回True。

torch.numel(obj),返回Tensor对象中的元素总数。

torch.eye(n),返回一个单位方阵,和MATLAB的eye()非常像。还有其他参数。

torch.from_numpy(obj),利用一个numpy的array创建Tensor。注意,若obj原来是1列或者1行,无论obj是否为2维,所生成的Tensor都是一阶的,若需要2阶的Tensor,需要利用view()函数进行转换。

torch.linspace(start, end, steps),返回一个1维的Tensor。

torch.ones(),与MATLAB的ones很接近。

torch.ones_like(input),返回一个全1的Tensor,其维度与input相一致。

torch.arange(start, end, step),直接返回一个Tensor而不是一个迭代器。

torch.zeros(),与MATLAB的zeros很像。

torch.zeros_like(),与torch.ones_like()类似。

torch.cat(seq, dim),将tuple seq中描述的Tensor进行连接,通过实例说明用法。

torch.chunk(input, chunks, dim),与torch.cat()的作用相反。注意,返回值的数量会随chunks的值而发生变化.

torch.index_select(input, dim, index),注意,index是一个1D的Tensor。

torch.masked_select(input, mask),有点像MATLAB中利用bool类型矩阵进行索引的功能,要求mask是ByteTensor类型的Tensor。参考示例代码。注意,执行结果是一个1D的Tensor。

torch.squeeze(input),将input中维度数值为1的维度去除。可以指定某一维度。结果是共享input的内存的。

torch.t(input),将input进行转置,不是in place。输出的结果是共享内存的。要求input为2D。

torch.unsqeeze(input, dim),在input目前的dim维度上增加一维。

好多random sampling的函数接口,还有inplace的。

torch.save()和torch.load()

不常见的运算函数

torch.clamp(input, min, max),将input的值约束在min和max之间

torch.trunc(input),将input的小数部分舍去。

torch.norm()

还有一些统计功能的函数。

torch.eq(input, other),返回一个Tensor。

torch.equal(input, other),返回True,False。

还有一些用于比较的函数,包括ne(), kthmin(), topk()

torch.grad(),与MATLAB的diag可能不同,这个函数将返回一个与原Tensor维度相同的Tensor。

torch.trace(),

torch.tril()和torch.triu(),返回下三角和上三角Tensor。

有一些用于batch上乘法,加法的函数。

torch.btriface()和torch.btrisolve(),LU分解和线性求解。

torch.dot(), torch.eig(), torch.inverse(), torch.matmul(), torch.mv()等函数。有各种decomposition的函数。

简单示例代码

import numpy as npimport torchimport copyif __name__ == '__main__':	t1 = torch.randn(4, 4)	n2 = np.random.rand(3)	n2 = n2.reshape(3, 1)	tn2 = torch.from_numpy(n2)	n3 = copy.deepcopy(n2)	n3 = n3.reshape(1,3)	tn3 = torch.from_numpy(n3)	# cat().	t4 = torch.cat( (tn3, tn3), dim = 0 )	t5 = torch.cat( (tn3, tn3), dim = 1 )	t4_2 = torch.cat( (t4, tn3), dim = 0 )	# thunk().	t6_0, t6_1 = torch.chunk( t1, 2, dim = 0 )	t7_0, t7_1 = torch.chunk( t1, 2, dim = 1 )	# masked_select().	t8_mask = t1.ge(0.1)	t8 = torch.masked_select( t1, t8_mask )

PyTorch 最开始的教程里给出了将Tensor转存在GPU上的方法

if torch.cuda.is_available():    device = torch.device("cuda")    y = torch.ones_like(x, device=device)    x = x.to(device)    z = x + y    print(z)    print(z.to("cpu", torch.double))

转载地址:http://vsugf.baihongyu.com/

你可能感兴趣的文章
机器学习算法应用中常用技巧-1
查看>>
机器学习算法应用中常用技巧-2
查看>>
通过一个kaggle实例学习解决机器学习问题
查看>>
决策树的python实现
查看>>
Sklearn 快速入门
查看>>
了解 Sklearn 的数据集
查看>>
用ARIMA模型做需求预测
查看>>
推荐系统
查看>>
详解 TensorBoard-如何调参
查看>>
TensorFlow-11-策略网络
查看>>
浅谈 GBDT
查看>>
如何选择优化器 optimizer
查看>>
一文了解强化学习
查看>>
CART 分类与回归树
查看>>
seq2seq 的 keras 实现
查看>>
seq2seq 入门
查看>>
什么是 Dropout
查看>>
用 LSTM 做时间序列预测的一个小例子
查看>>
用 LSTM 来做一个分类小问题
查看>>
详解 LSTM
查看>>