Machine Learning Week 4

Neural Networks

Nonlinear Hypothesis

quadratic features( sum(xixj)=sum(xi)/2 )
There will be too many features if we use the linear hypothesis when there has a lot of original features.

features will be too many

Neural Networks

To mimic human brain

Neuron Model: Logistic Unit

Single Neuron

Single Neuron

Neural Network is a group of single neuron
Input layer - hidden layer - output layer (used to compute the hypothesis function)

Neural network

Notations used in neural network

unit i of layer j

a(j)i

layer j-1, unit i in layer j, unit q in layer j-1,
Θ(j1)iq

Θj=Sj+1(Sj+1)

Vectorised implementation

+a(j)0=1

z(j+1)=Θ(j)a(j)

a(j+1)=g(z(j+1))

Logistic Calculation Using Neural Network

And function

And

Or function

Or

XNor Function

XNor

Multiple output units: One-vs-all


Weekly Matlab Exercise


%lrCostFunction

%theta2=theta(2:size(theta,1),1)
%J=1/m*(-y'*log(sigmoid(X*theta))-(1.-y)'*log(1-sigmoid(X*theta)))+lambda./(2*m)*(theta2'*theta2)
%grad1=(1/m*(sigmoid(X*theta)-y)'*X)'
%theta1=lambda/m*theta
%theta1(1,1)=0
%grad=grad1+theta1
theta2=theta(2:size(theta,1),1)'*theta(2:size(theta,1),1);
J=1/m*(-y'*log(sigmoid(X*theta))-(1.-y)'*log(1-sigmoid(X*theta)))+lambda/(2*m)*theta2;
grad1=1/m*X'*(sigmoid(X*theta)-y)+lambda/m*theta;
grad2=1/m*X'*(sigmoid(X*theta)-y);
grad2(2:end,1)=grad1(2:end,1);
grad=grad2;


%oneVsAll

options=optimset('GradObj','on','MaxIter',50);
for i=1:num_labels
initial_theta=zeros(n+1,1);
i_indices=find(y==i);
theta=fmincg(@(t)(lrCostFunction(t,X,y==i,lambda)),...
             initial_theta,options);
all_theta1(i,:)=theta';
end
all_theta=all_theta1


%predictOneVsAll

[values,indeces]=max(sigmoid(X*all_theta'),[],2)
p=indeces'


%predict

X=[ones(size(X,1),1) X];
A2=sigmoid(Theta1*X');
A2=[ones(1,size(A2,2));A2];
A3=sigmoid(Theta2*A2)
[vals,inds]=max(A3,[],1);
p=inds'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值