吴恩达深度学习第一课Week2编程作业Logistics Regression with a Neural Network mindset

Load the data
Step 1 is to load the data and then you can show the picture with an index.
here’s the code:

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


def load_dataset():
    train_dataset = h5py.File(r'D:\DeepLearning\Week2\week2dataset\datasets\train_catvnoncat.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:])  # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:])  # your train set labels

    test_dataset = h5py.File(r'D:\DeepLearning\Week2\week2dataset\datasets\test_catvnoncat.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:])  # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:])  # your test set labels

    classes = np.array(test_dataset["list_classes"][:])  # the list of classes

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_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, classes

#loading the data cat or not cat
train_set_x_orig,train_set_y_orig,test_set_x_orig,test_set_y_orig,classes=load_dataset()

#you can check the picture
plt.imshow(train_set_x_orig[25])
pylab.show()#here need to add this sentence to show the picture .remeber to import the pylab

**Reshape the data **
Reshape the training and test data ,以便将大小为(m,n,3)的图像展平为单个形状的向量(m×n×3,1)。
把一个维度为(a,b,c,d)的矩阵X展平为(b×c×d,a)的一个技巧是这个语句:
X_flatten = X.reshape(X.shape [0],-1).T
详细的解释可以参考这篇文章:https://blog.csdn.net/tefuirnever/article/details/88919206

now reshape the data:

#reshape the training and test examples
train_set_x_orig=train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T
test_set_x_orig=test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).T

Mean Normalization

#normalization
train_set_x=train_set_x_flatten/255
test_set_x=test_set_x_flatten/255

Need to Remember:
预处理数据集常见的步骤是
1.找出数据集的shape
2.reshape数据集,使每个示例大小都为(m×n×3,1)
3.normalization标准化数据

接下来要做的是:
1.初始化模型参数
2.通过最小化损失来学习模型参数
3.使用学习到的参数进行预测

初始化参数
初始化参数w和b,其中w维度是(m×n×3,1),b=0

#sigmoid
def sigmoid(z):
    return 1/(1+np.exp(-z))

#initialize the parameters w and b with 0
def initialize_with_zeros(dim):
    w=np.zeros((dim,1))
    b=0
    return w,b

前向和后向传播
公式如下:
在这里插入图片描述实现代码:

#gradient decent:propagate
def propagate(w,b,X,Y):
    m=X.shape[1]
    A=sigmoid(np.dot(w.T,X)+b)
    cost=(-1/m)*np.sum(Y*np.log(A)+(1-Y)*np.log(1-A),axis=1)#computer cost
    dw=1/m*np.dot(X,(A-Y).T)
    db=1/m*np.sum(A-Y)
    cost=np.squeeze(cost)#squeeze的作用是去掉维度为1的维,这里是把一个一维的变成一个数字
    return dw,db,cost

优化函数
计算loss function 及其梯度,使用梯度下降来更新参数

def optimize(w,b,X,Y,num_iterations,learning_rate,print_cost=False):
    costs=[]
    for i in range(num_iterations):
        dw,db,cost=propagate(w,b,X,Y)
        w=w-learning_rate*dw
        b=b-learning_rate*db

        if i%100==0:
            costs.append(cost)
        if print_cost and i%100==0:
            print('cost after iteration %i:%f'%(i,cost))
    return w,b,dw,db,costs

预测
根据上一个函数学习到的w和b来预测数据集

def predict(w,b,X):
    m=X.shape[1]
    Y_prediction=np.zeros((1,m))
    w=w.reshape(X.shape[0],1)
    A=sigmoid(np.dot(w.T,X)+b)
    for i in range(A.shape[1]):
        if A[0,i]>=0.5:
            Y_prediction[0,i]=1
        else:
            Y_prediction[0,i]=0
    assert(Y_prediction.shape==(1,m))
    return Y_prediction

将所有功能整合到模型中

def model(X_train,Y_train,X_test,Y_test,num_iterations=2000,learning_rate=0.5,print_cost=False):
    w,b=initialize_with_zeros(X_train.shape[0])#initialize the parameters
    #gradient descent to find the optimized parameters
    w,b,dw,db,costs=optimize(w,b,X_train,Y_train,num_iterations,learning_rate,print_cost)
    #predict the test data
    Y_prediction_test=predict(w,b,X_test)
    Y_prediction_train=predict(w,b,X_train)
    #output the accuracy
    print('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

运行:

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)
print(d)

得出结果:
训练准确度:99.0%
预测准确度:70.0%
在这里插入图片描述使用自己的图片来预测
找一张猫和非猫的图片来预测下:
以下是我找到的图片1
在这里插入图片描述
代码如下

# --------------测试本地图片------------

fname =r'C:\Users\lee\Pictures\fish.jpg'
image = np.array(plt.imread(fname))
my_image = np.array(Image.fromarray(image).resize(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)
plt.show()
print("y = " + str(np.squeeze(my_predicted_image)) + ", 你的预测结果是\""
      + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") +  "\" picture.")

预测结果如下
在这里插入图片描述图片2
在这里插入图片描述在这里插入图片描述图片3
在这里插入图片描述错误的预测
在这里插入图片描述
整体的代码

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

def load_dataset():
    train_dataset = h5py.File(r'D:\DeepLearning\Week2\week2dataset\datasets\train_catvnoncat.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:])  # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:])  # your train set labels

    test_dataset = h5py.File(r'D:\DeepLearning\Week2\week2dataset\datasets\test_catvnoncat.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:])  # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:])  # your test set labels

    classes = np.array(test_dataset["list_classes"][:])  # the list of classes

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_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, classes



train_set_x_orig,train_set_y,test_set_x_orig,test_set_y,classes =load_dataset()
# index = 46 # 取第46张图片
# plt.imshow(train_set_x_orig[index])
# plt.show()
# print("y = "+ str(train_set_y[ : ,index]) +",it is a ' " +classes[np.squeeze(train_set_y[:,index])].decode("utf-8") + " ' picture.")
# print("---------------数据集的详细内容-----------------")
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 =" + str(m_train))
# print("测试集的样本数量是: m_test =" + str(m_test))
# print("每个图片的长宽是: num_px =" + str(num_px))
# print("每个图片的尺寸是: (" + str(num_px) +"," + str(num_px) + ",3")
# print("训练集X的尺寸:" + str(train_set_x_orig.shape))
# print("训练集y的尺寸:" + str(train_set_y.shape))
# print("测试集X的尺寸:" + str(test_set_x_orig.shape))
# print("测试集y的尺寸:" + str(test_set_y.shape))
# print("---------将样本整合为一个向量后数据集详情----------")
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
# print("训练集x向量化后的尺寸:"+ str(train_set_x_flatten.shape))
# print("训练集y的尺寸:" + str(train_set_y.shape))
# print("测试集向量化后尺寸:" + str(test_set_x_flatten.shape))
# print("测试集y的尺寸:" + str(test_set_y.shape))
# print("整形后的检查:" + str(train_set_x_flatten[0:5,0]))# 取列向的第0-4个数
# -------------------数据归一化----------------------
train_set_x = train_set_x_flatten/255
test_set_x = test_set_x_flatten/255
# -------------------构建sigmoi函数---------------
def sigmoid(z):
    s = 1 / (1 + np.exp(-z))
    return s
# print("---------------测试simoid函数---------------------")
# print("sigmiod([0,2]) = " + str(sigmoid(np.array([0,2]))))
# ----------------------初始化权重和偏置----------------------
def initialize_with_zeros(dim):
    w = np.zeros((dim,1))
    b = 0
    assert (w.shape == (dim,1))
    assert (isinstance(b,float)or isinstance(b,int))
    return w,b
# print("---------------测试初始化结果---------------------")
dim = 2
w,b = initialize_with_zeros(dim)
# print("w = " + str(w))
# print("b = " + str(b))
# ------------构建正向传播函数------------
def propagate(w,b,X,Y):
    m = X.shape[1]
    A = sigmoid(np.dot(w.T,X) + b)
    cost = -1/m*np.sum(Y*np.log(A) + (1-Y)*np.log(1-A))
    dw = 1/m*np.dot(X,(A - Y).T)
    db = 1/m*np.sum(A-Y)
    assert (dw.shape == w.shape)
    assert (db.dtype == float)
    cost = np.squeeze(cost)
    assert (cost.shape == ())
    grads = {"dw":dw,
             "db":db}
    return grads,cost
# print("--------------正向传播测试--------------------")
# w,b,X,Y = np.array([[1],[2]]),2,np.array([[1,2],[3,4]]),np.array([[1,0]])
# grads,cost= propagate(w,b,X,Y)
# print("dw = " + str(grads["dw"]))
# print("db = " + str(grads["db"]))
# print("cost = " + str(cost))
# -------------------构建反向传播函数--------------------
def optimize(w,b,X,Y,num_iteration,learning_rate,print_cost = False):#num_iteration是优化循环的迭代次数
    costs = []
    for i in  range(num_iteration):
        grads,cost = propagate(w,b,X,Y)
        dw = grads["dw"]
        db = grads["db"]
        w = w - learning_rate*dw
        b = b - learning_rate*db
        if i % 100 == 0:
            costs.append(cost)
        if print_cost and i % 100 == 0:
            print("经过i次循环后损失函数值 i = %i:%f" %(i,cost))
    params = {"w" : w,
              "b" : b}
    grads = {"dw" : dw,
             "db" : db}
    return params,grads,costs
# print("----------------测试反向传播--------------")
# params,grads,costs = optimize(w,b,X,Y,num_iteration= 100,learning_rate= 0.009,print_cost= False)
# print("w = " + str(params["w"]))
# print("b = " + str(params["b"]))
# print("dw = " + str(grads["dw"]))
# print("db = " + str(grads["db"]))
# print(costs)
# --------------构建预测函数-----------------
def predict(w,b,X):
    m = X.shape[1]
    Y_prediction = np.zeros((1,m))
    w = w.reshape(X.shape[0],1)
    A = sigmoid(np.dot(w.T,X) + b)
    for i in range(A.shape[1]):
        if A[0,i] <= 0.5:
            Y_prediction[0,i] = 0
        else:
            Y_prediction[0,i] = 1
    assert (Y_prediction.shape == (1,m))
    return  Y_prediction
# print("-------------测试预测结果--------------------")
# print("预测结果:" + str(predict(w,b,X)))
# -------------------将所有函数合并到一个模型中-----------------
def model(X_train,Y_train,X_test,Y_test,num_iterations = 2000,learning_rate = 0.5,print_cost = False):
    w,b = initialize_with_zeros(X_train.shape[0])
    parameters,grads,costs = optimize(w,b,X_train,Y_train,num_iterations,learning_rate,print_cost)
    w = parameters["w"]
    b = parameters["b"]
    Y_prediction_test = predict(w,b,X_test)
    Y_prediction_train = predict(w,b,X_train)
    print("训练集精度:{} %".format(100 - np.mean(np.abs(Y_prediction_train-Y_train))*100))
    print("测试集精度:{} %".format(100 - np.mean(np.abs(Y_prediction_test-Y_test))*100))
    d = {"costs" : costs,
         "测试集预测结果": Y_prediction_test,
         "训练集预测结果": Y_prediction_train,
         "w": w,
         "b": b,
         "learning_rate": learning_rate,
         "num_iterations": num_iterations}
    return d
print("--------------------测试模型---------------------")
d = model(train_set_x,train_set_y,test_set_x,test_set_y,num_iterations= 2000,learning_rate=0.005,print_cost= True)
index = 15
plt.imshow(test_set_x[:,index].reshape((num_px,num_px,3)))
plt.show()
print("y = " + str(test_set_y[0,index]) + ",预测结果是\"" + classes[int(d["测试集预测结果"][0,index])].decode("utf-8")+"\" picture")
print("---------------显示被预测图片---------------")
costs = np.squeeze(d['costs'])
plt.plot(costs)
plt.ylabel('代价函数值')
plt.xlabel('每一百次迭代')
plt.title("学习率 = " + str(d["learning_rate"]))
plt.show()
# ---------------------改变学习率---------------------
learning_rates = [0.01,0.011,0.005]
models = {}
for i in learning_rates:
    print("学习率是:" + str(i))
    models[str(i)] = model(train_set_x,train_set_y,test_set_x,test_set_y,num_iterations = 1500,learning_rate = i,print_cost = False)
    print('\n' + "--------------------------------" + '\n')
for i in learning_rates:
        # plt.plot(np.squeeze(models[str(i)]["损失函数"]), label= str(models[str(i)]["学习率"]))
    plt.plot(np.squeeze(models[str(i)]["costs"]), label=str(models[str(i)]["learning_rate"]))
plt.xlabel('cost')
plt.xlabel("iterations")
legend = plt.legend(loc='upper center',shadow= True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.show()
# --------------测试本地图片------------

fname =r'C:\Users\lee\Pictures\20201025105732.jpg'
image = np.array(plt.imread(fname))
my_image = np.array(Image.fromarray(image).resize(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)
plt.show()
print("y = " + str(np.squeeze(my_predicted_image)) + ", 你的预测结果是\""
      + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") +  "\" picture.")


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值