softMax(2)---predict实现
来源:互联网 发布:按option无法切换mac 编辑:程序博客网 时间:2024/05/20 13:37
softMax的cost写好之后,模型也就可以正式开始进行训练了。
首先要把softMaxExercise里边的Debug赋值为false,可以看到,梯度检验这一步在正式训练过程中是完全不需要做的。
softMax模型
softMax的模型是什么样的呢?
假设一共有k个类,那么对于样本
softMax将样本
Train
%% STEP 4: Learning parameters%% Once you have verified that your gradients are correct, % you can start training your softmax regression code using softmaxTrain% (which uses minFunc).options.maxIter = 100;softmaxModel = softmaxTrain(inputSize, numClasses, lambda, ... inputData, labels, options);% Although we only use 100 iterations here to train a classifier for the % MNIST data set, in practice, training for more iterations is usually% beneficial.
训练的过程实质上便是学习模型参数的过程,模型参数包括了各个类的
训练过程是怎样的呢?
来看看softmaxTrain里边是怎么做的吧。
首先初始化模型参数,一般原则是初始化为接近0的值,如下所示,randn生成-1~1直接的随机数,所以theta里边的初始值都是在-0.005~0.005之间的随机值。
theta的大小为numClasses*inputSize,对于第j个类,它的参数
% initialize parameterstheta = 0.005 * randn(numClasses * inputSize, 1);
接着这一段把lbfgs加了进来,lbfgs是一种对函数求解最小值的方法,这个我没看,不过原理大致也是和梯度下降是差不多的吧。
这里我解释下梯度下降的做法。
首先要有个最优化目标,这里便是代价函数
令
求出梯度值之后,接着就是更新参数,
那么就有
根据求取梯度的反方向每次对参数更新step步长。
那什么时候结束更新呢?满足下列条件中的一个即可停止
1.更新前后参数
2.迭代更新次数达到最高迭代次数
softmaxCost的函数便是用来求取
而minFunc则是用来更新
% Use minFunc to minimize the functionaddpath minFunc/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, % softmaxCost.m satisfies this.minFuncOptions.display = 'on';[softmaxOptTheta, cost] = minFunc( @(p) softmaxCost(p, ... numClasses, inputSize, lambda, ... inputData, labels), ... theta, options);
得到最优参数之后,模型便把参数进行了保存。
% Fold softmaxOptTheta into a nicer formatsoftmaxModel.optTheta = reshape(softmaxOptTheta, numClasses, inputSize);softmaxModel.inputSize = inputSize;softmaxModel.numClasses = numClasses;
predict
预测部分是我们需要填写的,这段代码超级简单的哦!
首先从模型中取出参数。
theta = softmaxModel.optTheta; % this provides a numClasses x inputSize matrixpred = zeros(1, size(data, 2));
接下来
% Instructions: Compute pred using theta assuming that the labels start % from 1.Mat = theta*data;[maxValue, pred] = max(Mat);
嗯,就是这么简单。
对于同一个样本
至于theta*data为什么就能计算
Mat中的第i列也就是对第i个样本的k个类别所求取得
实验结果
- softMax(2)---predict实现
- Softmax回归C++实现
- softmax简单实现
- SOFTMAX 的python实现
- softmax(python实现)
- Tensorflow实现softmax回归
- MatLab实现Softmax Regression
- Softmax层的实现
- softmax 代码实现
- TensorFlow实现Softmax
- softmax层的实现
- softmax原理及Matlab实现
- softmax函数的硬件实现
- softmax分类器 python实现
- 使用Tensorflow实现Softmax Regression
- tensorflow 实现一个Softmax Regression
- softmax regression的tensorflow实现
- 第二课 Tensorflow实现softmax
- LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)
- Masonry介绍与使用实践:快速上手Autolayout
- hdu5504 GT and sequence(BestCoder Round #60 )
- hdu5505(好题)
- 两个线程按特定时间顺序交替执行
- softMax(2)---predict实现
- hdu1251 统计难题 字典树
- 基站定位属性
- 网络获取json解析
- Linux vi指令详解
- Winpcap 链接 VS2013 小提示
- Insert & delte elements in doublely linked list
- JDBC-Java程序向mysql中插入数据的中文乱码
- Unity3d 协程、调用函数、委托