Pytorch基础知识
张量
简介
几何代数中定义的张量是基于向量和矩阵的推广,比如我们可以将标量视为零阶张量,矢量可以视为一阶张量,矩阵就是二阶张量。
张量维度 | 代表含义 |
---|---|
0维张量 | 代表的是标量(数字) |
1维张量 | 代表的是向量 |
2维张量 | 代表的是矩阵 |
3维张量 | 时间序列数据 股价 文本数据 单张彩色图片(RGB) |
张量是现代机器学习的基础。它的核心是一个数据容器,多数情况下,它包含数字,有时候它也包含字符串,但这种情况比较少。
这里有一些存储在各种类型张量的公用数据集类型:
- 3维 = 时间序列
- 4维 = 图像
- 5维 = 视频
例子:一个图像可以用三个字段表示:
(width, height, channel) = 3D
但是,在机器学习工作中,我们经常要处理不止一张图片或一篇文档——我们要处理一个集合。我们可能有10,000张郁金香的图片,这意味着,我们将用到4D张量:
(batch_size, width, height, channel) = 4D
在PyTorch中, torch.Tensor
是存储和变换数据的主要工具。如果你之前用过NumPy
,你会发现 Tensor
和NumPy的多维数组非常类似。然而,Tensor
提供GPU计算和自动求梯度等更多功能,这些使 Tensor
这一数据类型更加适合深度学习。
创建Tensor
-
初始化一个随机数
Tensor
>>> import tensor >>> torch_random_4_3 = torch.rand(4,3) >>> print(torch_random_4_3) tensor([[0.5949, 0.7045, 0.9920], [0.0107, 0.6573, 0.7009], [0.6846, 0.9896, 0.4545], [0.9499, 0.4030, 0.4142]])
-
初始化一个全 0
Tensor
或者 全 1Tensor
>>> import tensor >>> tensor_zero_4_3 = torch.zeros(4,3, dtype=torch.long) >>> print(tensor_zero_4_3) tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]) >>> tensor_one_4_3 = torch.ones(4,3, dtype=torch.long) >>> print(tensor_one_4_3) tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])
-
从
List
或者numpy.array
转换成Tensor
>>> import torch >>> tensor_by_list = torch.tensor([5.5, 3]) >>> print(tensor_by_list) tensor([5.5000, 3.0000]) >>> import numpy as np >>> tensor_by_numpy = torch.tensor(np.array([5.5, 3])) >>> print(tensor_by_numpy) tensor([5.5000, 3.0000], dtype=torch.float64)
-
创建相同维度的
Tensor
>>> import torch >>> tensor_one_4_3 = torch.ones(4,3, dtype=torch.double) >>> tensor_random_cpdim = torch.randn_like(tensor_one_4_3, dtype=torch.float) >>> print(tensor_one_4_3) tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], dtype=torch.float64) >>> print(tensor_random_cpdim) tensor([[ 1.2542, 0.3018, 1.7961], [ 2.0652, 0.3100, 0.9422], [-0.7710, 0.8658, -0.0066], [-0.6149, -1.5649, -0.1749]]) >>> print(tensor_one_4_3.shape) # 等同于 print(tensor_one_4_3.size()) torch.Size([4, 3]) >>> print(tensor_random_cpdim.shape) # 等同于 print(tensor_one_4_3.size()) torch.Size([4, 3])
-
常见的构造Tensor的方法:
函数 功能 torch.Tensor(sizes) 基础构造函数 torch.tensor(data) 类似于np.array torch.ones(sizes) 全1矩阵 torch.zeros(sizes) 全0矩阵 torch.eye(sizes) 对角为1,其余为0 torch.arange(s,e,step) 从s到e,步长为step torch.linspace(s,e,steps) 从s到e,均匀分成step份 torch.rand rand是[0,1)均匀分布 torch.randn(sizes) randn是服从N(0,1)的正态分布 torch.normal(mean,std) 正态分布(均值为mean,标准差是std) torch.randperm(m) 随机排列
张量的操作
-
加法操作:
>>> import torch # 方式1 >>> y = torch.rand(4, 3) >>> print(x + y) tensor([[ 2.8977, 0.6581, 0.5856], [-1.3604, 0.1656, -0.0823], [ 2.1387, 1.7959, 1.5275],