您的位置:首页 > 理论基础 > 计算机网络

吴恩达老师的公开课,简单神经网络的作业总结

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的下次迭代的值

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)

/////////////////////////////

如果要训练深度学习,网络深度,模型设计,还有数据集的制作,这些都需要仔细学习一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐