deeplearning.ai 吴恩达网上课程学习(四)——Logistic代码实战,基于Linux,Python 3.4

本文主要是介绍deeplearning.ai 吴恩达网上课程学习(四)——Logistic代码实战,基于Linux,Python 3.4,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

此文章偏向于实践和自己的理解,本文讲述了Python代码的一些基本操作,由浅入深最后实现logistic的代码。(原文中代码实现基于jupyter notebook文中代码实现基于之前搭建的Linux的Python平台二者代码会有细小的区别。

搭建平台的链接深度学习(一):搭建TensorFlow)

原创内容来自:

https://www.missshi.cn/api/view/blog/59aa08fee519f50d04000170


一、使用numpy构建基本函数(numpy是Python在科学计算中最常用的库)

练习1:利用np.exp()实现sigmod函数:

np.exp和math.exp()的区别:

math.exp()只对标量(有些物理量,只具有数值大小,而没有方向)执行,np.exp可用于向量或者矩阵。






所以,具体实现一个真正的、可用于矢量或矩阵的sigmod函数:


sigmod函数,可用于矢量和矩阵

import numpy as np
def sigmod(x):s = 1.0 / (1 + np.exp(-x))return s
x = np.array([0,1, 2, 3])
print sigmod(x) 
#注意最后一步原文中写错了
 [ 0.73105858,  0.88079708,  0.95257413]


练习2:计算sigmod函数的导数

sigmod函数的导数公式:


import numpy as np
def sigmoid_derivative(x):s = 1.0 / (1 + 1 / np.exp(x))ds = s * (1 - s)    return ds     
x = np.array([1, 2, 3])
print "sigmoid_derivative(x) = " + str(sigmoid_derivative(x))

sigmoid_derivative(x) = [0.19661193 0.10499359 0.04517666]

练习3:将一副图像转为为一个向量

在numpy中,有两个常用的函数:np.shape和np.reshape()。其中,X.shape可以用于查看当前矩阵的维度。X.reshape()可以用于修改矩阵的维度或形状。

例如,对于一副彩色图像,其通常是由一个三维矩阵组成的(RGB三个通道),我们通常需要将其转换为一个矢量,其长度为3*length*width。

import numpy as np
def image2vector(image):v = image.reshape((image.shape[0] * image.shape[1] * image.shape[2], 1))return vimage = np.array([[[ 0.67826139,  0.29380381],[ 0.90714982,  0.52835647],[ 0.4215251 ,  0.45017551]],[[ 0.92814219,  0.96677647],[ 0.85304703,  0.52351845],[ 0.19981397,  0.27417313]],[[ 0.60659855,  0.00533165],[ 0.10820313,  0.49978937],[ 0.34144279,  0.94630077]]])print "image2vector(image) = " + str(image2vector(image))



练习4:按行归一化:数据进行归一化后,梯度下降算法的收敛速度会明显加快。

下面对一个矩阵进行按行归一化,归一化后的结果是每一个的长度为1。


import numpy as np
def normalizeRows(x):x_norm = np.linalg.norm(x, axis=1, keepdims = True)  #归一化,见下面函数解释x = x / x_norm  #利用numpy的广播,用矩阵与列向量相除。return xx = np.array([[0, 3, 4],[1, 6, 4]])
print "normalizeRows(x) = " + str(normalizeRows(x))


关于  np.linalg.norm:参考https://blog.csdn.net/hqh131360239/article/details/79061535




练习5:广播的使用及softmax函数的实现

广播能帮助我们对不同维度的矩阵、向量、标量之前快速计算。


import numpy as np
def softmax(x):x_exp = np.exp(x) x_sum = np.sum(x_exp, axis = 1, keepdims = True) s = x_exp/x_sum return sx = np.array([[9, 2, 5, 0, 0],[7, 5, 0, 0 ,0]])
print "softmax(x) = " + str(softmax(x)

# softmax(x) = [[ 9.80897665e-01 8.94462891e-04 1.79657674e-02 1.21052389e-04 1.21052389e-04] [ 8.78679856e-01 1.18916387e-01 8.01252314e-04 8.01252314e-04 8.01252314e-04]]


矢量化:

练习1:L1误差函数的实现


import numpy as np
def L1(yhat, y):loss = np.sum(np.abs(y - yhat))return lossyhat = np.array([.9, 0.2, 0.1, .4, .9])
y = np.array([1, 0, 0, 1, 1])
print "L1 = " + str(L1(yhat,y))
# L1 = 1.1


练习2:L2误差函数的实现


import numpy as np
def L2(yhat, y):loss = np.sum(np.power((y - yhat), 2))return lossyhat = np.array([.9, 0.2, 0.1, .4, .9])
y = np.array([1, 0, 0, 1, 1])
print "L2 = " + str(L2(yhat,y))

# L2 = 0.43

Logistic的实现:

包括:初始化、计算代价函数和梯度、使用梯度下降算法进行优化等并把他们整合成为一个函数。

本实验用于通过训练来判断一副图像是否为猫。

1. 引入相关库文件:


import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage%matplotlib inline  #设置matplotlib在行内显示图片

上面是基于jupyter notebook的代码,在之前搭建的TensorFlow平台不可用。

()如果出现import 库报错,请进行下列的步骤,如果没有,可以跳过:

下面就要安装对应的库:

① 安装matplotlib库,Ctrl+Alt+T键调出终端,输入

sudo apt-get install python-matplotlib  

② 安装h5py的库,Ctrl+Alt+T键调出终端,输入:

sudo apt-get install libhdf5-dev
sudo apt-get install python-h5py

③安装scipy:

sudo apt-get install python-scipy  

%matplotlib inline是jupyter notebook里的命令, 意思是将那些用matplotlib绘制的图显示在页面里而不是弹出一个窗口

此时在py文件中写入代码:

import numpy as np
import h5py
import matplotlib.pyplot as plt
import scipy
from PIL import Image
from scipy import ndimage

2. 读取数据和预处理:

在训练之前,首先要读取数据,数据来源:http://www.missshi.cn/#/books搜索train_catvnoncat.h5和test_catvnoncat.h5进行下载,尊重原创:


代码如下:(下载数据后注意更改数据文件名称(和原文代码有区别)

def load_dataset():train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")  #读取H5文件train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set featurestrain_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labelstest_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set featurestest_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labelsclasses = np.array(test_dataset["list_classes"][:]) # the list of classestrain_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))  #对训练集和测试集标签进行reshapetest_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classestrain_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes = load_dataset()

数据说明:(和原文代码有区别)


index = 25
plt.imshow(train_set_x_orig[index])
plt.show()   #plt.imshow()函数负责对图像进行处理,并显示其格式,而plt.show()则是将plt.imshow()处理后的函数显示出来。 
print "y = " + str(train_set_y_orig[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y_orig[:, index])].decode("utf-8") +  "' picture."

x1 = np.squeeze(x)  # 从数组的形状中删除单维条目,即把shape中为1的维度去掉

关闭图像显示结果:

# y = [1], it's a 'cat' picture.

我们先看看我们得到的是怎么样的数据:(和原文代码有区别)

m_train_x = train_set_x_orig.shape
m_test_x = test_set_x_orig.shape
m_train_y = train_set_y_orig.shape
m_test_y = test_set_y_orig.shape
print "m_train_x" + str(m_train_x)
print "m_test_x" + str(m_test_x)
print "m_train_y" + str(m_train_y)
print "m_test_y" + str(m_test_y)

得到的结果:


也就是我们的训练集train_x有209张照片,每张照片是RGB的,为64*64*3.训练集的train_y是个1*209的list表示的是训练集标签(0或者1);我们的测试集test_x有50张照片,每张照片是RGB的,为64*64*3.测试集的test_y是个1*50的list表示的是测试集标签(0或者1);

我们也可以根据需要计算出训练集的大小、测试集的大小以及图片的大小:

m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]
print (m_train, m_test, num_px)
#结果 209, 50, 64

train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T

3. Logistic的结构:



先粘过来上节课的伪代码流程图:

Step1:实现sigmod函数
def sigmoid(z):s = 1.0 / (1 + 1 / np.exp(z))return s
Step2:初始化参数
def initialize_with_zeros(dim):w = np.zeros((dim, 1))b = 0return w, b

Step3:前向传播与反向传播


def propagate(w, b, X, Y):m = X.shape[1]# FORWARD PROPAGATION (FROM X TO COST)A = sigmoid(np.dot(w.T, X) + b)                                     # compute activationcost = -1.0 / m * np.sum(Y * np.log(A) + (1.0 - Y) * np.log(1.0 - A))                                  # compute cost# BACKWARD PROPAGATION (TO FIND GRAD)dw = 1.0 / m * np.dot(X, (A - Y).T) #整除运算,需要将1改为1.0,否则运算结果全部为0db = 1.0 / m * np.sum(A - Y)cost = np.squeeze(cost)grads = {"dw": dw,"db": db}return grads, cost

Step4:更新参数


def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):costs = []  for i in range(num_iterations): #每次迭代循环一次, num_iterations为迭代次数# Cost and gradient calculation grads, cost = propagate(w, b, X, Y)# Retrieve derivatives from gradsdw = grads["dw"]db = grads["db"]# update rule w = w - learning_rate * dwb = b - learning_rate * db# Record the costsif i % 100 == 0:costs.append(cost)# Print the cost every 100 training examplesif print_cost and i % 100 == 0:print ("Cost after iteration %i: %f" %(i, cost))params = {"w": w,"b": b}grads = {"dw": dw,"db": db}return params, grads, costs

Step5:利用训练好的模型对测试集进行预测:


首先理解下shape这个函数:

比如2*3的矩阵:


def predict(w, b, X):m = X.shape[1]Y_prediction = np.zeros((1,m))w = w.reshape(X.shape[0], 1)# Compute vector "A" predicting the probabilities of a cat being present in the pictureA = sigmoid(np.dot(w.T, X) + b)for i in range(A.shape[1]):# Convert probabilities A[0,i] to actual predictions p[0,i]if A[0][i] > 0.5:Y_prediction[0][i] = 1else:Y_prediction[0][i] = 0return Y_prediction

Step6:将以上功能整合到一个模型中:

def model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False):# initialize parameters with zeros w, b = initialize_with_zeros(X_train.shape[0])# Gradient descentparameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)# Retrieve parameters w and b from dictionary "parameters"w = parameters["w"]b = parameters["b"]# Predict test/train set examples Y_prediction_test = predict(w, b, X_test)Y_prediction_train = predict(w, b, X_train)# Print train/test Errorsprint("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))d = {"costs": costs,"Y_prediction_test": Y_prediction_test, "Y_prediction_train" : Y_prediction_train, "w" : w, "b" : b,"learning_rate" : learning_rate,"num_iterations": num_iterations}return d

Step7:模型测试:

#前边定义的所有函数
。。。此处省略

#数据输入(和原文代码有区别)

train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes = load_dataset()#数据输入train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T  #数据预处理
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).Ttrain_set_x = train_set_x_flatten/255. #归一化
test_set_x = test_set_x_flatten/255.
m_train = train_set_x_orig.shape[0] #获得一些大小参数
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]
#调用模型
d = model(train_set_x, train_set_y_orig, test_set_x, test_set_y_orig, num_iterations = 2000, learning_rate = 0.005, print_cost = True)

注意原博客中数据带入有错误,请自行改正。结果:


下面挑选其中的一些图片来看我们的预测结果:(和原文代码有区别)

index = 14
plt.imshow(test_set_x[:,index].reshape((num_px, num_px, 3)))
plt.show() 
print ("y = " + str(test_set_y_orig[0,index]) + ", you predicted that it is a \"" + classes[int(d["Y_prediction_test"][0,index])].decode("utf-8") +  "\" picture.")

Step8:画出代价函数变化曲线:

costs = np.squeeze(d['costs'])
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations (per hundreds)')
plt.title("Learning rate =" + str(d["learning_rate"]))
plt.show()


Step9:学习速率对最终的结果的影响

learning_rates = [0.01, 0.001, 0.0001]
models = {}
for i in learning_rates:print ("learning rate is: " + str(i))models[str(i)] = model(train_set_x, train_set_y_orig, test_set_x, test_set_y_orig, num_iterations = 1500, learning_rate = i, print_cost = False)print ('\n' + "-------------------------------------------------------" + '\n')for i in learning_rates:plt.plot(np.squeeze(models[str(i)]["costs"]), label= str(models[str(i)]["learning_rate"]))plt.ylabel('cost')
plt.xlabel('iterations')legend = plt.legend(loc='upper center', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.show()

结果:



Step10:用一副你自己的图像,而不是训练集或测试集中的图像进行分类:

## START CODE HERE ## (PUT YOUR IMAGE NAME) 
my_image = "Penguins.jpg"   # change this to the name of your image file 
## END CODE HERE ### We preprocess the image to fit your algorithm.
fname = "image/" + my_image
image = np.array(plt.imread(fname, flatten=False))  #读取图片
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T  #放缩图像
my_predicted_image = predict(d["w"], d["b"], my_image)  #预测plt.imshow(image)
print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") +  "\" picture.")

如果用个cat.jpg:



imread在1.2.0版本用imageio.imread代替

Read an image from a file as an array.从文件中把图片读成数组

This function is only available if Python Imaging Library (PIL) is installed.该功能只在安装了PIL情况下使用

imresize功能将在1.2.0版本中,被skimage.transform.resize取代

Resize an image.调整图片大小

This function is only available if Python Imaging Library (PIL) is installed.该功能只在安装了PIL情况下使用

这篇关于deeplearning.ai 吴恩达网上课程学习(四)——Logistic代码实战,基于Linux,Python 3.4的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/980637

相关文章

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Python版本与package版本兼容性检查方法总结

《Python版本与package版本兼容性检查方法总结》:本文主要介绍Python版本与package版本兼容性检查方法的相关资料,文中提供四种检查方法,分别是pip查询、conda管理、PyP... 目录引言为什么会出现兼容性问题方法一:用 pip 官方命令查询可用版本方法二:conda 管理包环境方法

Linux云服务器手动配置DNS的方法步骤

《Linux云服务器手动配置DNS的方法步骤》在Linux云服务器上手动配置DNS(域名系统)是确保服务器能够正常解析域名的重要步骤,以下是详细的配置方法,包括系统文件的修改和常见问题的解决方案,需要... 目录1. 为什么需要手动配置 DNS?2. 手动配置 DNS 的方法方法 1:修改 /etc/res

Linux创建服务使用systemctl管理详解

《Linux创建服务使用systemctl管理详解》文章指导在Linux中创建systemd服务,设置文件权限为所有者读写、其他只读,重新加载配置,启动服务并检查状态,确保服务正常运行,关键步骤包括权... 目录创建服务 /usr/lib/systemd/system/设置服务文件权限:所有者读写js,其他

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Linux挂载linux/Windows共享目录实现方式

《Linux挂载linux/Windows共享目录实现方式》:本文主要介绍Linux挂载linux/Windows共享目录实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录文件共享协议linux环境作为服务端(NFS)在服务器端安装 NFS创建要共享的目录修改 NFS 配

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注