吴恩达老师的公开课,简单神经网络的作业总结
2017-11-07 09:59
253 查看
定义四个变量
train_set_x shape: (209, 64, 64, 3)
train_set_y shape: (1, 209)
test_set_x shape: (50, 64, 64, 3)
test_set_y shape: (1, 50)
分别为训练集的X,Y,测试集的X,Y。
这里有一点需要注意,作业中直接导入的文件train_catvnoncat.h5和test_catvnoncat.h5两个文件,如果后面需要训练自己的数据,需要建立数据集
//////////////////////////////////////////
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
建立了两个为(64×63×3,209)和(64×64×3,50)这样的形式,作为两个新的变量
为什么上面的语句可以实现这一点,需要继续学习
////////////////////////////////////////
初始化定义w,b,其中w为(m,1)的形式,其中m为输入的列向量的维度
函数命名为initialize_with_zeros(dim)
////////////////////////////////////////
进行正向传播和损失函数的计算
m = X.shape[1]
A = sigmoid(np.dot(w.T,X)+b)
cost = -np.sum((np.dot(Y,np.log(A).T)+np.dot((1-Y),np.log(1-A).T)))/m
dw = np.dot(X,(A-Y).T)/m
db = np.sum(A-Y)/m
上面为计算公式,下面的为声明和一个计算,不是很懂
assert(dw.shape == w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape == ())
在写程序时出现了一个问题:dw = 1/m×np.dot(X,(A-Y).T)运行一直为0,不是很懂,但是需要注意
函数命名为propagate(w, b, X, Y)
///////////////////////////////////////
反响传播计算,利用学习率计算w,b的下次迭代的值
函数命名为optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False)
////////////////////////////////////
对结果进行预测
///////////////////////////////
函数命名完成,需要进行训练,先建立model,然后训练数据集
w, b = initialize_with_zeros(X_train.shape[0])
上面的语句显示最终的正确率,如何计算得到的,还不是很懂,python接触时间太短,需要了解
函数命名为model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False)
/////////////////////////////
如果要训练深度学习,网络深度,模型设计,还有数据集的制作,这些都需要仔细学习一下
train_set_x shape: (209, 64, 64, 3)
train_set_y shape: (1, 209)
test_set_x shape: (50, 64, 64, 3)
test_set_y shape: (1, 50)
分别为训练集的X,Y,测试集的X,Y。
这里有一点需要注意,作业中直接导入的文件train_catvnoncat.h5和test_catvnoncat.h5两个文件,如果后面需要训练自己的数据,需要建立数据集
//////////////////////////////////////////
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
建立了两个为(64×63×3,209)和(64×64×3,50)这样的形式,作为两个新的变量
为什么上面的语句可以实现这一点,需要继续学习
////////////////////////////////////////
初始化定义w,b,其中w为(m,1)的形式,其中m为输入的列向量的维度
函数命名为initialize_with_zeros(dim)
////////////////////////////////////////
进行正向传播和损失函数的计算
m = X.shape[1]
A = sigmoid(np.dot(w.T,X)+b)
cost = -np.sum((np.dot(Y,np.log(A).T)+np.dot((1-Y),np.log(1-A).T)))/m
dw = np.dot(X,(A-Y).T)/m
db = np.sum(A-Y)/m
上面为计算公式,下面的为声明和一个计算,不是很懂
assert(dw.shape == w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape == ())
在写程序时出现了一个问题:dw = 1/m×np.dot(X,(A-Y).T)运行一直为0,不是很懂,但是需要注意
函数命名为propagate(w, b, X, Y)
///////////////////////////////////////
反响传播计算,利用学习率计算w,b的下次迭代的值
costs = [] for i in range(num_iterations): 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 ("Cost after iteration %i: %f" %(i, cost))
函数命名为optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False)
////////////////////////////////////
对结果进行预测
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)) 函数命名为predict(w, b, X)
///////////////////////////////
函数命名完成,需要进行训练,先建立model,然后训练数据集
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("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}
上面的语句显示最终的正确率,如何计算得到的,还不是很懂,python接触时间太短,需要了解
函数命名为model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False)
/////////////////////////////
如果要训练深度学习,网络深度,模型设计,还有数据集的制作,这些都需要仔细学习一下
相关文章推荐
- Coursera吴恩达机器学习课程 总结笔记及作业代码——第4周神经网络
- Coursera吴恩达机器学习课程 总结笔记及作业代码——第5周神经网络续
- Coursera deep learning 吴恩达 神经网络和深度学习 第二周 编程作业 Logistic Regression with a Neural Network mindset
- Coursera 深度学习 deep learning.ai 吴恩达 神经网络和深度学习 第一课 第二周 编程作业 Python Basics with Numpy
- Coursera deep learning 吴恩达 神经网络和深度学习 第四周 编程作业 Building your Deep Neural Network
- 【中文】【吴恩达课后编程作业】Course 1 - 神经网络和深度学习 - 第三周作业
- 吴恩达 DeepLearning 神经网络基础 第一课第三周编程题目及作业
- 【中文】【吴恩达课后编程作业】Course 1 - 神经网络和深度学习 - 第四周作业(1&2)
- Coursera deep learning 吴恩达 神经网络和深度学习 第三周 编程作业 Planar data classification with one hidden layer
- 吴恩达老师深度学习视频课笔记:单隐含层神经网络公式推导及C++实现(二分类)
- Coursera 吴恩达 Deep Learning 第二课 改善神经网络 Improving Deep Neural Networks 第三周 编程作业代码 Tensorflow Tutorial
- Coursera 吴恩达 Deep Learning 第二课 改善神经网络 Improving Deep Neural Networks 第二周 编程作业代码Optimization methods
- 吴恩达神经网络反向传播算法matlab作业疑问的解答
- 吴恩达老师深度学习视频课笔记:多隐含层神经网络公式推导(二分类)
- 【中文】【吴恩达课后编程作业】Course 1 - 神经网络和深度学习 - 第二周作业
- 《神经网络和深度学习》之神经网络基础(第二周)课后作业——神经网络思维的逻辑回归
- 简单神经网络(复习用)
- BP简单的理解神经网络
- Coursera吴恩达机器学习课程 总结笔记及作业代码——第7周支持向量机
- BP 神经网络 定位算法实现过程总结