深度学习框架,似乎永远离不开哪个最热哪个最实用的话题,自己接触甚浅,尚不敢对齐进行大加评论,这里也只是初步接触。目前常见的有TensorFlow,pytorch,Keras等,至于目前哪个做好用,就像TIOBE编程语言的统计一样,每一个都有自己的拥趸。目前Tensorflow和pytorch竞争相当火爆,pytorch上升速度极快,至于是否真正超过了TesorFlow了,可以通过这篇博文进行了解。
使用Tensorflow配和使用Numpy,Numpy提供了强大的数组和矩阵的功能,二者的区别引用一位他人的总结就是:“一个好的框架应该要具备三点:对大的计算图能方便的实现;能自动求变量的导数;能简单的运行在GPU上;pytorch都做到了,但是现在很多公司用的都是TensorFlow,而pytorch由于比较灵活,在学术科研上用得比较多一点。鄙人认为可能,Google可能下手早一些,而Facebook作后来者,虽然灵活,但是很多公司已经入了TensorFlow的坑了,要全部迁移出来还是很费功夫;而且,TensorFlow在GPU的分布式计算上更为出色,在数据量巨大时效率比pytorch要高一些,这个也是一个重要的原因吧。”,因此pytorch一些函数方法和numpy中的函数方法说几乎等同也不为过。下面先看一下Numpy中的几个方法
import numpy as np
a = np.array([[1, 2], [3, 4]])
#创建空矩阵.数组
numpy.empty(shape, dtype = float, order = 'C')
#创建全0矩阵
numpy.zeros(shape, dtype = float, order = 'C')
#创建指定范围的数组
numpy.arange(start, stop, step, dtype)
1.张量Tesnors
我们知道深度学习中,Tensorflow和pytorch等都是基于对数组矩阵进行大量的拟合,变换,微分等运算,因此张量是线性代数中用到的一种数据结构,类似向量和矩阵,可以在张量上进行算术运算。张量可以说是Tensorflow和pytorch的基石。
Tensors 类似于 NumPy 的 ndarrays ,同时 Tensors 可以使用 GPU 进行计算。使用tensor之前先导入torch
torch.is_tensor(obj) | 如果 obj 是一个 pytorch tensor, 则返回True. |
torch.is_storage(obj) | 如果 obj 是一个 pytorch storage object, 则返回True |
import torch
x = torch.randn(5,3)
print(torch.is_tensor(x))
print(torch.is_storage(x))
print(torch.numel(x))
torch.numel(input) → int | 返回 input Tensor 中的元素总数 |
2.创建函数
创建函数和Numpy中的几乎完全一样,包括常见的ones,zeros,eye,arrange等
torch.zeros(*sizes, out=None) → Tensor | 返回填充了标量值为 0 的 Tensor, 其形状由可变参量 sizes 定义 |
返回一个全0 的张量,其形状有sizes定义
x = torch.zeros(5,dtype=torch.long)
print(x)
x = torch.zeros((5,3),dtype=torch.long)
print(x)
x = torch.zeros([5,3],dtype=torch.long)
print(x)
torch.zeros_like(input, out=None) → Tensor | 返回一个用标量值 0 填充的 Tensor, 其大小与 input 相同. |
还有一个常用函数同zeros,就是zeros_like,见名知著,顾名思义就是像什么一样的一个功能函数
x = torch.zeros((5,3),dtype=torch.long)
print(x)
y = torch.zeros_like(x)
print(y)
torch.from_numpy(ndarray) → Tensor | 从 numpy.ndarray 类 创建一个 Tensor 类 |
import torch
import numpy as np
x = np.array([5,3])
y = torch.from_numpy(x)
print(y)
注意:将numpy.ndarray
转换为Tensor
,返回的张量tensor和numpy的ndarray共享同一内存空间,修改一个会导致另一个也被修改,返回的张量不能改变大小
torch.eye(n, m=None, out=None) | 返回对角线位置全为1, 其它位置全为0的二维 tensor |
返回一个2维张量,对角线为1,其它位置为0,注意:eye(N)是单位矩阵,非全1矩阵
- n (int) -行数
- m (int, optional)列数,如果为None,则默认为n
- out (Tensor, optional)
torch.empty() | |
torch.empty_like() |
这里的创建规则同ones和ones_like一样
x = torch.empty(5,3)
y = torch.empty_like(x)
print(y)
torch.full(size, fill_value, …) | 返回大小为sizes,单位值为fill_value的矩阵 |
torch.full_like(input, fill_value, …) | 返回与input相同size,单位值为fill_value的矩阵 |
x=torch.full([5,3], 1)
print(x)
x=torch.full([], 1) # 生成一个标量
print(x)
x=torch.full([1], 1) # 生成一个标量
print(x)
输出结果为如下:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
tensor(1.)
tensor([1.])
还有一些其他函数比如等比,等差数列比较好理解
torch.linspace(start, end, steps=100, out=None) → Tensor | 返回 start 和 end 之间等间隔 steps 点的一维 Tensor.输出 是尺寸 steps 为一维 tensor |
torch.logspace(start, end, steps=100, out=None) → Tensor | 返回一个在 10^start和 10^end之间的对数间隔 steps 点的一维 Tensor,输出是长度为 steps 的一维 tensor |
生成一维Tensor有arrage和range两个函数
torch.arange(start=0, end, step=1, out=None) → Tensor | 从 start 用步长为 step 开始, 间隔在 [start, end) 中的值返回大小层次为 floor((end−start)/step)floor((end−start)/step) 的一维 Tensor. |
torch.range(start, end, step=1, out=None) → Tensor | 返回一个在 start 到 end 并且步长为 step 的区间内, 大小为 floor((end−start)/step)+1floor((end−start)/step)+1 为一维 Tensor. |
推荐使用torch.arrange()
3. 索引,切片,连接,换位(Index, Slicing, Joining, Mutating)
torch.cat(seq, dim=0, out=None) → Tensor | 在给定维度上对输入的张量序列 seq 进行连接操作. 所有张量必须具有相同的形状(在 cat 维度中除外) 或为空. |
torch.chunk(tensor, chunks, dim=0) | 在给定维度(轴)上将输入张量进行分块处理. |
torch.gather(input, dim, index, out=None) → Tensor | 沿给定轴 dim ,将输入索引张量 index 指定位置的值进行聚合. |