Deep learning practice of ANg(C1week2)

本文介绍了如何使用Python、h5py库和sigmoid激活函数训练一个简单的深度学习模型来识别猫的图像。通过加载训练和测试数据集,对数据进行预处理、标准化,然后使用梯度下降法优化模型参数。模型训练后,测试了不同学习率对准确率的影响,最终在测试集上达到了70%的准确率。
摘要由CSDN通过智能技术生成

for self-use

目的:识别图片中的是否是猫。

分析:已有训练集及测试集,设计一个简单算法解决此二分类问题。给出的数据集类型为h5,查看知其有三个键,样本数据在train_set_x中,对应标签在train_set_y中。取出样本数据(209,64*64*3)及对应标签(209,1),在处理完维数并标准化数据后,可以开始进行训练。week1要求不高,激活函数使用sigmoid即可。

Code

preprocess

import h5py
import numpy as np
import matplotlib.pyplot as plt


train_data = h5py.File("D:\\DL\\C1WEEK2\\datasets\\train_catvnoncat.h5","r")
test_data = h5py.File("D:\\DL\\C1WEEK2\\datasets\\test_catvnoncat.h5","r")

#取出训练集和测试集
train_data_org = train_data['train_set_x'][:]
train_label_org = train_data['train_set_y'][:]
test_data_org = test_data['test_set_x'][:]
test_label_org = test_data['test_set_y'][:]

#数据维度处理
m_train = train_data_org.shape[0]
m_test = test_data_org.shape[0]
train_data_trans =  train_data_org.reshape(m_train,-1).T
test_data_trans =  test_data_org.reshape(m_test,-1).T

train_label_tran = train_label_org[np.newaxis,:] #增加一个新的维度  50行变1行50列
test_label_tran = test_label_org[np.newaxis,:] 

#标准化数据
train_data_sta = train_data_trans / 255
test_data_sta = test_data_trans / 255

#初始化参数
n_dim = train_data_trans.shape[0]
w = np.zeros((n_dim,1))
b = 0

sigmoid

#定义sigmoid
def sigmoid(z):
    a = 1 / (1+np.exp(-z))
    return a

forwardPropagation & costFution & gradientDescent

#forward propagation & costFuction & GD
def fwdppg(w,b,X,y):
    z = np.dot(w.T,X)+b
    A = sigmoid(z)
    
    m = X.shape[1]  # 0行,1列
    J = -1/m * np.sum(y * np.log(A) + (1-y) * np.log(1-A))
    
    dw = np.dot(X,(A-y).T)/m
    db = np.sum(A-y)/m
    
    grads = {'dw':dw,'db':db}
    return grads,J

optimize 

#优化
def optimize(w,b,X,y,alpha,n_iters,print_cost):
    costs = []
    for i in range(n_iters):
        grads,J = fwdppg(w,b,X,y)
        dw = grads['dw']
        db = grads['db']
        
        w = w - alpha * dw
        b = b - alpha * db
        
        if i % 100 == 0:
            costs.append(J)
            if print_cost:
                print('n_iters is ',i,' cost is ',J)
            
    grads = {'dw':dw,'db':db}
    params = {'w':w,'b':b}
        
    return grads,params,costs

predict

#预测
def predict(w,b,X_test):
    
    z = np.dot(w.T,X_test) + b
    A = sigmoid(z)
    
    m = X_test.shape[1]
    y_pred = np.zeros([1,m])
    
    for i in range(m):
        if A[:,i] > 0.5:
            y_pred[0,i] = 1
        else:
            y_pred[0,i] = 0
    return y_pred

model

#整合模型
def model(w,b,X_train,y_train,X_test,y_test,alpha,n_iters,print_cost):
    grads,params,costs = optimize(w,b,X_train,y_train,alpha,n_iters,print_cost)
    w = params['w']
    b = params['b']
    
    y_pred_train = predict(w,b,X_train)
    y_pred_test = predict(w,b,X_test)
    
    print('The Train Accurancy: ',np.mean(y_pred_train == y_train)*100,'%')
    print('The Test Accurancy: ',np.mean(y_pred_test == y_test)*100,'%')
    
    B = {
        'w' : w,
        'b' : b,
        'costs' : costs,
        'y_pred_train' : y_pred_train,
        'y_pred_test' : y_pred_test,
        'alpha' : alpha
    }
    
    return B

use it

D = model(w,b,train_data_sta,train_label_tran,test_data_sta,test_label_tran,alpha=0.005,n_iters=2000,print_cost=True)
n_iters is  0  cost is  0.6931471805599453
n_iters is  100  cost is  0.5845083636993086
n_iters is  200  cost is  0.46694904094655476
n_iters is  300  cost is  0.37600686694802077
n_iters is  400  cost is  0.3314632893282512
n_iters is  500  cost is  0.30327306747438293
n_iters is  600  cost is  0.27987958658260487
n_iters is  700  cost is  0.2600421369258757
n_iters is  800  cost is  0.24294068467796626
n_iters is  900  cost is  0.22800422256726063
n_iters is  1000  cost is  0.2148195137844964
n_iters is  1100  cost is  0.20307819060644985
n_iters is  1200  cost is  0.19254427716706857
n_iters is  1300  cost is  0.18303333796883509
n_iters is  1400  cost is  0.17439859438448876
n_iters is  1500  cost is  0.16652139705400335
n_iters is  1600  cost is  0.15930451829756614
n_iters is  1700  cost is  0.152667324712965
n_iters is  1800  cost is  0.1465422350398234
n_iters is  1900  cost is  0.1408720757031016
The Train Accurancy:  99.04306220095694 %
The Test Accurancy:  70.0 %

check the graph

plt.plot(D['costs'])
plt.xlabel('per hundred inters')
plt.ylabel('cost')

output:

try different alpha

alphas = [0.01,0.001,0.0001]
for i in alphas:
    print('alpha = ',i)
    D = model(w,b,train_data_sta,train_label_tran,test_data_sta,test_label_tran,alpha=i,n_iters=2000,print_cost=False)
    print('==========================')
    plt.plot(D['costs'],label = str(i))
    
plt.xlabel('per hundred iters')
plt.ylabel('costs')
plt.legend()
alpha =  0.01
The Train Accurancy:  99.52153110047847 %
The Test Accurancy:  70.0 %
==========================
alpha =  0.001
The Train Accurancy:  91.38755980861244 %
The Test Accurancy:  68.0 %
==========================
alpha =  0.0001
The Train Accurancy:  71.29186602870813 %
The Test Accurancy:  40.0 %
==========================

 

 try our own picture

fname = 'C:\\Users\\JUMP_C\\Desktop\\6B31D1B92637CA60CBACF2937FF297A8.gif'
image = plt.imread(fname)
plt.imshow(image)

 

 

#看一下图片的格式,把他转换成符合要求的格式
image.shape

from skimage import transform
image_tran = transform.resize(image,(64,64,3)).reshape(64*64*3,1)

image_tran.shape

 (300, 300, 4)

(12288, 1)

y = predict(D['w'],D['b'],image_tran)
print(int(y))

1

于是汤姆猫作为一只猫猫被认可了! 

参考:手把手教大家实现吴恩达深度学习作业第二周01_哔哩哔哩_bilibili

 这个小姐姐真的太好了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值