吴恩达作业3:利用深层神经网络实现小猫的分类

利用4层神经网络实现小猫的分类,小猫训练样本是(209,64*64*3=12288),故输入节点是12288个,隐藏层节点依次为20,7,5,输出层为1。

首先看文件路径,dnn_utils_v2.py代码是激活函数和激活函数导数 载入数据集 打印预测错误照片的代码:

 

import numpy as np
import matplotlib.pyplot as plt
import h5pydef sigmoid(Z):"""Implements the sigmoid activation in numpyArguments:Z -- numpy array of any shapeReturns:A -- output of sigmoid(z), same shape as Zcache -- returns Z as well, useful during backpropagation"""A = 1/(1+np.exp(-Z))cache = Zreturn A, cachedef relu(Z):"""Implement the RELU function.Arguments:Z -- Output of the linear layer, of any shapeReturns:A -- Post-activation parameter, of the same shape as Zcache -- a python dictionary containing "A" ; stored for computing the backward pass efficiently"""A = np.maximum(0,Z)assert(A.shape == Z.shape)cache = Z return A, cachedef relu_backward(dA, cache):"""Implement the backward propagation for a single RELU unit.Arguments:dA -- post-activation gradient, of any shapecache -- 'Z' where we store for computing backward propagation efficientlyReturns:dZ -- Gradient of the cost with respect to Z"""Z = cachedZ = np.array(dA, copy=True) # just converting dz to a correct object.# When z <= 0, you should set dz to 0 as well. dZ[Z <= 0] = 0assert (dZ.shape == Z.shape)return dZdef sigmoid_backward(dA, cache):"""Implement the backward propagation for a single SIGMOID unit.Arguments:dA -- post-activation gradient, of any shapecache -- 'Z' where we store for computing backward propagation efficientlyReturns:dZ -- Gradient of the cost with respect to Z"""Z = caches = 1/(1+np.exp(-Z))dZ = dA * s * (1-s)assert (dZ.shape == Z.shape)return dZ"""
载入数据集
"""
def load_data():train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")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]))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, classesdef print_mislabeled_images(classes, X, y, p):"""Plots images where predictions and truth were different.X -- datasety -- true labelsp -- predictions"""a = p + ymislabeled_indices = np.asarray(np.where(a == 1))plt.rcParams['figure.figsize'] = (40.0, 40.0)  # set default size of plotsnum_images = len(mislabeled_indices[0])for i in range(num_images):index = mislabeled_indices[1][i]plt.subplot(2, num_images, i + 1)plt.imshow(X[:, index].reshape(64, 64, 3), interpolation='nearest')plt.axis('off')plt.title("Prediction: " + classes[int(p[0, index])].decode("utf-8") + " \n Class: " + classes[y[0, index]].decode("utf-8"))

StartDeepNeural.py是整个模型前向传播和后向传播的代码

 

 

import numpy as np
import dnn_utils_v2
def initialize_parameters(n_x,n_h,n_y):W1 = np.random.randn(n_h,n_x)*0.01b1 = np.zeros((n_h,1))W2 = np.random.randn(n_y, n_h) * 0.01b2 = np.zeros((n_y,1))assert (W1.shape==(n_h,n_x))assert (b1.shape == (n_h, 1))assert (W2.shape == (n_y, n_h))assert (b2.shape == (n_y, 1))parameters={'W1': W1,'b1': b1,'W2': W2,'b2': b2}return parameters
"""
多层神经网络的参数初始化:注意权重的初始化 为防止梯度爆炸和梯度消失
"""
def initialize_parameters_deep(layer_dims):L=len(layer_dims)parameters={}for i in range(1,L):parameters['W'+str(i)] = np.random.randn(layer_dims[i],layer_dims[i-1])*np.sqrt(2.0/layer_dims[i-1])parameters['b' + str(i)] = np.zeros((layer_dims[i], 1))assert (parameters['W'+str(i)].shape==(layer_dims[i],layer_dims[i-1]))assert (parameters['b' + str(i)].shape == (layer_dims[i], 1))return parameters
"""
前向传播过程中某一层未加激活函数的操作
"""
def linear_forward(A,W,b):Z = np.dot(W,A)+bassert(Z.shape==(W.shape[0],A.shape[1]))cache=(A,W,b)return Z,cache
"""
前向传播过程中 某一层通过激活函数来采取相应的操作
"""
def linear_activation_forward(A_prev,W,b,activation):if activation=='sigmoid':Z, linear_cache=linear_forward(A_prev,W,b)A, activation_cache = dnn_utils_v2.sigmoid(Z)elif activation=='relu':Z, linear_cache = linear_forward(A_prev, W, b)A, activation_cache = dnn_utils_v2.relu(Z)assert(A.shape==(W.shape[0],A_prev.shape[1]))cache=(linear_cache,activation_cache)###save ( (A W b ),Z) tuplereturn A,cache
"""
整个模型的前向传播过程
"""
def L_model_forward(X,parameters):L=len(parameters)//2A=Xcaches=[]for i in range(1,L):A_prev=AA, cache=linear_activation_forward(A_prev,parameters['W'+str(i)],parameters['b'+str(i)],activation='relu')caches.append(cache)AL, cache = linear_activation_forward(A, parameters['W' + str(L)], parameters['b' + str(L)],activation='sigmoid')caches.append(cache)assert(AL.shape==(parameters['W' + str(L)].shape[0],X.shape[1]))return AL,caches
"""
计算损失值
"""
def compute_cost(AL,Y):m = Y.shape[1]cost = (1. / m) * (-np.dot(Y, np.log(AL).T) - np.dot(1 - Y, np.log(1 - AL).T))#cost = -1 / m * (np.dot(Y, np.log(AL).T) + np.dot((1 - Y), np.log(1 - AL).T))cost = np.squeeze(cost)assert (cost.shape==())return cost
"""
后向传播过程中某一层未加激活函数的操作
"""
def linear_backward(dZ,cache):A_prev,W,b=cachem=A_prev.shape[1]dW=1/m*np.dot(dZ,A_prev.T)db=1/m*np.sum(dZ,axis=1,keepdims=True)dA_prev=np.dot(W.T,dZ)assert (dW.shape == W.shape)assert (db.shape == b.shape)assert (dA_prev.shape == A_prev.shape)return dA_prev,dW,db
"""
后向传播过程中 某一层通过激活函数来采取相应的操作
"""
def linear_activation_backward(dA,cache,activation):linear_cache, activation_cache=cacheif activation=='relu':dZ=dnn_utils_v2.relu_backward(dA, activation_cache)dA_prev, dW, db=linear_backward(dZ, linear_cache)elif activation=='sigmoid':dZ=dnn_utils_v2.sigmoid_backward(dA, activation_cache)dA_prev, dW, db = linear_backward(dZ, linear_cache)return dA_prev, dW, db
"""
整个模型的后向传播过程
"""
def L_model_backward(AL,Y,caches):grads={}L=len(caches) ###[ [( (X W1 b1 ),Z1)],[( (A1 W2 b2 ),Z2) ] [( (A2 W3 b3 ),Z3) ]]m=AL.shape[1]Y=Y.reshape(AL.shape)dAL=-np.divide(Y,AL)+np.divide((1-Y),(1-AL))current_cache=caches[L-1]grads['dA'+str(L)],grads['dW'+str(L)],grads['db'+str(L)]=linear_activation_backward(dAL, current_cache, activation='sigmoid')for i in reversed(range(L-1)):current_cache = caches[i]grads['dA' + str(i+1)], grads['dW' + str(i+1)], grads['db' + str(i+1)]=linear_activation_backward(grads['dA'+str(i+2)], current_cache, activation='relu')return grads
"""
更新参数
"""
def update_parameters(parameters,grads,learning_rate):L=len(parameters)//2for i in range(L):parameters['W'+str(i+1)]=parameters['W'+str(i+1)]-learning_rate*grads['dW' + str(i+1)]parameters['b' + str(i + 1)] = parameters['b' + str(i + 1)] - learning_rate * grads['db' + str(i + 1)]return parameters

 

DeepNeuralCat_noCat.py就是训练的代码:首先看数据集

 

 
import numpy as np
import dnn_utils_v2
import matplotlib.pyplot as plt
import StartDeepNeural
train_x_orig, train_y_orig, test_x_orig, test_y_orig,classa=dnn_utils_v2.load_data()
print('train={}'.format(train_x_orig.shape))
print(train_y_orig.shape)
print(train_y_orig[:,:10])
print(test_x_orig.shape)
print(test_y_orig.shape)
print(classa)
plt.imshow(train_x_orig[0])
plt.show()

 

打印结果:

可知训练样本为209个,维度为(64,64,3),测试样本为50个,维度为(64,64,3),0代表不是猫

拉成二维向量 对于训练样本(209,64*64*3),测试样本(50,64*64*3)

 

import numpy as np
import dnn_utils_v2
import matplotlib.pyplot as plt
import StartDeepNeural
train_x_orig, train_y_orig, test_x_orig, test_y_orig,classa=dnn_utils_v2.load_data()
# print('train={}'.format(train_x_orig.shape))
# print(train_y_orig.shape)
# print(train_y_orig[:,:10])
# print(test_x_orig.shape)
# print(test_y_orig.shape)
# print(classa)
# plt.imshow(train_x_orig[0])
# plt.show()
##train
train_x_flatten = train_x_orig.reshape(train_x_orig.shape[0],train_x_orig.shape[1] *train_x_orig.shape[2] * 3).T
train_x = train_x_flatten / 255.
#print(train_x.shape)
##test
test_x_flatten = test_x_orig.reshape(test_x_orig.shape[0],test_x_orig.shape[1] *test_x_orig.shape[2] * 3).T
test_x = test_x_flatten / 255.
#print(test_x.shape)

 

开始训练,下面代码也写了两层神经网络,只是没有用到而已。

 

import numpy as np
import dnn_utils_v2
import matplotlib.pyplot as plt
import StartDeepNeural
train_x_orig, train_y_orig, test_x_orig, test_y_orig,classa=dnn_utils_v2.load_data()
# print('train={}'.format(train_x_orig.shape))
# print(train_y_orig.shape)
# print(train_y_orig[:,:10])
# print(test_x_orig.shape)
# print(test_y_orig.shape)
# print(classa)
# plt.imshow(train_x_orig[0])
# plt.show()
##train
train_x_flatten = train_x_orig.reshape(train_x_orig.shape[0],train_x_orig.shape[1] *train_x_orig.shape[2] * 3).T
train_x = train_x_flatten / 255.
#print(train_x.shape)
##test
test_x_flatten = test_x_orig.reshape(test_x_orig.shape[0],test_x_orig.shape[1] *test_x_orig.shape[2] * 3).T
test_x = test_x_flatten / 255.
#print(test_x.shape)def two_layer_model(X,Y,num_iterations,learning_rate):n_x = 12288n_h = 7n_y = 1layer_dims = (n_x, n_h, n_y)#m=X.shape[1]#(n_x, n_h, n_y)=layer_dimsparameters=StartDeepNeural.initialize_parameters_deep(layer_dims)# W1 = parameters['W1']# W2 = parameters['W2']# b1 = parameters['b1']# b2 = parameters['b2']costs=[]#return W1,W2,b1,b2for i in range(0,num_iterations):# A1, cache1=StartDeepNeural.linear_activation_forward(X,W1,b1,activation='relu')# A2, cache2 = StartDeepNeural.linear_activation_forward(A1, W2, b2, activation='sigmoid')AL, caches=StartDeepNeural.L_model_forward(X, parameters) ##[ [( (X W1 b1 ),Z1)],[( (A1 W2 b2 ),Z2) ]]cost=StartDeepNeural.compute_cost(AL, Y)grads=StartDeepNeural.L_model_backward(AL, Y, caches)parameters=StartDeepNeural.update_paremeters(parameters, grads, learning_rate)if i %100==0:print('iterations{}:cost {}'.format(i,cost))costs.append(cost)return costs,parameters
def predict(X,Y,parameters):AL, caches = StartDeepNeural.L_model_forward(X, parameters)##AL.shape=(1,m)m=X.shape[1]p=np.zeros((1,m))for i in range(AL.shape[1]):if AL[0][i]>0.5:p[0][i]=1else:p[0][i] = 0result = np.squeeze(np.dot(p, Y.T) + np.dot(1 - p, 1 - Y.T))accuracy=result/mreturn accuracy,p
def two_layer_model_test():costs, parameters = two_layer_model(train_x, train_y_orig,num_iterations = 3000, learning_rate = 0.0075)# print(parameters)accuracy ,p_train= predict(train_x, train_y_orig, parameters)print('train accuracy is {}'.format(accuracy))accuracy ,p_test= predict(test_x, test_y_orig, parameters)print('test accuracy is {}'.format(accuracy))plt.plot(costs)plt.xlabel('iterations')plt.ylabel('costs')plt.title('learning rate is 0.0075')plt.show()def L_layer_model(X, Y, layer_dims,learning_rate=0.0075,num_iterations=2000):parameters = StartDeepNeural.initialize_parameters_deep(layer_dims)costs = []# return W1,W2,b1,b2for i in range(0, num_iterations):# A1, cache1=StartDeepNeural.linear_activation_forward(X,W1,b1,activation='relu')# A2, cache2 = StartDeepNeural.linear_activation_forward(A1, W2, b2, activation='sigmoid')AL, caches = StartDeepNeural.L_model_forward(X, parameters)  ##[ [( (X W1 b1 ),Z1)],[( (A1 W2 b2 ),Z2) ]]cost = StartDeepNeural.compute_cost(AL, Y)grads = StartDeepNeural.L_model_backward(AL, Y, caches)parameters = StartDeepNeural.update_parameters(parameters, grads, learning_rate)if i % 100 == 0:#print(grads)print('iterations{}:cost {}'.format(i, cost))costs.append(cost)return costs, parameters
def print_mislabel_images(classes,test_x,test_y_orig,p_test):a=test_y_orig+p_testmislable_index=np.asarray(np.where(a==1))#[[1,0]] [[1,1]]np.where 返回(array([0]),array([1]))##np.asarray 返回 array([[0],[1]])plt.figure(figsize=(40, 40))  # set default size of plots  画布大小 4000×4000num_images=len(mislable_index[0])for i in range(num_images):index=mislable_index[1][i]plt.subplot(2,num_images,i+1)plt.imshow(test_x[:,index].reshape(64,64,3))#plt.axis('off')plt.title('prediction '+classes[int(p_test[0][index])].decode('utf-8')+' real '+classes[int(test_y_orig[0][index])].decode('utf-8'))plt.savefig('1.jpg')
def L_layer_model_test():layers_dims = [12288, 20, 7, 5, 1]costs,parameters = L_layer_model(train_x, train_y_orig, layers_dims)accuracy , p_train= predict(train_x, train_y_orig, parameters)print('train accuracy is {}'.format(accuracy))accuracy , p_test= predict(test_x, test_y_orig, parameters)print('test accuracy is {}'.format(accuracy))print_mislabel_images(classa,test_x,test_y_orig,p_test)# plt.plot(costs)# plt.xlabel('iterations')# plt.ylabel('costs')# plt.title('learning rate is 0.0075')plt.show()
if __name__=='__main__':L_layer_model_test()

 

打印结果:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/493632.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

A-KAZE论文研读

AKAZE是KAZE的加速版本。KAZE在构建非线性空间的过程中很耗时&#xff0c;在AKAZE中将Fast Explicit Diffusion(FED)加入到金字塔框架可以dramatically speed-up。在描述子方面&#xff0c;AKAZE使用了更高效的Modified Local Difference Binary(M-LDB)&#xff0c;可以从非线性…

和你抢“饭碗”的40家服务机器人企业大盘点!

来源&#xff1a;物联网智库摘要&#xff1a;本文将对国内近40家服务机器人企业进行汇总介绍&#xff0c;所选企业在其相应版块活跃度较高。从三个大类进行了细分盘点。国家机器人联盟&#xff08;IFR&#xff09;根据应用环境将机器人分为了工业机器人和服务机器人。服务机器人…

YOLO9000

YOLO9000是YOLO的第三个版本。前两个版本是YOLO v1&#xff0c;YOLO v2&#xff0c;在CVPR2017的文章《Better,Faster,Stronger》中的前半部分都是对前两个版本的介绍&#xff0c;新的内容主要在Stronger部分。YOLO9000中的9000指的是YOLO可以对超过9000种图像进行分类。 Bett…

吴恩达作业4:权重初始化

权重初始化的 正确选择能够有效的避免多层神经网络传播过程中的梯度消失和梯度爆炸问题&#xff0c;下面通过三个初始化的方法来验证&#xff1a; sigmoid导数函数&#xff1a;最大值小于0.25&#xff0c;故经过多层反向传播以后&#xff0c;会导致最初的层&#xff0c;权重无…

先发制人!Waymo将首推商用载人自动驾驶服务,Uber们怕不怕?

编译&#xff1a;费棋来源&#xff1a;AI科技大本营“真的&#xff0c;真的很难。”11 月举办的一场会议上&#xff0c;Alphabet 旗下 Waymo CEO John Krafcik 对做自动驾驶汽车技术的艰难不无感慨。在他看来&#xff0c;未来几十年内&#xff0c;自动驾驶汽车将一直存在限制&a…

利用ORB/AKAZE特征点进行图像配准

Kp1,kp2都是list类型&#xff0c;两幅图都是500个特征点。这和ORB论文中的数据是一样的。4.4章节 Matches也是list类型&#xff0c;找到325个匹配对。 AKAZE文章中提到一个指标&#xff1a;MS(matching score)# Correct Matches/# Features, 如果overlap area error 小于40%…

吴恩达作业5:正则化和dropout

构建了三层神经网络来验证正则化和dropout对防止过拟合的作用。 首先看数据集&#xff0c;reg_utils.py包含产生数据集函数&#xff0c;前向传播&#xff0c;计算损失值等&#xff0c;代码如下&#xff1a; import numpy as np import matplotlib.pyplot as plt import h5py …

十年之后,数字孪生将这样改变我们的工作与生活

来源&#xff1a;资本实验室数字孪生是近几年兴起的非常前沿的新技术&#xff0c;简单说就是利用物理模型&#xff0c;使用传感器获取数据的仿真过程&#xff0c;在虚拟空间中完成映射&#xff0c;以反映相对应的实体的全生命周期过程。在未来&#xff0c;物理世界中的各种事物…

什么是图像

图像&#xff0c;尤其是数字图像的定义&#xff0c;在冈萨雷斯的书中是一个二维函数f(x,y),x,y是空间平面坐标&#xff0c;幅值f是图像在该点处的灰度或者强度。下面通过OpenCV中最常用的图像表示方法Mat来看一下在计算机中是怎么定义图像的。 Mat的定义 OpenCV在2.0之后改用…

吴恩达作业6:梯度检验

梯度检验的目的就是看反向传播过程中的导数有没有较大的误差&#xff0c;首先看Jtheta*x的梯度检验&#xff1a;代码如下 import numpy as np """ Jx*theta的前向传播 """ def forward_propagation(x,theta):Jx*thetareturn J ""&quo…

10年后的计算机会是怎样的?

作者&#xff1a;孙鹏&#xff08;剑桥大学计算机系博士&#xff09;来源&#xff1a;新原理研究所上个世纪三十年代&#xff0c;邱奇和图灵共同提出了通用计算机的概念[1]。在接下来的十多年里&#xff0c;因为战争需要下的国家推动&#xff0c;计算机得以很快从理论发展成为实…

什么是图像变换

还是看OpenCV官方手册&#xff0c;我觉得这样可以同时学习如何使用函数和如何理解一些基本概念。 首先&#xff0c;这里的几何变换geometrical transformations是针对2D图像而言的&#xff0c;不改变图像内容而是将像素网格变形deform the pixel grid&#xff0c;映射到目标图…

MSRA20周年研究趋势文章|图像识别的未来:机遇与挑战并存

文/微软亚洲研究院 代季峰 林思德 郭百宁识别图像对人类来说是件极容易的事情&#xff0c;但是对机器而言&#xff0c;这也经历了漫长岁月。在计算机视觉领域&#xff0c;图像识别这几年的发展突飞猛进。例如&#xff0c;在 PASCAL VOC 物体检测基准测试中&#xff0c;检测器的…

吴恩达作业7:梯度下降优化算法

先说说BatchGD用整个训练样本进行训练得出损失值&#xff0c;SGD是只用一个训练样本训练就得出损失值&#xff0c;GD导致训练慢&#xff0c;SGD导致收敛到最小值不平滑&#xff0c;故引入Mini-batch GD&#xff0c;选取部分样本进行训练得出损失值&#xff0c; 普通梯度下降算…

什么是单应矩阵和本质矩阵

知乎上面的大牛还是很多&#xff0c;直接搜Homography或者单应矩阵就能得到很多大神的回答&#xff0c;可能回答中的一句话或者一个链接就够自己学习很久。 其实在之前研究双目视觉的时候就接触了对极几何&#xff0c;通过视觉就可以得到物体的远近信息&#xff0c;这也是特斯…

tensorflow实现反卷积

先看ogrid用法 from numpy import ogrid,repeat,newaxis from skimage import io import numpy as np size3 x,yogrid[:size,:size]#第一部分产生多行一列 第二部分产生一行多列 print(x) print(y) 打印结果&#xff1a; newaxis用法&#xff1a; """ newaxis…

寿命能推算吗?加州大学科学家提出“预测方法”

来源&#xff1a;中国科学报从古至今&#xff0c;从国内到国外&#xff0c;从炼丹术到现代科学&#xff0c;长生不老似乎一直是人类乐此不疲的追求。但若要延缓衰老&#xff0c;首先要弄清是什么造成了衰老。近日&#xff0c;加州大学洛杉矶分校&#xff08;UCLA&#xff09;生…

Deep Image Homography Estimation

在知乎问题&#xff1a;深度学习应用在哪些领域让你觉得「我去&#xff0c;这也能行&#xff01;」&#xff1f;中遇到一篇提交在arXiv 2016&#xff08;arXiv不是正式发表&#xff0c;只是可以证明原创性&#xff0c;提供时间戳的网站&#xff09;的文章《Deep Image Homograp…

tensorflow:双线性插值反卷积

首先生成333的黑色图片 """ 生成333黑色图像 """ def produce_image():size 3x, y ogrid[:size, :size] # 第一部分产生多行一列 第二部分产生一行多列z x yz z[:, :, newaxis] # 增加第三维# print(z)img repeat(z, 3, 2)/12 # 在第三…

腾讯医疗AI新突破:提出器官神经网络,全自动辅助头颈放疗规划 | 论文

来源&#xff1a;量子位腾讯医疗AI实验室又有新研究。这次跟美国加州大学合作&#xff0c;在国际权威期刊《Medical Physics》发表最新研究成果&#xff1a;《器官神经网络&#xff1a;深度学习用于快速和全自动整体头颈危及器官靶区勾画》AnatomyNet: Deep Learning for Fast …