在上海做兼职去哪个网站搜索,镇江新区,莱芜区招聘信息,永康市建设局网站为什么打不开自我学习 Deep Learning and Unsupervised Feature Learning Tutorial Solutions 1.先训练稀疏自编码器提取特征#xff0c;再把特征和label给softmax分类器进行训练#xff0c;最后用test数据集进行测试。 2.由于实际应用中找到大量有标注的样本是非常困难的#xff0c;所…
自我学习 Deep Learning and Unsupervised Feature Learning Tutorial Solutions 1.先训练稀疏自编码器提取特征再把特征和label给softmax分类器进行训练最后用test数据集进行测试。 2.由于实际应用中找到大量有标注的样本是非常困难的所有采用先用大量无标注样本来进行无监督训练自编码器再用自编码器来提取特征配合有标注样本来进行有监督训练softmax分类器。 3.数据预处理方面例如如果对未标注数据集进行PCA预处理就必须将得到的矩阵 U 保存起来并且应用到有标注训练集和测试集上而不能使用有标注训练集重新估计出一个不同的矩阵 U 也不能重新计算均值并做均值标准化否则的话可能得到一个完全不一致的数据预处理操作导致进入自编码器的数据分布迥异于训练自编码器时的数据分布。 4.自学习(self-taught learning) 不要求未标注数据 x_u 和已标注数据 x_l 来自同样的分布。另外一种带限制性的方式也被称为半监督学习它要求 x_u和 x_l 服从同样的分布。 流程图 稀疏自编码器学习图像特征实现自学习—用到无标签的样本集 softmax回归对样本分类—用到有标签的训练样本集 步骤 第0步设置神经网络的结构
该神经网络包括三层 输入层的神经元个数数字识别则设置输入的图像大小 输出端的神经元个数也就是类别数 隐藏层神经元个数 另外一些关于系数编码的参数 sparsityParam、lambda、beta 最大迭代次数maxIter
第一步产生无标签样本集和有标签样本集训练数据集和测试数据集
1导入数据集mnistData和mnistLabels mnistData是一个矩阵每一列为一个输入样本也就是一个输入的数字图像所有像素点按列排布 mnistLabels是一个向量它存储的数字表示mnistData中每一列样本的类别 2将输入的样本集mnistData进行分组 ① 首先将mnistData分为两组一组为有标签的数据集数字0-4的样本另一组为无标签的数据集数字5-9的样本 这两组的指标集分别为labeledSet和unlabeledSet ② 然后再将有标签的数据集平均分为两组一组作为训练集、一组作为测试集 这两组的指标集分别为trainSet和testSet 这里的指标指在mnistData中的列序号 ③ 分别得到上述三组指标集得到相应的数据集并得到有标签数据集的标签 unlabeledData无标签数据集每一列为一个样本 trainData有标签训练集每一列为一个样本相应的标签存放在trainLabels中 testData有标签测试集每一列为一个样本相应的标签存放在testLabels中
用29404个无标注数据unlabeledData手写数字数据库MNIST Dataset中数字为5-9的数据来训练稀疏自动编码器得到其权重参数opttheta。这一步的目的是提取这些数据的特征虽然我们不知道它提取的究竟是哪些特征当然可以通过可视化结果看出来可假设其提取的特征为Features但是我们知道它提取到的特征实际上就是已训练好的稀疏自动编码器的隐藏层的激活值即第2层激活值。注意本节所有训练稀疏自动编码器的算法用的都L-BFGS算法。
第二步训练稀疏自编码器
利用无标签数据集unlabeledData训练稀疏自编码器 ① 初始化化自编码器的参数theta ② 调用minFunc中的最优化函数计算得到稀疏自编码器的参数 包括设置minFunc函数的一些参数及对minFunc函数的调用这里要用到稀疏自编码器的代价函数和梯度计算的函数sparseAutoencoderCost
第三步利用稀疏自编码器对有标签的训练样本集和测试样本集提取特征
在得到稀疏自编码器后可以利用它从有标签的数据集中提取图像特征这里需要完成feedForwardAutoencoder.m函数 所谓图像的特征其实就是指该图像在稀疏自编码器的权值矩阵W1作用下得到的隐藏层的输出 可以得到训练集的特征trainFeatures和测试集的特征testFeatures 它们的每一列分别是由稀疏自编码器提取出的特征
训练样本集提取特征
把15298个已标注数据trainData手写数字数据库MNIST Dataset中数字为0-4的前一半数据作为训练数据集通过这个已训练好的稀疏自动编码器即权重参数为opttheta的稀疏自动编码器就可提取出跟上一步一样的相同的特征参数这里trainData提取的特征表达假设为trainFeatures它其实也是隐藏层的激活值。如果还不明白这里打一个比方假设上一步提取的是一个通信信号A(对应unlabeledData)的特征是一阶累积量而这一步提取的就是通信信号B对应trainData的一阶累积量它们提取的都是同样的特征只是对象不同而已。同样地unlabeledData和trainData提取的是同样的特征Features只是对象不同而已。
测试样本集提取特征
把15298个已标注数据testData手写数字数据库MNIST Dataset中数字为0-4的后一半数据作为测试数据集通过这个已训练好的稀疏自动编码器即权重参数为opttheta的稀疏自动编码器就可提取出跟上一步一样的相同的特征参数这里testData提取的特征表达假设为testFeatures它其实也是隐藏层的激活值。 注意如果上一步对unlabeledData做了预处理一定要把其各种数据预处理参数比如PCA中主成份U保存起来因为这一步的训练数据集trainData和下一步的测试数据集testData也一定要做相同的预处理。本节练习因为用的是手写数字数据库MNIST Dataset已经经过了预处理所以不用再预处理。 具体见http://ufldl.stanford.edu/wiki/index.php/%E8%87%AA%E6%88%91%E5%AD%A6%E4%B9%A0 第四步利用训练样本集训练softmax回归模型
利用训练集的特征集trainFeatures及其标签集trainLabels训练softmax回归模型 注softmaxTrain函数的输入参数特征维数标签数惩罚项权值λ训练数据集的数据训练数据集的标签其他参数
把第三步提取出来的特征trainFeatures和已标注数据trainData的标签trainLabels作为输入来训练softmax分类器得到其回归模型softmaxModel。
第五步对测试数据集进行分类 利用得到的softmax回归模型对测试集进行分类
把第三步提取出来的特征testFeatures输入训练好的softmax回归模型softmaxModel从而预测出已标注数据testData的类别pred再把pred和已标注数据testData本来的标签testLabels对比就可得出正确率。
综上Self-taught learning是利用未标注数据用无监督学习来提取特征参数然后用有监督学习和提取的特征参数来训练分类器。
本次实验主要是进行0~4这5个数字的分类虽然进行无监督训练用的是数字5~9的训练样本这依然不会影响后面的结果。 5-9的数字进行降维的训练训练出一般图像到低维空间的表示矩阵W和B,后用W和B,将要分类的图像0-4用低维表示.
%% CS294A/CS294W Self-taught Learning Exercise% Instructions
% ------------
%
% This file contains code that helps you get started on the
% self-taught learning. You will need to complete code in feedForwardAutoencoder.m
% You will also need to have implemented sparseAutoencoderCost.m and
% softmaxCost.m from previous exercises.
%
%%
% STEP 0: Here we provide the relevant parameters values that will
% allow your sparse autoencoder to get good filters; you do not need to
% change the parameters below.
% 该神经网络包括三层
% 输入层的神经元个数数字识别则设置输入的图像大小
% 输出端的神经元个数也就是类别数
% 隐藏层神经元个数
% 另外一些关于系数编码的参数
% sparsityParam、lambda、beta
% 最大迭代次数maxIter% 设置神经网络的相关参数
inputSize 28 * 28;%样本特征维数
numLabels 5;%样本类别
hiddenSize 200;%隐藏层神经元个数
sparsityParam 0.1; % desired average activation of the hidden units.% (This was denoted by the Greek alphabet rho, which looks like a lower-case p,% in the lecture notes).
lambda 3e-3; % weight decay parameter
beta 3; % weight of sparsity penalty term
maxIter 400; %最大迭代步数%% % 第一步产生无标签样本集和有标签样本集训练数据集和测试数据集
% 1导入数据集mnistData和mnistLabels
% mnistData是一个矩阵每一列为一个输入样本也就是一个输入的数字图像所有像素点按列排布
% mnistLabels是一个向量它存储的数字表示mnistData中每一列样本的类别
% 2将输入的样本集mnistData进行分组
% ① 首先将mnistData分为两组一组为有标签的数据集数字0-4的样本另一组为无标签的数据集数字5-9的样本
% 这两组的指标集分别为labeledSet和unlabeledSet
% ② 然后再将有标签的数据集平均分为两组一组作为训练集、一组作为测试集
% 这两组的指标集分别为trainSet和testSet
% 这里的指标指在mnistData中的列序号
% ③ 分别得到上述三组指标集得到相应的数据集并得到有标签数据集的标签
% unlabeledData无标签数据集每一列为一个样本
% trainData有标签训练集每一列为一个样本相应的标签存放在trainLabels中
% testData有标签测试集每一列为一个样本相应的标签存放在testLabels中 % STEP 1: Load data from the MNIST database
%
% This loads our training and test data from the MNIST database files.
% We have sorted the data for you in this so that you will not have to
% change it.% Load MNIST database files
addpath mnist/ %MNIST数据集及其相关操作函数均在此文件夹中
mnistData loadMNISTImages(mnist/train-images.idx3-ubyte);
mnistLabels loadMNISTLabels(mnist/train-labels.idx1-ubyte);% Set Unlabeled Set (All Images)% 无标签样本集和有标签样本集的指标集将整个数据集分为无标签样本集和有标签样本集
% Simulate a Labeled and Unlabeled set
labeledSet find(mnistLabels 0 mnistLabels 4);%返回mnistLabels中元素值大于等于0且小于等于4的数字的行号
unlabeledSet find(mnistLabels 5);% 训练数据集和测试数据集的指标集有标签数据集再分为两部分训练数据集和测试数据集
numTrain round(numel(labeledSet)/2);%训练样本个数
trainSet labeledSet(1:numTrain);%训练样本集
testSet labeledSet(numTrain1:end);%测试样本集% 无标记样本集的数据
unlabeledData mnistData(:, unlabeledSet);% 训练数据集的数据和标签
trainData mnistData(:, trainSet);% mnistData中大于等于0且小于等于4的数字的前一半数字作为有标签的训练数据
trainLabels mnistLabels(trainSet) 1; % Shift Labels to the Range 1-5% 测试数据集的数据和标签
testData mnistData(:, testSet);% mnistData中大于等于0且小于等于4的数字的后一半数字作为有标签的测试数据
testLabels mnistLabels(testSet) 1; % Shift Labels to the Range 1-5% Output Some Statistics
fprintf(# examples in unlabeled set: %d\n, size(unlabeledData, 2));
fprintf(# examples in supervised training set: %d\n\n, size(trainData, 2));
fprintf(# examples in supervised testing set: %d\n\n, size(testData, 2));%% % 第二步训练稀疏自编码器
% 利用无标签数据集unlabeledData训练稀疏自编码器
% ① 初始化化自编码器的参数theta
% ② 调用minFunc中的最优化函数计算得到稀疏自编码器的参数
% 包括设置minFunc函数的一些参数及对minFunc函数的调用这里要用到稀疏自编码器的代价函数和梯度计算的函数sparseAutoencoderCost% STEP 2: Train the sparse autoencoder
% This trains the sparse autoencoder on the unlabeled training
% images. % 按均匀分布随机初始化theta参数, 初始化化自编码器的参数theta
% Randomly initialize the parameters
theta initializeParameters(hiddenSize, inputSize);%% ----------------- YOUR CODE HERE ----------------------
% Find opttheta by running the sparse autoencoder on
% unlabeledTrainingImages
% 利用L-BFGS算法用无标签数据集来训练稀疏自动编码器% 利用无标签样本集对稀疏自编码器进行学习
%利用优化函数这里要用到minFunc文件夹下的优化函数和sparseAutoencoder文件夹下的sparseAutoencoderCost函数
addpath minFunc/
addpath sparseAutoencoder/
opttheta theta; % 优化函数的一些参数设置
options.Method lbfgs; % Here, we use L-BFGS to optimize our cost% function. Generally, for minFunc to work, you% need a function pointer with two outputs: the% function value and the gradient. In our problem,% sparseAutoencoderCost.m satisfies this.
options.maxIter 400; % Maximum number of iterations of L-BFGS to run
options.display on;
% 调用优化函数得到opttheta即为稀疏自编码器的所有权值构成的向量
[opttheta, cost] minFunc( (p) sparseAutoencoderCost(p, ...inputSize, hiddenSize, ...lambda, sparsityParam, ...beta, unlabeledData), ...theta, options);%% -----------------------------------------------------% Visualize weights
W1 reshape(opttheta(1:hiddenSize * inputSize), hiddenSize, inputSize);
display_network(W1);%%
% 第三步利用稀疏自编码器对有标签的训练样本集和测试样本集提取特征
% 在得到稀疏自编码器后可以利用它从有标签的数据集中提取图像特征这里需要完成feedForwardAutoencoder.m函数
% 所谓图像的特征其实就是指该图像在稀疏自编码器的权值矩阵W1作用下得到的隐藏层的输出
% 可以得到训练集的特征trainFeatures和测试集的特征testFeatures
% 它们的每一列分别是由稀疏自编码器提取出的特征
%% STEP 3: Extract Features from the Supervised Dataset
% You need to complete the code in feedForwardAutoencoder.m so that the
% following command will extract features from the data.
% 利用稀疏自编码器提取训练样本集中所有样本的特征
trainFeatures feedForwardAutoencoder(opttheta, hiddenSize, inputSize, ...trainData);
% 利用稀疏自编码器提测试练样本集中所有样本的特征
testFeatures feedForwardAutoencoder(opttheta, hiddenSize, inputSize, ...testData);%%
% 第四步利用训练样本集训练softmax回归模型
% 利用训练集的特征集trainFeatures及其标签集trainLabels训练softmax回归模型
% 注softmaxTrain函数的输入参数特征维数标签数惩罚项权值λ训练数据集的数据训练数据集的标签其他参数
%% STEP 4: Train the softmax classifiersoftmaxModel struct;
%% ----------------- YOUR CODE HERE ----------------------
% Use softmaxTrain.m from the previous exercise to train a multi-class
% classifier.
% 利用L-BFGS算法用从有标签训练数据集中提取的特征及其标签训练softmax回归模型% Use lambda 1e-4 for the weight regularization for softmaxlambda 1e-4;
inputSize hiddenSize;
numClasses numel(unique(trainLabels));%unique为找出向量中的非重复元素并进行排序% You need to compute softmaxModel using softmaxTrain on trainFeatures and
% trainLabelsaddpath Softmax_Regression/
options.maxIter 100;
softmaxModel softmaxTrain(inputSize, numLabels, lambda, ...trainData, trainLabels, options); function [activation] feedForwardAutoencoder(theta, hiddenSize, visibleSize, data)
% 该函数的作用是利用稀疏自编码器从数据中提取特征
% theta: trained weights from the autoencoder
% visibleSize: the number of input units (probably 64)
% hiddenSize: the number of hidden units (probably 25)
% data: Our matrix containing the training data as columns. So, data(:,i) is the i-th training example. % We first convert theta to the (W1, W2, b1, b2) matrix/vector format, so that this
% follows the notation convention of the lecture notes. W1 reshape(theta(1:hiddenSize*visibleSize), hiddenSize, visibleSize);
b1 theta(2*hiddenSize*visibleSize1:2*hiddenSize*visibleSizehiddenSize);%% ---------- YOUR CODE HERE --------------------------------------
% Instructions: Compute the activation of the hidden layer for the Sparse Autoencoder.
activationsigmoid(W1*datarepmat(b1,1,size(data,2)));%-------------------------------------------------------------------end%-------------------------------------------------------------------
% Heres an implementation of the sigmoid function, which you may find useful
% in your computation of the costs and the gradients. This inputs a (row or
% column) vector (say (z1, z2, z3)) and returns (f(z1), f(z2), f(z3)). function sigm sigmoid(x)sigm 1 ./ (1 exp(-x));
end参考文献 UFLDL教程五之self-taught learning
Deep Learning 7_深度学习UFLDL教程Self-Taught Learning_Exercise斯坦福大学深度学习教程
自我学习
Deep Learning 6_深度学习UFLDL教程Softmax Regression_Exercise斯坦福大学深度学习教程
吴恩达 Andrew Ng 的公开课