Coursera 吴恩达Deep Learning 第二周作业逻辑回归 Matlab 实现

时间:2019-02-11
本文章向大家介绍Coursera 吴恩达Deep Learning 第二周作业逻辑回归 Matlab 实现,主要包括Coursera 吴恩达Deep Learning 第二周作业逻辑回归 Matlab 实现使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

由于不太熟悉python,把python的代码复制跑了一遍之后打算用Matlab 写一遍。
原来的python的参照GitHub:https://github.com/Kulbear/deep-learning-coursera/blob/master/Neural Networks and Deep Learning/Logistic Regression with a Neural Network mindset.ipynb

所用的符号基本一致,原理不多赘述。

素材来源,在百度里面搜索了尺寸64✖64的猫的图片(不会爬取网页图片,直接下载了整个网页)。得到1700多张图,手动删掉了500张看上去不像猫的图片之后,还剩1200多张。打算用800张用于训练,剩余的混合其他不是猫的图片一起做测试。(其实剩余的1200张里面还有一些不是猫的图片)。

1 从文件夹读取图片和预处理

找了很多种批量读取文件夹图片的方法,这种刚好和windows批量命名符合,但是再也找不到原博客了。

有的图片用imread()读入只是一个64×64的数组,导致出错,读到一半就终止。if用于判断读入的图片是不是64×64。800张图只读了784张。

files = dir(fullfile('F:\cat\cat_train\','*.jpg'));
lengthFiles = length(files);
train_set_x_o=[];
train_set_x_flatten=[];
for k = 1:200
    Img = imread(strcat('F:\cat\cat_train\',files(k).name));%文件所在路径
    if size(Img,3)==3
       train_set_x_o=[train_set_x_o;Img];
       Img = reshape(Img,size(Img,1)*size(Img,2)*size(Img,3) ,1);
    else 
        continue
    end
       train_set_x_flatten=[train_set_x_flatten,Img];
end
train_set_x=im2double(train_set_x_flatten)/255;
train_set_y=ones(1,size(train_set_x,2));

train_set_x_o用来保存原来的RGB数组,依旧是个三维数组,顺编写了一个查看图片的函数。

function [] = cat_display(train_set_x_o,k)
%cat_dislay 
%   display the photo
    imshow(train_set_x_o(64*(k-1)+1:64*k,:,:))
end

cat_display(train_set_x_o,15)%查看第十五张图片

由于比较懒直接复制了一遍修改路径,把用于测试的图片也读入,写的比较难看

%Read test data
files = dir(fullfile('F:\cat\cat_test\','*.jpg'));
lengthFiles = length(files);
test_set_x_o_1=[];
test_set_x_o_2=[];
test_set_x_flatten_1=[];
test_set_x_flatten_2=[];
for k = 1:100
    Img = imread(strcat('F:\\cat\cat_test\',files(k).name));%文件所在路径
    if size(Img,3)==3
       test_set_x_o_1=[test_set_x_o_1;Img];
       Img = reshape(Img,size(Img,1)*size(Img,2)*size(Img,3) ,1);
    else 
        continue
    end
       test_set_x_flatten_1=[test_set_x_flatten_1,Img];
end
test_set_x_1=im2double(test_set_x_flatten_1)/255;
test_set_y_1=ones(1,size(test_set_x_1,2));

files = dir(fullfile('F:\cat\non_cat_test\','*.jpg'));
lengthFiles = length(files);
for k = 1:100
    Img = imread(strcat('F:\\cat\non_cat_test\',files(k).name));%文件所在路径
    if size(Img,3)==3
       test_set_x_o_2=[test_set_x_o_2;Img];
       Img = reshape(Img,size(Img,1)*size(Img,2)*size(Img,3) ,1);
    else 
        continue
    end
       test_set_x_flatten_2=[test_set_x_flatten_2,Img];
end
test_set_x_2=im2double(test_set_x_flatten_2)/255;
test_set_y_2=zeros(1,size(test_set_x_2,2));

test_set_x_o=[test_set_x_o_1;test_set_x_o_2];
test_set_x=[test_set_x_1,test_set_x_2];
test_set_y=[test_set_y_1,test_set_y_2];

400张是猫的图片读入了395张,400张不是猫的图片读入了396张。
预处理就完成了。

2 算法的各个部分

2-1Helper Functions

sigmoid 函数

function S = sigmoid(Z)
%sigmoid
%Compute the sigmoid of Z
S=1./(1+exp(-Z));
end

初始化参数

用于初始化w和b

function [w,b] = initialize_with_zeros(dim)
%initialize_with_zeros
w=zeros(dim,1);
b=0;

正向传播和反向传播

function [dw,db,cost] = propagate(w,b,X,Y)
%propagate
%propagation forward
m=size(X,2);
A=sigmoid(w'*X+b);
cost=(-1/m)*sum(Y.*log(A)+(1-Y).*log(1-A));
%propagation backward
dw=(1/m)*X*(A-Y)';
db=(1/m)*sum(A-Y);
end

梯度下降

function [w,b,dw,db,costs] = optimize(w,b,X,Y,num_iterations,learningrate)
costs=[];
for k=1:num_iterations
    [dw,db,cost]=propagate(w,b,X,Y);
    w=w-learningrate*dw;
    b=b-learningrate*db;
    if mod(k,100)==0
        costs=[costs,cost];
        disp(strcat('Cost after iterations',32,num2str(k),32,'is',32,num2str(cost)));
    end
end
end

以上就完成了所需函数的编写

3 将已有的函数组合成一个模型

function [costs,w,b,Y_prediction_test] = model(X_train,Y_train, X_test, Y_test,num_iterations,learningrate)
%model
[w,b]=initialize_with_zeros(size(X_train,1));
[w,b,dw,db,costs]=optimize(w,b,X_train,Y_train,num_iterations,learningrate);
 Y_prediction_test = predict(w, b, X_test);
 Y_prediction_train = predict(w, b, X_train);
 %Print Errors
 disp(strcat('test accuracy:',num2str(1-mean(abs(Y_prediction_test-Y_test)))));
 disp(strcat('train accuracy:',num2str(1-mean(abs(Y_prediction_train-Y_train)))));
 
end

编写一个main,运行read读取数据之后就可以直接运行model了

read
[costs,w,b,Y_predition_test] = model(train_set_x,train_set_y,test_set_x,test_set_y,2000,0.005);
plot(costs)