吴恩达-神经网络-week1-hw3
Ref:https://blog.csdn.net/u013733326/article/details/79702148
点击查看代码
from testCases import *
from planar_utils import plot_decision_boundary, \
sigmoid, load_planar_dataset, load_extra_datasets
import sklearn
import sklearn.datasets
import sklearn.linear_model
import numpy as np
import matplotlib.pyplot as plt
import traceback
np.random.seed(1)
X, Y = load_planar_dataset()
plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral)
# plt.scatter(X[0, :], X[1, :], c=np.squeeze(Y), s=40, cmap=plt.cm.Spectral)
# --------------------dimension------------------------------------------
shape_X = X.shape
shape_Y = Y.shape
m = shape_Y[1] # the number of training set
print("X的维度: " + str(shape_X))
print("Y的维度: " + str(shape_Y))
print("数据集的数据个数: " + str(m) + "个")
# ------------------Logistics Reg----------------------------------------
print('======================Logistics Regression===============================')
LR_model = sklearn.linear_model.LogisticRegressionCV()
LR_model.fit(X.T, Y.T)
plt.figure()
plot_decision_boundary(lambda x: LR_model.predict(x), X, Y)
plt.title("Logistic Regression")
LR_predictions = LR_model.predict(X.T)
accur = (np.dot(Y, LR_predictions) + np.dot(1 - Y, 1 - LR_predictions)) / float(Y.size) * 100
print("逻辑回归准确性:%d" % float(accur)
+ '% ' + '(正确标记的数据点所占的百分比)') # 47%
# -------------------NN----------------------------------------------------
def layers_size(X, Y, h_layers=4):
"""
:param X: Input features
:param Y: Label
:param h_layers: # ( the hidden layers )
:return: the number of input layer, the hidden of input layer, the number of output layer
"""
n_x = X.shape[0] # input
n_h = h_layers # hidden layer
n_y = Y.shape[0] # output
return (n_x, n_h, n_y)
def initial_parameter(n_x, n_h, n_y):
"""
W[i] 的维度是 (n[i], n[i-1])
b[i] 的维度是 (n[i], 1)
:param n_x: the number of input layer
:param n_h: the number of hidden layer
:param n_y: the number of output layer
:return: 初始化的参数
"""
np.random.seed(2)
W1 = np.random.randn(n_h, n_x) * 0.02
b1 = np.zeros(shape=(n_h, 1))
W2 = np.random.randn(n_y, n_h) * 0.02
b2 = np.zeros(shape=(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))
paras = {
'W1': W1,
'b1': b1,
'W2': W2,
'b2': b2,
}
return paras
def forward_propagation(X, paras):
"""
two layers nn model, 计算前向传播, tanh 和 SIGMOID 激活函数
:param X:
:param paras:
:return:
"""
W1 = paras['W1']
b1 = paras['b1']
W2 = paras['W2']
b2 = paras['b2']
Z1 = np.dot(W1, X) + b1
A1 = np.tanh(Z1)
Z2 = np.dot(W2, A1) + b2
A2 = sigmoid(Z2)
assert (A2.shape == (1, X.shape[1]))
cache = {
'Z1': Z1,
'A1': A1,
'Z2': Z2,
'A2': A2,
}
return (A2, cache)
def compute_cost(A2, Y):
"""
this function compute the cost,
:param A2: the activation of second layer
:param Y: label: 0(not cat); 1(cat)
:return: the cost of 2 layers nn model
"""
try:
m = Y.shape[1]
log_prob = np.multiply(np.log(A2), Y) + np.multiply((1 - Y), np.log(1 - A2))
cost = - np.sum(log_prob) / m
cost = float(np.squeeze(cost))
assert (isinstance(cost, float))
return cost
except:
print(traceback.print_exc())
def back_propagation(paras, cache, X, Y):
"""
the backward propagation of two layers nn model
:param paras: the parameters of nn model
:param cache: the activations of nn model
:param X: input features
:param Y: label
:return: gradient: dW2, db2, dW1, db1
"""
m = X.shape[1]
W1 = paras['W1']
W2 = paras['W2']
A1 = cache['A1']
A2 = cache['A2']
dZ2 = A2 - Y
dw2 = (1 / m) * np.dot(dZ2, A1.T)
db2 = (1 / m) * np.sum(dZ2, axis=1, keepdims=True)
dZ1 = np.multiply(np.dot(W2.T, dZ2), 1 - np.power(A1, 2))
dw1 = (1 / m) * np.dot(dZ1, X.T)
db1 = (1 / m) * np.sum(dZ1, axis=1, keepdims=True)
grads = {
'dW1': dw1,
'db1': db1,
'dW2': dw2,
'db2': db2
}
return grads
def update_params(params, grads, alpha=0.01):
"""
update the parameters of nn model according to the gradient of backward propagation
:param params: the parameters of the last iteration in the path of backward propagation
:param grads: the gradient of backward propagation
:param alpha: learning rate, default is 0.01
:return: the updated parameters
"""
W1, W2 = params['W1'], params['W2']
b1, b2 = params['b1'], params['b2']
dw1, dw2 = grads['dW1'], grads['dW2']
db1, db2 = grads['db1'], grads['db2']
W1 = W1 - alpha * dw1
W2 = W2 - alpha * dw2
b1 = b1 - alpha * db1
b2 = b2 - alpha * db2
parameters = {
'W1': W1,
'b1': b1,
'W2': W2,
'b2': b2
}
return parameters
def nn_model(X, Y, n_h=4, num_iterations=10000, print_cost=False):
"""
two layers of nn model,
:param X: input features
:param Y: output, the label
:param n_h: the number of the hidden layer
:param iterations: the number of the iterations
:param print_cost: default is False
:return: the parameters of the nn model which have been trained
"""
np.random.seed(3)
n_x = layers_size(X, Y)[0]
n_y = layers_size(X, Y)[2]
params = initial_parameter(n_x, n_h, n_y)
for i in range(num_iterations):
A2, cache = forward_propagation(X, params)
cost = compute_cost(A2, Y)
grads = back_propagation(params, cache, X, Y)
params = update_params(params, grads, alpha=0.5)
if i % 1000 == 0 and print_cost:
print(f'第{i}次循环成本为:' + str(cost))
return params
def predict_y(X, params):
"""
:param X: input features
:param params:
:return: 0 or 1
"""
A2, cache = forward_propagation(X, params)
predict_y = np.round(A2)
return predict_y
print('======================Two layers nn model===============================')
parameters = nn_model(X, Y, n_h=4, num_iterations=10000, print_cost=True)
prediction_y = predict_y(X, parameters)
accu = float((np.dot(Y, prediction_y.T) + np.dot(1 - Y, 1 - prediction_y.T)) / float(Y.size) * 100)
# print('准确率: %.1f' % accu + "%")
print(f'Hidden layer: 的准确率为{accu}')
# figure
plt.figure()
plot_decision_boundary(lambda x: predict_y(x.T, parameters), X, Y)
plt.title('Decision Boundary for hidden layer size ' + str(4))
# Multi hidden layers
plt.figure(figsize=(16, 32))
hidde_layers = [1, 2, 4, 6, 10, 20, 50]
for i, n_h in enumerate(hidde_layers):
params = nn_model(X, Y, n_h, num_iterations=2000, print_cost=False)
prediction_y = predict_y(X, params)
plt.subplot(5, 2, i + 1)
plot_decision_boundary(lambda x: predict_y(x.T, parameters), X, Y)
plt.title(f'Decision Boundary for hidden layer size {n_h}')
accu = float((np.dot(Y, prediction_y.T) + np.dot(1 - Y, 1 - prediction_y.T)) / float(Y.size) * 100)
print(f'Hidden layer: {n_h}的准确率为{accu}')
plt.savefig('hidden layer.png')
if __name__ == "__main__":
pass
吴恩达-神经网络-week1-hw3的更多相关文章
- coursera-斯坦福-机器学习-吴恩达-笔记week1
1 Introduction 1.1 概念:一个程序被认为能从经验E中学习,解决任务 T,达到性能度量值P,当且仅当, 有了经验E后,经过P评判, 程序在处理 T 时的性能有所提升. 1.2 机器学习 ...
- 吴恩达--神经网络-week1-hw4
# Ref: https://blog.csdn.net/u013733326/article/details/79767169 import numpy as np import testCases ...
- 吴恩达深度学习第1课第4周-任意层人工神经网络(Artificial Neural Network,即ANN)(向量化)手写推导过程(我觉得已经很详细了)
学习了吴恩达老师深度学习工程师第一门课,受益匪浅,尤其是吴老师所用的符号系统,准确且易区分. 遵循吴老师的符号系统,我对任意层神经网络模型进行了详细的推导,形成笔记. 有人说推导任意层MLP很容易,我 ...
- 【吴恩达课后测验】Course 1 - 神经网络和深度学习 - 第二周测验【中英】
[中英][吴恩达课后测验]Course 1 - 神经网络和深度学习 - 第二周测验 第2周测验 - 神经网络基础 神经元节点计算什么? [ ]神经元节点先计算激活函数,再计算线性函数(z = Wx + ...
- 【吴恩达课后测验】Course 1 - 神经网络和深度学习 - 第一周测验【中英】
[吴恩达课后测验]Course 1 - 神经网络和深度学习 - 第一周测验[中英] 第一周测验 - 深度学习简介 和“AI是新电力”相类似的说法是什么? [ ]AI为我们的家庭和办公室的个人设备供电 ...
- Python3 反向传播神经网络-Min-Batch(根据吴恩达课程讲解编写)
# -*- coding: utf-8 -*- """ Created on Sat Jan 20 13:47:54 2018 @author: markli " ...
- 吴恩达deepLearning.ai循环神经网络RNN学习笔记_看图就懂了!!!(理论篇)
前言 目录: RNN提出的背景 - 一个问题 - 为什么不用标准神经网络 - RNN模型怎么解决这个问题 - RNN模型适用的数据特征 - RNN几种类型 RNN模型结构 - RNN block - ...
- 吴恩达deepLearning.ai循环神经网络RNN学习笔记_没有复杂数学公式,看图就懂了!!!(理论篇)
本篇文章被Google中国社区组织人转发,评价: 条理清晰,写的很详细! 被阿里算法工程师点在看! 所以很值得一看! 前言 目录: RNN提出的背景 - 一个问题 - 为什么不用标准神经网络 - RN ...
- 用纯Python实现循环神经网络RNN向前传播过程(吴恩达DeepLearning.ai作业)
Google TensorFlow程序员点赞的文章! 前言 目录: - 向量表示以及它的维度 - rnn cell - rnn 向前传播 重点关注: - 如何把数据向量化的,它们的维度是怎么来的 ...
随机推荐
- wpf 的style
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- 【mysql】用户和权限管理
1.用户管理 相关命令如下 命令 描述 备注 create user zhang3 identified by '123123'; 创建名称为zhang3 的用户,密码设为123123: sele ...
- layui关闭弹出框
layer.close(index) - 关闭特定层 //当你想关闭当前页的某个层时 var index = layer.open(); var index = layer.alert(); var ...
- 关于Ubuntu18.04 linux系统下使用Tim QQ 微信
先配上张图 步骤: 1.1 :需要安装环境deepin-wine 1.1:(你把他理解为jdk就好,没有jdk无法运行java程序,同理没有deepin-wine环境无法运行腾讯产品) 1.2 :去哪 ...
- 【GIS】点图层符号的方向和大小
方向:根据属性字段设置点图层中每个要素的符号方向和大小, 1 所有要素使用同一种符号---简单渲染 在图层属性---符号系统---单一符号中进行设置,首先设置符号,在后面的[高级]选项按钮中分别设置[ ...
- idea项目在maven projects中显示灰色的解决办法。建新建module src变成标准的文件夹
在使用idea的过程中,有时会遇到其中一个maven模块变成灰色(可以通过view - tool windows -> maven projects 现实),如下所示: 造成这个的原因可能是忽略 ...
- nacos配置
server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: #nacos 服务 ...
- idea控制台中文乱码解决办法
也可以通过idea右下角的设置,但是properties文件是不能设置的,这个只能在file->setting->file encodings 设置
- HCNP Routing&Switching之OSPF LSA类型(二)
前文我们了解了OSPF的一类.二类.三类LSA,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15209829.html:今天我们来聊一聊OSPF的四类和五类L ...
- 【HMS Core 6.0全球上线】Network Kit全链路网络加速技术,应用无惧网络拥塞
HMS Core 6.0已于7月15日全球上线,本次版本向广大开发者开放了众多全新能力与技术.其中HMS Core Network Kit开放了全链路网络加速技术,助力开发者为用户提供低时延的畅快网络 ...