cousera深度学习课程第二周代码练习

代码式样

以下是本人练习的代码:

#以下是导入的库
import numpy as np 
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
from lr_utils import load_dataset
%matplotlib inline

# 加载数据 (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()

# 图片例子
index = 25
plt.imshow(train_set_x_orig[index])
print ("y = " + str(train_set_y[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") +  "' picture.")

m_train = 209
m_test = 50
num_px = 64

# 重新塑造数组形状
train_set_x_flatten = train_set_x_orig.reshape((train_set_x_orig.shape[1]*train_set_x_orig.shape[2]*train_set_x_orig.shape[3],train_set_x_orig.shape[0]))
test_set_x_flatten = test_set_x_orig.reshape((test_set_x_orig.shape[1]*test_set_x_orig.shape[2]*test_set_x_orig.shape[3],test_set_x_orig.shape[0]))

train_set_x = train_set_x_flatten/255.
test_set_x = test_set_x_flatten/255.

# 梯度函数: sigmoid
def sigmoid(z):
    s = 1/(1 + np.exp(-z))
    return s

# 梯度函数: 创建一个形状为(dim,1)的零矢量
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
    
# 梯度函数: 正/反向传播

def propagate(w, b, X, Y):
    m = X.shape[1]
    # 正向传播 (从X到cost)
    A = 1/(1 + np.exp(-(np.sum(w*X,axis = 0) + b)))                                  # compute activation
    cost = -(1 / m) * np.sum((Y * np.log(A) + (1 - Y) * np.log(1 - A)), axis=1)            # compute cost
  
    # 反向传播 (查找梯度)
    dw = (1/m)*np.dot(X,(A - Y).T)
    db = (1/m)*np.sum(A - Y,axis = 1)
    
    assert(dw.shape == w.shape)
    assert(db.dtype == float)
    cost = np.squeeze(cost)
    assert(cost.shape == ())
    
    grads = {"dw": dw,
             "db": db}
    
    return grads, cost
    
# 梯度函数: 优化w,b

def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):

    costs = []
    
    for i in range(num_iterations):
        
        
        # 代价与梯度的计算
        grads, cost = propagate(w, b, X, Y)
        
        # 从梯度里检索字典
        dw = grads["dw"]
        db = grads["db"]
        
        # 更新w,b参数,使最优化
        w = w - learning_rate*dw
        b = b - learning_rate*db
        
        # 记录代价
        if i % 100 == 0:
            costs.append(cost)
        
        # Print the cost every 100 training iterations
        if 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

# 梯度函数: 预测

def predict(w, b, X):

    m = X.shape[1]
    Y_prediction = np.zeros((1,m))
    w = w.reshape(X.shape[0], 1)
    
    # 计算向量A,以预测图片中存在猫的概率
    A = (1/(1 + np.exp(-(np.sum(w*X,axis = 0) + b)))).reshape((1,m))
    
    for i in range(A.shape[1]):
        
        # 将A[0][i]转换成p[0][i]
        A = np.squeeze(A)
        if A[i] <= 0.5:
            Y_prediction[0][i] = 0
        else:
            Y_prediction[0][i] = 1
    
    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.005, print_cost = False):

    # 初始化参数为0
    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 = False)
    
    # 从字典中检索w,b
    w = parameters["w"]
    b = parameters["b"]
    
    # 预测训练/测试数据集
    Y_prediction_test = predict(w, b, X_test)
    Y_prediction_train = predict(w, b, X_train)


    # Print train/test Errors
    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, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.005, print_cost = True)

训练数据,测试数据是网站的jupyter提供的数据,所以要想运行上面的代码,请到课程练习中练习。

在编写上述代码时,向量化函数计算时,下了很多的功夫,运行代码时,一直运行不出正确结果,经过本子上的推敲,深刻理解了其原理,也使代码顺利编译出正确结果。

用到的函数

np.array([[1],[2]]) #创建21的数组
np.sum(array,axis = 0/1) # =0时,每行相加, =1时,每列相加
array.shape #显示数组的形状
array.shape[0] #显示数组第一纬度数,其他的以此类推
array.reshape #重塑数组形状
np.exp(x) #表示e^x
np.zeros((dim,1)) #显示为dim
1形状,其数都是0的数组
np.dot(A,B) #表示A,B两个矩阵点乘
np.squeeze(A) #表示数组的形状中删除单维条目,即把shape中为1的维度去掉

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值