花都网站推广,北京和隆优化招聘,新塘网站建设,顺德水利和国土建设局网站为了方便后续练习的展开#xff0c;我们尝试自己创建一个数据生成器#xff0c;用于自主生成一些符合某些条件、具备某些特性的数据集。
导入相关的包
# 随机模块
import random# 绘图模块
import matplotlib as mpl
import matplotlib.pyplot as plt# 导入numpy
import nu…为了方便后续练习的展开我们尝试自己创建一个数据生成器用于自主生成一些符合某些条件、具备某些特性的数据集。
导入相关的包
# 随机模块
import random# 绘图模块
import matplotlib as mpl
import matplotlib.pyplot as plt# 导入numpy
import numpy as np# 导入pytorch
import torch
from torch import nn, optim
import torch.nn.functional as F
from torch.utils.data import Dataset, TensorDataset, DataLoader以上均为此前用到的包其它新的包将在使用时再进行导入及介绍。 46 回归类数据集创建方法
46.1 手动生成数据
回归类模型的数据特征和标签都是连续性数值。 正常情况应该是对于连续型数值标签的预测我们采用回归类模型此处因为先生成数据后进行建模因此我们称可用于回归模型训练的数据为回归类模型数据分类模型数据亦然。 数据生成
生成两个特征、存在偏差自变量和因变量存在线性关系的数据集
num_inputs 2 # 两个特征
num_examples 100 # 总共一千条数据然后通过线性方程确定自变量和因变量的真实关系
torch.manual_seed(420) # 设置随机数种子# 线性方程系数
w_true torch.tensor([2, -1]).reshape(2, 1)
b_true torch.tensor(1.)# 特征和标签取值
features torch.randn(num_examples, num_inputs)
labels_true torch.mm(features, w_true) b_true
labels labels_true torch.randn(size labels_true.shape) * 0.01此处设置所有的数据都是浮点型。 注意此时labels_true和features满足严格意义上的线性方程关系 y 2 x 1 − x 2 1 y 2x_1-x_21 y2x1−x21 但我们实际使用的标签labels则是在labels_true的基础上增添了一个扰动项torch.randn(size labels_true.shape) * 0.01这其实也符合我们一般获取数据的情况真实客观世界或许存在某个规律但我们搜集到的数据往往会因为各种原因存在一定的误差无法完全描述真实世界的客观规律这其实也是模型误差的来源之一另一个误差来源是模型本身捕获规律的能力。这其中 y 2 x 1 − x 2 1 y2x_1-x_21 y2x1−x21相当于我们从上帝视角创建的数据真实服从的规律而扰动项则相当于人为创造的获取数据时的误差。 这种按照某种规律生成数据、又 人为添加扰动项 的创建数据的方法也是数学领域创建数据的一般方法。 数据探索
features[: 10]
# output :
tensor([[-0.0070, 0.5044],[ 0.6704, -0.3829],[ 0.0302, 0.3826],[-0.5131, 0.7104],[ 1.8092, 0.4352],[ 2.6453, 0.2654],[ 0.9235, -0.4376],[ 2.0182, 1.3498],[-0.2523, -0.0355],[-0.0646, -0.5918]])labels[: 10]
# output :
tensor([[ 0.4735],[ 2.7285],[ 0.6764],[-0.7537],[ 4.1722],[ 6.0236],[ 3.2936],[ 3.6706],[ 0.5282],[ 1.4557]])plt.subplot(121)
plt.scatter(features[:, 0], labels) # 第一个特征和标签的关系
plt.subplot(122)
plt.scatter(features[:, 1], labels) # 第二个特征和标签的关系不难看出两个特征和标签都存在一定的线性关系并且跟特征的系数绝对值有很大关系。当然若要增加线性模型的建模难度可以增加扰动项的数值比例从而削弱线性关系。
# 设置随机数种子
torch.manual_seed(420)# 修改因变量
labels1 labels_true torch.randn(size labels_true) * 2# 可视化展示# 扰动较小的情况
plt.subplot(221)
plt.scatter(features[:, 0], labels) # 第一个特征和标签的关系
plt.subplot(222)
plt.plot(features[:, 1], labels, ro) # 第二个特征和标签的关系# 扰动较大的情况
plt.subplot(223)
plt.scatter(features[:, 0], labels1) # 第一个特征和标签的关系
plt.subplot(224)
plt.plot(features[:, 1], labels1, yo) # 第二个特征和标签的关系当然我们也能生成非线性关系的数据集此处我们创建满足 y x 2 1 yx^21 yx21规律的数据集。
# 设置随机数种子
torch.manual_seed(420) num_inputs 2 # 两个特征
num_examples 1000 # 总共一千条数据# 线性方程系数
w_true torch.tensor(2.)
b_true torch.tensor(1.)# 特征和标签取值
features torch.randn(num_examples, num_inputs)
labels_true torch.pow(features, 2) * w_true b_true
labels labels_true torch.randn(size labels_true.shape) * 0.1# 可视化展示
plt.scatter(features, labels)46.2 创建生成回归类数据的函数
为了方便后续使用我们将上述过程封装在一个函数内
定义创建函数
def tensorGenReg(num_examples 1000, w [2, -1, 1], bias True, deg 1):回归类数据集创建函数。:param num_examples: 创建数据集的数据量:param w: 包括截距的如果存在特征系数向量:param bias是否需要截距:param delta扰动项取值:param deg方程次数:return: 生成的特征张量和标签张量if bias True:num_inputs len(w) - 1features_true torch.randn(num_examples, num_inputs)w_true torch.tensor(w[:-1]).reshape(-1, 1).float()b_true torch.tensor(w[-1]).float()if num_inputs 1:# 若输入特征只有1个则不能使用矩阵乘法labels_true torch.pow(features_true, deg) * w_true n_trueelse:labels_true torch.mm(torch.pow(features_true, deg), w_true) b_true# 在特征张量的最后添加一列全是1的列features torch.cat((features_true, torch.ones(len(features_true), 1)), 1)else:num_inputs len(w)features torch.randn(num_examples, num_inputs)w_true torch.tensor(w).reshape(-1, 1).float()if num_inputs 1:labels_true torch.pow(features, deg) * w_trueelse:labels_true torch.mm(torch.pow(features, deg), w_true)labels labels_true torch.randn(size labels_true.shape) * deltareturn features, labels 测试函数性能
首先查看扰动项较小的时候的数据情况
# 设置随机数种子
torch.manual_seed(420) # 扰动项取值为0.01
f, l tensorGenReg(delta 0.01)
f
# output :
tensor([[-0.0070, 0.5044, 1.0000],[ 0.6704, -0.3829, 1.0000],[ 0.0302, 0.3826, 1.0000],...,[-0.9164, -0.6087, 1.0000],[ 0.7815, 1.2865, 1.0000],[ 1.4819, 1.1390, 1.0000]])# 绘制图像查看结果
plt.subplot(223)
plt.scatter(f[:, 0], l) # 第一个特征和标签的关系
plt.subplot(224)
plt.scatter(f[:, 1], l) # 第二个特征和标签的关系然后查看扰动项较大时数据情况
# 设置随机数种子
torch.manual_seed(420) # 扰动项取值为2
f, l tensorGenReg(delta 2)# 绘制图像查看结果
plt.subplot(223)
plt.scatter(f[:, 0], l) # 第一个特征和标签的关系
plt.subplot(224)
plt.scatter(f[:, 1], l) # 第二个特征和标签的关系当特征和标签满足二阶关系时候数据表现
# 设置随机数种子
torch.manual_seed(420) # 2阶方程
f, l tensorGenReg(deg 2)# 绘制图像查看结果
plt.subplot(223)
plt.scatter(f[:, 0], l) # 第一个特征和标签的关系
plt.subplot(224)
plt.scatter(f[:, 1], l) # 第二个特征和标签的关系当只有一个特征时数据表现
# 设置随机数种子
torch.manual_seed(420) # 2阶方程
f, l tensorGenReg(w [1], deg 2, bias False)
plt.scatter(f, l)47 分类数据集创建方法
和回归模型的数据不同分类模型数据的标签是离散值。
47.1 手动创建分类数据集 数据生成
在尝试创建分类数据集之前首先回顾torch.normal创建某种服从正态分布的随机数的创建方法。
torch.randn(4, 2)
# output :
tensor([[ 1.4000, 0.3924],[-0.0695, -1.7610],[ 0.3227, 1.7285],[-0.1107, -1.6273]])torch.normal(4, 2, size(10,2))
# output :
tensor([[4.8092, 0.9773],[4.4092, 3.3987],[1.7446, 6.2281],[3.0095, 4.2286],[7.8873, 6.5354],[3.9286, 4.0315],[2.0309, 4.5259],[3.6491, 0.7394],[3.6549, 5.4767],[8.5935, 3.0440]])接下来尝试创建一个拥有两个特征的三分类的数据集每个类别包含500条数据并且第一个类别的两个特征都服从均值为4、标准差为2的正态分布第二个类别的两个特征都服从均值为-2、标准差为2的正态分布第三个类别的两个特征都服从均值为-6、标准差为2的正态分布创建过程如下:
# 设置随机数种子
torch.manual_seed(420)# 创建初始标记值
num_inputs 2
num_examples 500# 创建自变量簇
data0 torch.normal(4, 2, size(num_examples, num_inputs))
data1 torch.normal(-2, 2, size(num_examples, num_inputs))
data2 torch.normal(-6, 2, size(num_examples, num_inputs))# 创建标签
label0 torch.zeros(500)
label1 torch.ones(500)
label2 torch.full_like(label1, 2)# 合并生成最终数据
features torch.cat((data0, data1, data2)).float()
labels torch.cat((label0, label1, label2)).long().reshape(-1, 1)数据探索
features[: 10]
# output :
tensor([[3.9859, 5.0089],[5.3407, 3.2343],[4.0605, 4.7653],[2.9738, 5.4208],[7.6183, 4.8705],[9.2907, 4.5307],[5.8470, 3.1249],[8.0364, 6.6997],[3.4954, 3.9290],[3.8709, 2.8165]])labels[: 10]
# output :
tensor([[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]])# 可视化展示
plt.scatter(features[:, 0], features[:, 1], c labels)能够看出类别彼此交叉情况较少分类器在此数据集上会有不错表现。当然若要增加分类器的分类难度可以将各类的均值压缩并增加方差从而增加从二维图像上来看彼此交错的情况。
# 设置随机数种子
torch.manual_seed(420) # 创建初始标记值
num_inputs 2
num_examples 500# 创建自变量簇
data0 torch.normal(3, 2, size(num_examples, num_inputs))
data1 torch.normal(0, 2, size(num_examples, num_inputs))
data2 torch.normal(-3, 2, size(num_examples, num_inputs))# 创建标签
label0 torch.zeros(500)
label1 torch.ones(500)
label2 torch.full_like(label1, 2)# 合并生成最终数据
features1 torch.cat((data0, data1, data2)).float()
labels1 torch.cat((label0, label1, label2)).long().reshape(-1, 1)# 可视化展示
plt.subplot(121)
plt.scatter(features[:, 0], features[:, 1], c labels)
plt.subplot(122)
plt.scatter(features1[:, 0], features1[:, 1], c labels1)47.2 创建生成分类数据的函数
同样我们将上述创建分类函数的过程封装为一个函数。这里需要注意的是我们希望找到一个变量可以控制数据整体离散程度也就是后续建模的难以程度。这里我们规定如果每个分类数据集中心点较近、且每个类别的点内部方差较大则数据集整体离散程度较高反之离散程度较低。在实际函数创建过程中我们也希望能够找到对应的参数能够方便进行自主调节。 定义创建函数
def tensorGenCla(num_examples 500, num_inputs 2, num_class 3, deg_dispersion [4, 2], bias False):分类数据集创建函数。 :param num_examples: 每个类别的数据数量:param num_inputs: 数据集特征数量:param num_class数据集标签类别总数:param deg_dispersion数据分布离散程度参数需要输入一个列表其中第一个参数表示每个类别数组均值的参考、第二个参数表示随机数组标准差。:param bias建立模型逻辑回归模型时是否带入截距:return: 生成的特征张量和标签张量其中特征张量是浮点型二维数组标签张量是长正型二维数组。cluster_l torch.empty(num_examples, 1) # 每一类标签张量的形状mean_ deg_dispersion[0] # 每一类特征张量的均值的参考值std_ deg_dispersion[1] # 每一类特征张量的方差lf [] # 用于存储每一类特征张量的列表容器ll [] # 用于存储每一类标签张量的列表容器k mean_ * (num_class - 1) / 2 # 每一类特征张量均值的惩罚因子for i in range(num_class):data_temp torch.normal(i*mean_-k, std_, size(num_examples, num_inputs)) # 生成每一类张量lf.append(data_temp) # 将每一类张量添加到lf中labels_temp torch.full_like(cluster_l, i) # 生成类一类的标签ll.append(labels_temp) # 将每一类标签添加到ll中features torch.cat(lf).float()labels torch.cat(ll).long()if bias True:# 在特征张量中添加一列全是1的列features torch.cat((features, torch.ones(len(features), 1)), 1)return features, labels函数整体结构不复杂且所使用的方法都是此前介绍过的tensor常用方法唯一需要注意的是函数对于分布离散程度的控制。函数内部变量k是一个随着均值增加和分类类别数量增加而增加的数值且分类数量增加对k值增加影响是通过和1取平均后进行惩罚的结果。而i*mean_则是一个随着i增加稳步增量的量二者相减最终能获得一个整体特征均匀分布在0附近的特征张量。 测试函数性能
在使用函数的过程中离散度的第一个数值可以理解为簇的大概分布区间第二个数值可以理解为每个簇的离散程度。
# 设置随机数种子
torch.manual_seed(420) # 创建数据
f, l tensorGenCla(deg_dispersion [6, 2]) # 离散程度较小
f1, l1 tensorGenCla(deg_dispersion [6, 4]) # 离散程度较大# 绘制图像查看
plt.subplot(121)
plt.scatter(f[:, 0], f[:, 1], c l)
plt.subplot(122)
plt.scatter(f1[:, 0], f1[:, 1], c l1)48 创建小批量切分函数
在深度学习建模过程中梯度下降是最常用的求解目标函数的优化方法而针对不同类型、拥有不同函数特性的目标函数所使用的梯度下降算法也各有不同。目前为止我们判断小批量梯度下降MBGD是较为“普适”的优化算法它既拥有随机梯度下降SGD的能够跨越局部最小值点的特性同时又和批量梯度下降BGD一样拥有相对较快的收敛速度虽然速度略慢与BGD。而在小批量梯度下降过程中我们需要对函数进行分批量的切分因此在手动实现各类深度学习基础算法之前我们需要定义数据集小批量切分的函数。
shuffle过程将原序列乱序排列
l list(range(5))
l
# output :
[0, 1, 2, 3, 4]random.shuffle(l)
l
# output :
[3, 2, 0, 1, 4]批量切分函数的目标就是根据设置的“批数”将原数据集随机均匀切分。可通过如下函数实现
def data_iter(batch_size, features, labels):数据切分函数:param batch_size: 每个子数据集包含多少数据:param featurs: 输入的特征张量:param labels输入的标签张量:return l包含batch_size个列表每个列表切分后的特征和标签所组成 num_examples len(features)indics list(range(num_examples))random.shuffle(indices)l[] # 空列表用于存储数据for i in range(0, num_examples, batch_size):j torch.tensor(indices[i: min(i batch_size, num_examples)])l.append([torch.index_select(features, 0, j), torch.index_select(labels, 0, j)])return l# 设置随机数种子
torch.manual_seed(420) # 生成二分类数据集
features, labels tensorGenCla() features[:5]
# output :
tensor([[-4.0141, -2.9911],[-2.6593, -4.7657],[-3.9395, -3.2347],[-5.0262, -2.5792],[-0.3817, -3.1295]])labels
# output :
tensor([[0],[0],[0],...,[2],[2],[2]])l data_iter(10, features, labels)l[0] # 查看切分后的第一个数据集
# output :
[tensor([[ 0.7901, 2.4304],[ 4.0788, 3.7885],[-1.1552, -0.8829],[ 1.3738, 2.3689],[-2.1479, -6.6638],[-2.5418, -7.9962],[-1.0777, -0.7594],[ 5.6215, 3.9071],[ 3.5896, 3.3644],[ 1.2458, 0.0179]]),tensor([[1],[2],[1],[1],[0],[0],[1],[2],[2],[1]])]plt.scatter(l[0][0][:, 0], l[0][0][:, 1], c l[0][1])49 Python模块编写
本节定义的函数将后续课程中将经常使用因此需要将其封装为一个模块方便后续调用。封装为模块有以下几种基本方法
打开文本编辑器将写好并测试完成的函数写入其中并将文本的拓展名改写为.py在spyder或者pycharm中复制相关函数并保存为.py文件
然后将文件保存在jupyter主目录下并取名为torchLearning后续即可通过import torchLearning进行调用。如果是jupyterlab用户也可按照如下方式进行编写
Step 1.打开左侧文件管理栏页点击新建 Step 2.在新建目录中选择Test File
Step 3.在打开的文本编辑器中输入代码
需要保存的函数有
tensorGenReg函数tensorGenCla函数data_iter函数 Step 4.保存退出并将文件名改写为torchLearning.py 然后即可在其他ipy文件中调用具体调用方法见下一节内容。 文章转载自: http://www.morning.ddtdy.cn.gov.cn.ddtdy.cn http://www.morning.bryyb.cn.gov.cn.bryyb.cn http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.jydhl.cn.gov.cn.jydhl.cn http://www.morning.jpjxb.cn.gov.cn.jpjxb.cn http://www.morning.jfymz.cn.gov.cn.jfymz.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.xbckm.cn.gov.cn.xbckm.cn http://www.morning.nkmw.cn.gov.cn.nkmw.cn http://www.morning.zrlwl.cn.gov.cn.zrlwl.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.pmmrb.cn.gov.cn.pmmrb.cn http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.vaqmq.cn.gov.cn.vaqmq.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.xwbwm.cn.gov.cn.xwbwm.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.qbzdj.cn.gov.cn.qbzdj.cn http://www.morning.tngdn.cn.gov.cn.tngdn.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.ddfp.cn.gov.cn.ddfp.cn http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.nwgkk.cn.gov.cn.nwgkk.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.qmwzz.cn.gov.cn.qmwzz.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.rxcqt.cn.gov.cn.rxcqt.cn http://www.morning.xwnnp.cn.gov.cn.xwnnp.cn http://www.morning.rwbh.cn.gov.cn.rwbh.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.syhwc.cn.gov.cn.syhwc.cn http://www.morning.tjqcfw.cn.gov.cn.tjqcfw.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.rwyw.cn.gov.cn.rwyw.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.jgncd.cn.gov.cn.jgncd.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.mhnd.cn.gov.cn.mhnd.cn http://www.morning.kyjyt.cn.gov.cn.kyjyt.cn http://www.morning.gmswp.cn.gov.cn.gmswp.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.yfpnl.cn.gov.cn.yfpnl.cn http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn http://www.morning.tzcr.cn.gov.cn.tzcr.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.nhzxd.cn.gov.cn.nhzxd.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.gagapp.cn.gov.cn.gagapp.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.pcrzf.cn.gov.cn.pcrzf.cn http://www.morning.sxmbk.cn.gov.cn.sxmbk.cn http://www.morning.slfmp.cn.gov.cn.slfmp.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.bqfpm.cn.gov.cn.bqfpm.cn http://www.morning.qxdrw.cn.gov.cn.qxdrw.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.pfnrj.cn.gov.cn.pfnrj.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn