背景
当目标函数非线性时(比如下图明显需要一条曲线),就需要增加高次项来获得曲线,当特征数量比较多时,增加高次项会使特征数量爆炸式增长。
比如图像识别问题,对50 * 50像素的图像,如果将每个像素作为特征,增加二次特征就会产生约3 * 10 ^ 6个特征(C22500 = 2500 * 2499 / 2)
这种情况下,若要使用普通逻辑回归学习所有特征,计算量就过大了。
此时用神经网络学习这种复杂的非线性假设函数,就比较合适了。
神经网络
首先学习几个术语:
dendrite: 树突(输入)
cell body: 细胞体
nucleus:细胞核
node of ranvier:郎飞氏结
schwann cell:施万细胞
myelin sheath:髓鞘
axon:轴突(输出)
axon terminal:轴突终末
神经网络架构
类似神经元,神经网络的架构如下。重要组成元素有:
input layer:输入层,即第一层;
hidden layer:隐藏层,即中间层;
activation units:激活单元,即隐藏层中的节点;
output layer:输出层,即最后一层,得到最终结果;
注意a0总是为1,只出现在上一层的输入,不出现在下一层的输出。例如,用x0-x4计算出a1-a3:
a的计算如下:
向量化计算
a的计算就可表示为:
a(j) = g(z(j)) = g(θ(j-1) * a(j-1))
若a(j)是sj维向量,那么就要求θ(j-1) 是sj * (sj-1 + 1)的矩阵。
中间层的特征是靠自学的
输入层使用的特征是X的原始特征,隐藏层使用的特征是上一层训练出来的新的特征。
如果只看最后两层,其实和逻辑回归一样。
简单例子
and
or
not
组合出 xnor
多分类问题
相当于one-vs-all方法的扩展,需要几个分类,分别对应几个输出节点即可:
作业
测试题:Neural Networks: Representation
第1题容易做错:
第1个选项:单层感知机无法解决线性不可分问题,XOR就是线性不可分的
第4个选项:神经网络的输出并非概率,输出结果的和不一定为1。
第5题,同时交换θ(1) 和θ(2) 后,交换效果相当于抵消了。
大作业:Multi-class Classification and Neural Networks
lrCostFunction.m
带正则化的逻辑回归代价函数和梯度函数:
function [J, grad] = lrCostFunction(theta, X, y, lambda)
%LRCOSTFUNCTION Compute cost and gradient for logistic regression with
%regularization
% J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using
% theta as the parameter for regularized logistic regression and the
% gradient of the cost w.r.t. to the parameters.
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
%J = 0;
%grad = zeros(size(theta));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
%
% Hint: The computation of the cost function and gradients can be
% efficiently vectorized. For example, consider the computation
%
% sigmoid(X * theta)
%
% Each row of the resulting matrix will contain the value of the
% prediction for that example. You can make use of this to vectorize
% the cost function and</