01.神经网络与深度学习-第二周作业

作业2------代码来源于B站up主ladykaka007
https://space.bilibili.com/49109393?spm_id_from=333.788.b_765f7570696e666f.2
手把手教大家实现吴恩达深度学习作业第二周

#导入数据
import h5py
#训练原始数据
train_data = h5py.File('C:/Users/tf/Desktop/assignment2/datasets/train_catvnoncat.h5','r')
test_data = h5py.File('C:/Users/tf/Desktop/assignment2/datasets/test_catvnoncat.h5','r')
for key in train_data.keys():
    print(key)
--------------------------------
    list_classes
train_set_x
train_set_y
print(train_data['train_set_x'].shape)
print(train_data['train_set_y'].shape)
print(train_data['list_classes'].shape)
------------------------------------------
(209, 64, 64, 3)
(209,)
(2,)
print(test_data['test_set_x'].shape)
print(test_data['test_set_y'].shape)
#取出数据集  测试集
train_data_org = train_data['train_set_x'][:]
train_labels_org = train_data['train_set_y'][:]
test_data_org = test_data['test_set_x'][:]
test_labels_org = test_data['test_set_y'][:]
print("训练集图片:"+str(train_data_org.shape))
print("训练集标签:"+str(train_labels_org.shape))
print("测试集图片:"+str(test_data_org.shape))
print("测试集标签:"+str(test_labels_org.shape))
-----------------------------------------
训练集图片:(209, 64, 64, 3)
训练集标签:(209,)
测试集图片:(50, 64, 64, 3)
测试集标签:(50,)
#查看图片
import matplotlib.pyplot as plt
#matplotlib inline #可以在线显示图片,后面不用加plt.show()显示图片
plt.imshow(train_data_org[176])
#数据纬度的处理
m_train = train_data_org.shape[0]
m_test = test_data_org.shape[0]
train_data_tran = train_data_org.reshape(m_train,-1).T
test_data_tran = test_data_org.reshape(m_test,-1).T
print(m_train)
print(m_test)
print(train_data_tran.shape)
print(test_data_tran.shape)

# train_labels_tran = train_labels_org.reshape(1,train_labels_org.shape[0])
# test_labels_tran = test_labels_org.reshape(1,test_labels_org.shape[0])
import numpy as np
train_labels_tran = train_labels_org[np.newaxis,:]#和上面一行一个意思
test_labels_tran = test_labels_org[np.newaxis,:]#和上面一行一个意思
print(train_labels_tran.shape)
print(test_labels_tran.shape)
------------------------------------------
209
50
(12288, 209)
(12288, 50)
(1, 209)
(1, 50)
#标准化数据
train_data_sta = train_data_tran / 255
test_data_sta = test_data_tran / 255
print(train_data_sta.shape)
print(test_data_sta.shape)
#定义sigmoid函数
def sigmoid(z):
    a =1 / (1 + np.exp(-z))
    return a
#初始化参数
n_dim = train_data_sta.shape[0]
print(n_dim)
w = np.zeros((n_dim,1))
b = 0
#定义前向传播函数
def propagate(w,b,X,y):
    
    #1.前向传播函数
    z = np.dot(w.T,X) + b
    A = sigmoid(z)
    
    #2.代价函数
    m = X.shape[1]
    J = -1/m * np.sum(y * np.log(A)+(1-y) * np.log(1-A))
    
    #3.梯度下降
    dw = 1/m * np.dot(X,(A-y).T)
    db = 1/m * np.sum(A-y)
    
    grands = {'dw':dw,'db':db}
    
   # return dw,db,J  #返回参数太多,而且w,b平常都是一起参考的,所以建议用字典,这个不是必须的
    return grands,J
#优化部分
def optimize(w,b,X,y,alpha,n_iters,print_cost):
    
    costs =[]
    
    for i in range(n_iters):
        grands,J = propagate(w,b,X,y)
        dw = grands['dw']
        db = grands['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)
            
    grands = {'dw':dw,'db':db}
    params = {'w':w,'b':b}
        
    return grands,params,costs
#预测部分
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[:,i] = 1
        else:
            y_pred[:,i] = 0
    return y_pred
#模型的整合
def model(w,b,X_train,y_train,X_test,y_test,alpha,n_iters,print_cost):
    grands,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 acc is",np.mean(y_pred_train == y_train)*100,'%')
    print("the test acc is",np.mean(y_pred_test == y_test)*100,'%')
    
    d = {
        'w':w,
        'b':b,
        'costs':costs,
        'y_pred_train':y_pred_train,
        'y_pred_test':y_pred_test,
        'alpha':alpha
    }
    return d
d = model(w,b,train_data_sta,train_labels_tran,test_data_sta,test_labels_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.3314632893282513
n_iters is 500 cost is 0.30327306747438293
n_iters is 600 cost is 0.2798795865826048
n_iters is 700 cost is 0.26004213692587574
n_iters is 800 cost is 0.24294068467796623
n_iters is 900 cost is 0.22800422256726066
n_iters is 1000 cost is 0.21481951378449635
n_iters is 1100 cost is 0.20307819060644985
n_iters is 1200 cost is 0.1925442771670686
n_iters is 1300 cost is 0.18303333796883503
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.15266732471296504
n_iters is 1800 cost is 0.1465422350398234
n_iters is 1900 cost is 0.14087207570310162
the train acc is 99.04306220095694 %
the test acc is 70.0 %

训练集上面效果是99.04%,测试集上面是70%,说明有点过拟合了,太过贴合训练标签了,毕竟我们用的是逻辑回归,效果已经不错了

plt.plot(d['costs'])
plt.xlabel('per hundred iters')
plt.ylabel('costs')

在这里插入图片描述

index  = 45
print('y is',test_labels_tran[0,index])
print('y prediction is ',int(d['y_pred_test'][0,index]))

结果:

y is 0
y prediction is  0
plt.imshow(test_data_org[index])

在这里插入图片描述

alphas = [0.01,0.001,0.0001]
for i in alphas:
    print('alpha',i)
    d = model(w,b,train_data_sta,train_labels_tran,test_data_sta,test_labels_tran,alpha=i,n_iters =2000,print_cost =False)
    print("="*20)
    plt.plot(d['costs'],label = str(i))
plt.xlabel('per hundred iters')
plt.ylabel('cost')
plt.legend()
-------------------------------------------


alpha 0.01
the train acc is 99.52153110047847 %
the test acc is 70.0 %
====================
alpha 0.001
the train acc is 91.38755980861244 %
the test acc is 68.0 %
====================
alpha 0.0001
the train acc is 71.29186602870813 %
the test acc is 40.0 %
====================

在这里插入图片描述

# 下面是自己网上找的图片来经行验证
fname  ='C:/Users/tf/Desktop/cat2.jpg'
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
 -------------------------
 (12288, 1)
y = predict(d['w'],d['b'],image_tran)
print(int(y))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值