import numpy as np
import matplotlib.pyplot as plt
import h5py
from testCase import *
from dnn_utils import sigmoid, sigmoid_backward, relu, relu_backward, tanh, tanh_backward
import lr_utils
# Customize the structure of neural network
layer_dims = [1, 4, 1] # units on each layer
forward_funcs = [relu, sigmoid] # activate functions in forward-propagate process
learning_rate = 0.005
num_iteration = 2500
def nn_initialize(layer_dimensions):
np.random.seed(3)
params = {}
L = len(layer_dimensions)
for l in range(1, L):
params['W' + str(l)] = np.random.randn(layer_dimensions[l], layer_dimensions[l - 1]) * 0.01
params['b' + str(l)] = np.zeros([layer_dimensions[l], 1])
assert (params['W' + str(l)].shape == (layer_dimensions[l], layer_dimensions[l - 1]))
assert (params['b' + str(l)].shape == (layer_dimensions[l], 1))
return params
def forward(input_x, params, forward_functions):
L = len(forward_functions)
cache = {'A0': input_x}
for l in range(1, L + 1):
# retrive W[l], A[l-1], b[l]
Wl = params['W' + str(l)]
bl = params['b' + str(l)]
Al_1 = cache['A' + str(l - 1)]
Zl = np.dot(Wl, Al_1) + bl
cache['Z' + str(l)] = Zl
cache['A' + str(l)] = forward_functions[l - 1](Zl)
return cache
def compute_cost(AL, Y):
m = Y.shape[1]
cost = np.dot(Y, np.log(AL.T)) + np.dot(1 - Y, np.log(1 - AL.T))
cost /= (-m)
cost = cost.item()
return cost
def backward(Y, cache, params, backward_functions):
m = Y.shape[1]
L = len(backward_functions)
grads = {}
Al = cache['A' + str(L)]
grads['dA' + str(L)] = -(np.divide(Y, Al) - np.divide(1 - Y, 1 - Al))
for i, backfunc in enumerate(reversed(backward_functions)):
l = L - i
dAl = grads['dA' + str(l)]
Zl = cache['Z' + str(l)]
# dZl = 0
if backfunc == relu_backward:
dZl = relu_backward(dAl, Zl)
elif backfunc == sigmoid_backward:
dZl = sigmoid_backward(dAl, Zl)
elif backfunc == tanh_backward:
dZl = tanh_backward(dAl, Zl)
dWl = np.dot(dZl, cache['A' + str(l - 1)].T) / m
dbl = np.sum(dZl, axis=1, keepdims=True) / m
# We don't need to cache dA[0], namely dX
if l != 1:
dAl_1 = np.dot(params['W' + str(l)].T, dZl)
grads['dA' + str(l - 1)] = dAl_1
grads['dW' + str(l)] = dWl
grads['db' + str(l)] = dbl
return grads
def update(params, grads, learning_rate):
L = int(len(params) / 2)
for l in range(1, L + 1):
params['W' + str(l)] -= learning_rate * grads['dW' + str(l)]
params['b' + str(l)] -= learning_rate * grads['db' + str(l)]
return params
def dnn(input_X, Y, nn_structure, forward_functions, learning_rate, num_iterations):
# generate backward derivative functions according to forward activate functions
backward_functions = []
for func in forward_funcs:
if func == relu:
backward_functions.append(relu_backward)
elif func == sigmoid:
backward_functions.append(sigmoid_backward)
elif func == tanh:
backward_functions.append(tanh_backward)
# Redefine the number of features in nn_structure according to input_X.shape
nn_structure[0] = input_X.shape[0]
params = nn_initialize(nn_structure)
costs = []
L = len(forward_functions)
for i in range(num_iterations):
cache = forward(input_X, params, forward_functions)
AL = cache['A' + str(L)]
if i % 10 == 0:
costs.append(compute_cost(AL, Y))
grads = backward(Y, cache, params, backward_functions)
params = update(params, grads, learning_rate)
return params, costs
# Load training and test dataset and process
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = lr_utils.load_dataset()
train_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
train_x = train_x_flatten / 255
train_y = train_set_y
test_x = test_x_flatten / 255
test_y = test_set_y
parameters, Costs = dnn(train_x, train_y, layer_dims, forward_funcs, learning_rate, num_iteration)
plt.plot(Costs)
plt.title('Cost function')
plt.xlabel('iteration(×10)')
plt.ylabel('Cost')
plt.show()
def predict(X, Y, params, forward_functions):
L = len(forward_functions)
AL = forward(X, params, forward_functions)['A' + str(L)]
Y_predict = np.round(AL)
m = Y.shape[1]
accuracy = (m - np.sum(np.abs(Y - Y_predict))) / m
return accuracy
train_acc = predict(train_x, train_y, parameters, forward_funcs)
test_acc = predict(test_x, test_y, parameters, forward_funcs)
print(f'The training accuracy is:{train_acc*100}%')
print(f'The test accuracy is: {test_acc * 100}%')
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Week4_build_a_deep_NN.zip (31个子文件)
Week4_build_a_deep_NN
My_model
testCase.py 3KB
lr_utils.py 889B
DNN_training.py 5KB
__pycache__
planar_utils.cpython-39.pyc 2KB
dnn_utils.cpython-39.pyc 2KB
lr_utils.cpython-39.pyc 790B
testCase.cpython-39.pyc 3KB
dnn_utils.py 2KB
testCase.py 3KB
planar_utils.py 2KB
.idea
Week4_build_a_deep_NN.iml 454B
workspace.xml 4KB
misc.xml 195B
inspectionProfiles
profiles_settings.xml 174B
modules.xml 301B
.gitignore 50B
datasets
train_catvnoncat.h5 2.45MB
test_catvnoncat.h5 602KB
lr_utils.py 889B
pics
6.jpg 3KB
1.jpg 472KB
5.jpg 19KB
3.jpg 6KB
4.jpg 21KB
__pycache__
planar_utils.cpython-39.pyc 2KB
dnn_utils.cpython-39.pyc 2KB
lr_utils.cpython-39.pyc 781B
testCase.cpython-39.pyc 3KB
.ipynb_checkpoints
Build and Customize a Deep Neural Network-checkpoint.ipynb 148KB
Build and Customize a Deep Neural Network.ipynb 148KB
dnn_utils.py 2KB
共 31 条
- 1
资源评论
火卫二德莫斯
- 粉丝: 10
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功