张量(Tensor)
张量可以简单理解为多维数组,理论上张量是任意维度。
张量操作基本方法
首先检验一下终端的pytorch是否安装成功: Win + R 打开CMD 命令,输入“ipython”然后构建一个张量并输出
In [1]: import torch
In [2]: torch.Tensor(3,3)
Out[2]:
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
1.判断一个对象是否为Tensor
import torch
import numpy as np
a = np.arange(1, 10)
b = torch.Tensor(10)
print(a)
print(torch.is_tensor(a))
print(b)
print(torch.is_tensor(b))
[1 2 3 4 5 6 7 8 9]
False
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
True
(不得不说刚刚犯了一个很蠢的错误,我新建了一个 torch.py 文件来试验这些语句,因为要导入torch模块,然后就把这个文件导入了而没有导入正规的 “torch.py”,所以他一直报错说torch没有 Tensor这个模块,真是被自己蠢到了23333)
2.获取张量中元素个数
In [1]: import torch
In [2]: a=torch.Tensor(3,4)
In [3]: print(torch.numel(a))
12
3.创建单位矩阵
In [4]: torch.eye(3,3)
Out[4]:
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
4.将Numpy数组转化为Tensor
In [5]: import numpy as np
In [6]: a = np.arange(1,10)
In [7]: b = np.ndarray((2,3))
In [8]: print(torch.from_numpy(a))
tensor([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=torch.int32)
In [9]: print(torch.from_numpy(b))
tensor([[0., 0., 0.],
[0., 0., 0.]], dtype=torch.float64)
5.构造等差数列
In [26]: print(torch.arange(1,10))
tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [13]: print(torch.linspace(1,8,3))
tensor([1.0000, 4.5000, 8.0000])
In [14]: print(torch.linspace(1,8,4))
tensor([1.0000, 3.3333, 5.6667, 8.0000])
三个参数分别为初始值,终值和元素个数 lin(线性)-space(空间)
类似的还有指数空间 log-space
In [18]: print(torch.logspace(1,5,5))
tensor([1.0000e+01, 1.0000e+02, 1.0000e+03, 1.0000e+04, 1.0000e+05]) # e是乘以10的后面次
6.初始化矩阵
这个和Numpy非常类似
In [19]: print(torch.ones(2,3))
tensor([[1., 1., 1.],
[1., 1., 1.]])
In [20]: print(torch.zeros(2,3))
tensor([[0., 0., 0.],
[0., 0., 0.]])
In [21]: print(torch.ones(2,3)*255)
tensor([[255., 255., 255.],
[255., 255., 255.]])
7.随机矩阵:
#一维序列
In [28]: print(torch.randperm(10))
tensor([5, 1, 6, 2, 4, 8, 9, 0, 3, 7])
In [22]: print(torch.rand(2,3))
tensor([[0.7700, 0.8872, 0.3704],
[0.9212, 0.2521, 0.9426]])
# 服从标准正太分布的随机矩阵
In [27]: print(torch.randn(2,3))
tensor([[-0.9664, 1.3462, -0.9360],
[-2.2261, 0.5857, -1.6371]])
8.矩阵的拼接
# torch。cat()
a = torch.ones(2,3)
In [40]: print(torch.cat([a,a],dim=0)) # 默认为0,计算机中0表示第一个,即第一个维度,表示纵向
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
In [41]: print(torch.cat([a,a],dim=1)) # 横向
tensor([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])
9.切片
In [42]: a = torch.ones(2,10)
In [43]: a
Out[43]:
tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
In [45]: print(torch.chunk(a,5,dim=1))
(tensor([[1., 1.],
[1., 1.]]), tensor([[1., 1.],
[1., 1.]]), tensor([[1., 1.],
[1., 1.]]), tensor([[1., 1.],
[1., 1.]]), tensor([[1., 1.],
[1., 1.]]))
In [46]: b=torch.chunk(a,5,dim=1)
In [47]: print(b[1])
tensor([[1., 1.],
[1., 1.]])
# 分割为任意形状
In [23]: print(torch.split(a,[1,2,3,4],dim=1))
(tensor([[1.],
[1.]]), tensor([[1., 1.],
[1., 1.]]), tensor([[1., 1., 1.],
[1., 1., 1.]]), tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.]]))
10.从一个张量中选取部分数据组成一个新的张量
x=torch.Tensor([[1,2,3],[4,5,6]])
torch.gather(x,1,torch.LongTensor([[0,1],[2,3]])) # 第一个参数为原Tensor,第二个参数为维数,第三个参数为索引
Out[5]:
tensor([[1., 2.],
[6., 5.]])
11.找出矩阵中满足条件的元素
x= torch.rand(2,4)
In [17]: print(x)
tensor([[0.4096, 0.5604, 0.1067, 0.1627],
[0.6982, 0.5596, 0.5939, 0.8256]])
mask = x.ge(0.5) # 大于0.5为真
In [18]: print(mask)
tensor([[False, True, False, False],
[ True, True, True, True]])
In [16]: torch.masked_select(x,mask) # 赋值为真的元素形成一个一维数组
Out[16]: tensor([0.5604, 0.6982, 0.5596, 0.5939, 0.8256])
12.找出矩阵中非0元素的索引
In [19]: x = torch.Tensor([[0,1,2],[1,0,2],[1,2,0]])
In [20]: torch.nonzero(x)
Out[20]:
tensor([[0, 1],
[0, 2],
[1, 0],
[1, 2],
[2, 0],
[2, 1]])
13.改变矩阵的维度
# 增加维度
# dim指定在第几个维度增加[]
In [24]: x=torch.Tensor([1,2,3,4])
In [25]: y = x.unsqueeze(dim = 0)
In [26]: z = x.unsqueeze(dim = 1)
In [27]: print(x)
tensor([1., 2., 3., 4.])
In [28]: print(y)
tensor([[1., 2., 3., 4.]])
In [29]: print(z)
tensor([[1.],
[2.],
[3.],
[4.]])
# 降低维度 (逆操作)
In [30]: x=torch.Tensor([[[1,2,3],[4,5,6]]])
In [31]: print(x,x.shape)
tensor([[[1., 2., 3.],
[4., 5., 6.]]]) torch.Size([1, 2, 3])
In [35]: print(y,y.shape)
tensor([[1., 2., 3.],
[4., 5., 6.]]) torch.Size([2, 3])
机器学习
监督学习
利用一组带标签的数据,学习从输入到输出的映射关系 f ,然后将学习到的映射关系 f 应用到未知的数据上,来预测类别或者数值。 监督学习分为 分类任务 和 回归任务。
对于监督学习,“数据和特征决定了机器学习的上限,而模型和算法知识逼近这个上限而已。” 在监督学习中,最关键的是数据收集和特征处理。
半监督学习
由于现实生活中缺乏的不是数据,而是带有标签的数据,而打标签的过程是非常耗时耗力的。半监督学习是利用少量带有标签的数据进行学习。然后加上人工干涉,即对预测结果进行人工的审核和确认,若预测错误则再次加入到数据集中进行模型训练。
无监督学习
学习的数据没有任何标签,相反,学习的目的是为了标识出数据内在的规律个结构。常见的无监督学习包括关联规则分析、数据降维、聚类算法、词嵌入等。
强化学习
模仿生物体进行学习的方式,用试错学会在给定的情境下选择最恰当的行为。每个动作会有不同的回报,通过不断地试错,总结出获得最大回报的方法。
机器学习的常见概念
缺失值处理
一组数据中往往有多维的信息,如果其中某几个维度的值出现缺失,处理方法有:
- 直接删除数据(如果数据量较大的话)
- 利用均值或中位数进行填充
数据标准化
由于数据集中不同维度的数据要放在一起参与运算,而不同数据的量级和量纲均是不同的,所以不做处理就参与运算数学上解释不通并且大数据的波动容易影响模型的稳定性。于是事先对数据做出标准化是很有必要的。
常见的标准化方法有 Z-Score 和 Min-Max
Z-score 的数学描述是,对序列x1,x2,…xn进行如下变换:
y
i
=
x
i
−
x
′
S
y_i = \frac{x_i - x'}{S}
yi=Sxi−x′
其中x’为xi的均值,
S
=
1
n
−
1
∑
i
=
1
n
(
x
i
−
x
′
)
2
2
S = \sqrt[2]{{\frac{1}{n-1}}{\sum_{i=1}^n (x_i-x')^2}}
S=2n−11i=1∑n(xi−x′)2
计算产生的新序列 y1,y2,…yn服从均值为0,方差为1的分布,并且没有量纲。S中除以 n-1 是因为样本对方差进行估计是有偏估计,而除以n-1是对这个误差的修正,这个结论可以在数学中得到验证。
Min-Max方法:
y
i
=
x
i
−
m
i
n
{
x
j
}
m
a
x
{
x
j
}
−
m
i
n
{
x
j
}
y_i = \frac{x_i-min\{x_j\}} {max\{x_j\} - min\{x_j\} }
yi=max{xj}−min{xj}xi−min{xj}
交叉验证
简单交叉验证:
从数据集中随机选出一部分作为训练集,剩下的作为测试集来检验训练的效果
K折交叉验证:
将全部数据集划分为K个不相交的子集,每次选出其中一个作为测试集。好处是避免了随机化带来的影响,准确率会提高,而坏处是训练的时间增加。
过拟合与欠拟合
欠拟合是指模型对于数据的特征把握不好,没有能理解数据的内在规律,所以验证的时候往往会出现问题。
过拟合是指对于数据特征的把握过于到位,但是问题在于模型过于依赖于数据集,泛化能力于是就弱。