资源简介
本例中包含两层BP神经网络模板程序(可以直接调用,可定制中间层神经元个数,设置学习率,绘制衰减曲线,可用于简单的模式识别和预测)、一个调用的例程(包括简单的数据预处理如归一化的使用,测试结果准确率为98.3%)、一份鸢尾花处理后的数据和原始数据。欢迎下载。
代码片段和文件信息
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
#
# @Version : 1.0
# @Time : 2018/6/5
# @Author : 圈圈烃
# @File : Forward_NeuralNetwork
import numpy as np
import matplotlib.pyplot as plt
from planar_utils import plot_decision_boundary load_planar_dataset load_extra_datasets
def sigmoid(x):
“““
Compute the sigmoid of x
(计算x的sigmoid函数值)
:param x: A scalar or numpy array of any size (一个数或是一个一个任意大小的numpy数组)
:return: sigmoid(x) (计算值s)
“““
s = 1/(1+np.exp(-x))
return s
def layer_size(X Y):
“““
:param X: input dataset of shape (input size number of examples) (输入数据集大小(几个属性,样本量))
:param Y: labels of shape (output size number of exmaples) (标签数据大小(标签数,样本量))
:return:
n_x: the size of the input layer
n_y: the size of the output layer
“““
n_x = X.shape[0]
n_y = Y.shape[0]
return (n_x n_y)
def initialize_parameters(n_x n_h n_y):
“““
initialize_parameters
(参数初始化)
:param n_x: size of the input layer
:param n_h: size of the hidden layer
:param n_y: size of the output layer
:return:
W1: weight matrix of shape (n_h n_x) (第1层的权重矩阵(n_h n_x))
b1: bias vector of shape (n_h 1) (第1层的偏移量向量(n_h 1))
W2: weight matrix of shape (n_y n_h) (第2层的权重矩阵(n_y n_h))
b2: bias vector of shape (n_y 1) (第2层的偏移量向量(n_y 1))
“““
# np.random.seed(2) #Random initialization (随机种子初始化参数)
W1 = np.random.randn(n_h n_x) * 0.01
b1 = np.zeros((n_h 1))
W2 = np.random.randn(n_y n_h) * 0.01
b2 = np.zeros((n_y 1))
parameters = {
‘W1‘: W1
‘b1‘: b1
‘W2‘: W2
‘b2‘: b2
}
return parameters
def forward_propagation(X parameters):
“““
forward_propagation
(正向传播)
:param X: input data of size (n_x m) (输入数据集X)
:param parameters: python dictionary containing your parameters (output of initialization function) (字典类型, 权重以及偏移量参数)
:return:
A2: The sigmoid output of the second activation (第2层激活函数sigmoid函数输出向量)
cache: a dictionary containing “Z1“ “A1“ “Z2“ and “A2“ (字典类型包含“Z1“ “A1“ “Z2“ “A2“)
“““
W1 = parameters[‘W1‘]
b1 = parameters[‘b1‘]
W2 = parameters[‘W2‘]
b2 = parameters[‘b2‘]
Z1 = np.dot(W1 X) + b1
A1 = np.tanh(Z1) #第1层激活函数选择tanh
Z2 = np.dot(W2 A1) + b2
A2 = sigmoid(Z2) #第2层激活函数选择sigmod
assert (A2.shape == (1 X.shape[1])) #若A2的大小和((1 X.shape[1])) 则直接报异常
cache = {
‘Z1‘: Z1
‘A1‘: A1
‘Z2‘: Z2
‘A2‘: A2
}
return A2 cache
def compute_cost(A2 Y parameters):
“““
compute cost
(计算成本函数)
:param A2: The sigmoid output of the second activation of shape (1 number of examples) (第2层激活函数sigmoid函数输出向量)
:param Y: “true“ labels vector of shape (1 number of examples) (正确标签向量)
:param parameters: python dictionary c
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论