机器学习 2014斯坦福大学课程: 4-2 神经网络

机器学习 2014斯坦福大学课程: 4-2 神经网络

刚开始学习机器学习,学习的视频教程是coursera网站上吴恩达(Andrew Ng)教授的机器学习课程。
在此梳理并记录总结笔记,供学习交流,欢迎批评指正!

上节回顾

神经网络可以解决回归问题、分类问题
在本例子中,将分类问题的解决方法之一逻辑回归做了改进,用神经网络解决

  1. 构架神经网络(多少层,每层多少单元等)
  2. 通过前向传播算法可以计算出 h Θ ( x ) h_\Theta(x) hΘ(x),即拟合的式子
  3. 通过前向传播算法可以计算代价函数
  4. 通过前向后向传播算法可以计算代价函数偏导数
  5. 选择优化算法来优化代价函数,得到权重 Θ \Theta Θ,再代入到 h Θ ( x ) h_\Theta(x) hΘ(x)可以得到预测的y值

主要代码已经给出,还有一些补充操作

梯度校验

使用数值梯度方法来检验所写的代价函数偏导数是否相同,两者差应该小于1e-9
检验完成后,应该关闭这个梯度检验,因为耗费时间,然后开始训练样本

在这里插入图片描述
对每一个参数,求双向导数

初始化

初始化参数需要随机初始化,不能相同,否则训练出来的每一层单元的值都是一样的

解决思路

  1. 构架神经网络(多少层,每层多少单元等)
  2. 通过前向传播算法可以计算出 h Θ ( x ) h_\Theta(x) hΘ(x),即拟合的式子
  3. 通过前向传播算法可以计算代价函数
  4. 通过前向后向传播算法可以计算代价函数偏导数
  5. 数值计算梯度校验,得到结果后不再执行
  6. 选择优化算法来优化代价函数,得到权重 Θ \Theta Θ,再代入到 h Θ ( x ) h_\Theta(x) hΘ(x)可以得到预测的y值
  7. 预测值与真实值对比,得到准确率等

octave/matlab代码

numericalgradient函数:返回数值梯度
checkgradient函数:做检验,做检验的时候可以选择小部分数据,不全做,因为速度较慢
randinitial函数:随机初始化

function grad_num = numericalgradient(J,theta)
  grad_num=zeros(size(theta));
  grad_purb=zeros(size(theta));
  e=1e-4;
  for i=1:numel(theta)
    grad_purb(i)=e;
    grad_num(i)=(J(theta + grad_purb)-J(theta - grad_purb))/(2*e);
    grad_purb(i)=0;
  end
endfunction
function checkgradient(lambda)

  input_layer_size=3;
  hidden_layer_size=5;
  output_layer_size=3;
  m = 5;
% We generate some 'random' test data
Theta1 = debugInitializeWeights(hidden_layer_size, input_layer_size);
Theta2 = debugInitializeWeights(output_layer_size, hidden_layer_size);
% Reusing debugInitializeWeights to generate X
X  = debugInitializeWeights(m, input_layer_size - 1);
y  = 1 + mod(1:m, output_layer_size)';

nn_para=[Theta1(:) ; Theta2(:)];
  cF=@(p)costfunc(X,y,input_layer_size,hidden_layer_size,output_layer_size,p,lambda);
  [J,grad]=cF(nn_para); 
  grad_num= numericalgradient(cF,nn_para);
  
  fprintf('grad using costfunction and numberical gradient are :\n');
  disp([grad grad_num]);
  diff = norm(grad_num-grad)/norm(grad_num+grad);
  fprintf('The relative difference will be small (less than 1e-9)\n');
  disp(diff);
  
endfunction
function [theta] = randinitial (in,out)
theta=zeros(out,in+1);
eps=0.12
theta=rand(out,in+1)*2*eps-eps;
endfunction

myex:主脚本

clear;close all;clc;

%%determine neural network architecture
input_layer_size=400;
hidden_layer_size=25;
output_layer_size=10;

%%load and display
fprintf('Ex: Handwritten digits using neural network\n');
fprintf('1. Loading data...\n');
load('ex4data1.mat');
m=size(X,1);
n=size(X,2);
fprintf('%d training examples, %d features(20*20) \n',m,n);
fprintf('Display some traning examples:\n');
sel=randperm(m);
sel=sel(1:100);
displayData(X(sel,:));
fprintf('Program paused. Press enter to continue.\n');
pause;

%%check costfunc
fprintf('2. Checking costfunc using given parameters.\n');
load('ex4weights.mat');

nn_para=[Theta1(:);Theta2(:)];
lambda=0;
[cost,grad]=costfunc(X,y,input_layer_size,hidden_layer_size,output_layer_size,nn_para,lambda);
fprintf('when lambda=0 , cost is %f, the right answer is 0.28762 \n',cost);
lambda=1;
[cost,grad]=costfunc(X,y,input_layer_size,hidden_layer_size,output_layer_size,nn_para,lambda);
fprintf('when lambda=1 , cost is %f, the right answer is 0.383770 \n',cost);
fprintf('Program paused. Press enter to continue.\n');
pause;

%%check grad
fprintf('3. Checking gradient using numerical gradient.\n');
fprintf('when lambda=0, ');
lambda=0;
checkgradient(lambda);
fprintf('when lambda=3,  ');
lambda=3;
checkgradient(lambda);
fprintf('Program paused. Press enter to continue.\n');
pause;

%%training neural network...
fprintf('4.Training Neural Network... \n')
initial_Theta1 = randinitial(input_layer_size, hidden_layer_size);
initial_Theta2 = randinitial(hidden_layer_size, output_layer_size);
initial_nn_para = [initial_Theta1(:) ; initial_Theta2(:)];

options = optimset('MaxIter', 50);
lambda = 1;
costFunction = @(p)costfunc(X,y,input_layer_size,hidden_layer_size,output_layer_size,p,lambda);
[nn_para, cost] = fmincg(costFunction, initial_nn_para, options);
theta1=reshape(nn_para(1:hidden_layer_size*(input_layer_size+1)),hidden_layer_size,input_layer_size+1);
theta2=reshape(nn_para(hidden_layer_size*(input_layer_size+1)+1:end),output_layer_size,hidden_layer_size+1);
fprintf('Program paused. Press enter to continue.\n');
pause;

%%visualizing neural network
fprintf('5.Visualizing Neural Network... \n')
displayData(theta1(:, 2:end));
displayData(theta2(:, 2:end));
fprintf('Program paused. Press enter to continue.\n');
pause;

%%predict
fprintf('6.Predict Neural Network... \n')
pred=predict_test(theta1,theta2,X);
fprintf('Training Set Accuracy: %f\n', mean(double(pred == y)) * 100);

%%display wrong 
fprintf('7.Show some wrong indentifications... \n');
getwrongexample(pred,y,X);

python代码

def gradient_checking(nn_para,X,y,l):
    #input_layer_size=3
    #hidden_layer_size=5
    #output_layer_size=10
    def computeNumericalGrad(nn_para,X,y,e):

        gradnum=np.zeros([nn_para.shape[0],1])
        gradpurb=np.zeros([nn_para.shape[0],1])
        nn_para_temp=nn_para.reshape(nn_para.shape[0],1)
        for i in range(nn_para.shape[0]):
            gradpurb[i]=e
            plus=cost(nn_para_temp+gradpurb,X,y,400,25,10,l)
            minus=cost(nn_para_temp-gradpurb,X,y,400,25,10,l)
            gradnum[i]=(plus-minus)/(2*e)
            gradpurb[i]=0
        return gradnum
    e=0.0001
    numgrad=computeNumericalGrad(nn_para,X,y,e)
    grad=back_forward(nn_para,X,y,400,25,10,l)
    grad_temp=grad.reshape(grad.shape[0],1)
    diff=np.linalg.norm(numgrad-grad_temp)/np.linalg.norm(numgrad+grad_temp)
    print("The numerical gradient is:\n")
    print(numgrad)
    print("The back_prop gradient is:\n")
    print(grad_temp)
    print("the relative difference will be smaller than 10e-9 :",diff)

总结

注意python使用矩阵用法区别
在码代码的时候,可以适当草稿,理清每个矩阵的维度

参考

[1] 来自本课程课件和作业题,以及黄海广github上的中文笔记资料等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
神经网络机器学习是两个相关的概念,它们在斯坦福大学发布的相关PDF中得到详细探讨。 神经网络是一种模仿人类神经系统的计算模型,包括大量相互连接的简单单元,通过这些单元之间的信息传递和处理来模拟人脑的学习过程。神经网络可以通过训练来提取数据的特征,并应用于各种任务,例如图像识别、语音识别和自然语言处理等。它的主要优势在于对复杂非线性关系的建模能力以及对海量数据的处理能力。 而机器学习是一种通过算法和模型让计算机从数据中学习,并根据以往的经验来预测和决策的方法。机器学习算法可以通过学习已有的数据模式来自动发现数据之间的规律,并在给定新数据时进行预测或分类。机器学习可分为监督学习、无监督学习和强化学习等不同类型,具体选择哪种方法取决于问题的类型和可用数据。 斯坦福大学的PDF提供了对神经网络机器学习的深入理解和实践指南。通过阅读这份PDF,可以了解到神经网络机器学习的基本原理、算法和应用,以及它们在各个领域的研究成果和前沿动向。该PDF还包含了丰富的示例和实验,可以帮助读者更好地理解和应用这些概念。 总之,神经网络机器学习是现代人工智能领域的重要组成部分,它们在斯坦福大学通过相关PDF提供的详细说明和实践指南对于学习和应用这些技术具有很大的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值