neural networks logistic regression 神经网络逻辑回归
机器学习笔记 - 吴恩达 - 目录
笔记
描述
神经网络是什么?
通过多层的网络节点,实现复杂的特征提取。
为什么要使用神经网络?
单层的网络很难获取到数据更深层次的特征,通过建立多节点,深层次的网络可以挖掘数据更深层次的特征。
和不使用神经网络的分类,有什么直观的不同?
不使用神经网络的话,一层的网络只能一条直线分过去,而二层时就使用了更复杂的划分曲线。
可以玩玩这个网址playground.tensorflow提供的可视化功能,可以发现神经网络层数越多,分类的方式越精细。且每层其实都实现一定的权值分配,最终组合起来实现复杂分类效果。
最直观的认识:在图像分类中,最终训练后的网络各节点的权值所组成的图像看起来是一张模糊的图像(没找到那种图),由大量的训练图形合成,当预测时,如果权值与测试的图通过前向传播后计算的值大于一定阈值,可以判断属于这个分类。
代码
python实现
tensorflow实现
python实现
import numpy as npclass NeuralNetworksLogisticRegression:'''神经网络逻辑回归(仅1层隐藏层)参数:X - 训练集(需要将训练集的特征缩放到合适范围,并将参数以列向量重排)Y - 训练集的结果(取值为0|1)m - 样本数量W1 - 输入层的权重矩阵(W1,b1以行向量作特征,减少.T转置运算,便于计算)b1 - 输入层的常数项权重W2 - 隐藏层的权重矩阵(W2,b2以列向量作特征,这样才能正确做隐藏层的矩阵运算)b2 - 隐藏层的常数项权重learning_rate - 学习速率num_iter - 迭代次数costs - 代价函数值的集合(非必须操作)n_x - 输入层的数量n_h - 隐藏层的数量n_y - 输出层的数量使用:nn_lr = NeuralNetworksLogisticRegression()nn_lr.initialize_parameters(X, Y, n_h=4)nn_lr.train(learning_rate=0.05, num_iter=1000)predicted = nn_lr.predict(X)'''X = 0Y = 0m = 0W1 = 0W2 = 0b1 = 0b2 = 0learning_rate = 0num_iter = 0costs = []n_x = 0n_h = 0n_y = 0# 初始化变量def initialize_parameters(self, X, Y, n_h=1):'''1.加载训练集,并设置一些初始值2.设置神经网络的结构,对每层设置数量网络结构如下:(这里仅有1个隐藏层,且隐藏层为4个节点)[输入层] [隐藏层] [输出层][A1] [A2] [A3]oo o oo oo[输入层的激活函数] [隐藏层的激活函数]g1(x)=tanh(z) g2(x)=1/(1+np.exp(-z))(这里对不同层使用了不同的激活函数)3.用随机数矩阵初始化权重矩阵,用零矩阵初始化偏移参数:X - 训练集Y - 训练集的结果注意:神经网络中,不能简单将每层的权重设为0,否则无法每个节点权值一样无法实现功能'''self.X = Xself.Y = Yself.costs = []self.m = X.shape[1]# 设置神经网络的结构self.n_x = self.X.shape[0]self.n_h = n_hself.n_y = self.Y.shape[0]# 初始化参数self.W1 = np.random.randn(self.n_h, self.n_x) * 0.01self.b1 = np.zeros(shape = (self.n_h, 1))self.W2 = np.random.randn(self.n_y, self.n_h) * 0.01self.b2 = np.zeros(shape = (self.n_y, 1))# 前向传播def forward_propagation(self, X):'''计算前向传播公式:A1 = XZ2 = W1 * A1A2 = g(Z2) (add A2_0)Z3 = W2 * A2A3 = g(Z3)'''A1 = XZ2 = np.dot(self.W1, A1) + self.b1A2 = np.tanh(Z2) # 隐藏层的激活函数,采用的是g(x) = tanh(x)Z3 = np.dot(self.W2, A2) + self.b2A3 = 1 / (1 + np.exp(-Z3))cache = {'A2': A2,'A3': A3}return cache# 向后传播def backward_propagation(self, cache):'''计算向后传播公式:delta3 = A3 - YdW2 = 1/m * delta3 * A2.Tdb2 = 1/m * sum(delta3)delta2 = W2.T * delta3 * g2'(delta2)dW1 = 1/m * delta2 * X.Tdb1 = 1/m * sum(delta2)'''m = self.mA2 = cache['A2']A3 = cache['A3']delta3 = A3 - self.YdW2 = 1/m * np.dot(delta3, A2.T)db2 = 1/m * np.sum(delta3, axis=1, keepdims=True)# 由于激活函数是: g(x) = tanh(x)# 求导: g'(x) = 1 - tanh(x) ** 2delta2 = np.dot(self.W2.T, delta3) * (1 - A2 ** 2)dW1 = 1/m * np.dot(delta2, self.X.T)db1 = 1/m * np.sum(delta2, axis=1, keepdims=True)grads = {'dW1': dW1,'db1': db1,'dW2': dW2,'db2': db2}return grads# 梯度下降def gradient_descent(self):'''进行梯度下降的运算,公式:W = W - alpha * partial_derivative(J(W, b))'''for i in range(self.num_iter):cache = self.forward_propagation(self.X)grads = self.backward_propagation(cache) # 需要前向传播的参数计算dW1, db1 = grads['dW1'], grads['db1']dW2, db2 = grads['dW2'], grads['db2']# 梯度下降,更新参数W、bself.W1 = self.W1 - self.learning_rate * dW1self.b1 = self.b1 - self.learning_rate * db1self.W2 = self.W2 - self.learning_rate * dW2self.b2 = self.b2 - self.learning_rate * db2# 计算代价A_output = cache['A3'] # A3即是output层self.compute_cost(A_output) # 计算代价# 代价计算def compute_cost(self, A_output):'''计算代价'''cost = (-1 / self.m) * np.sum(self.Y * np.log(A_output) + (1 - self.Y) * np.log(1 - A_output))self.costs.append(cost)# 开始训练def train(self, learning_rate = 0, num_iter = 0):'''开始训练参数:learning_rate - 学习速率num_iter - 迭代次数'''self.learning_rate = learning_rateself.num_iter = num_iterself.gradient_descent()# 预测def predict(self, X):'''预测X数据集参数:X - 测试数据集返回:predicted - 对于测试数据集X的预测结果'''# 复用前向传播计算cache = self.forward_propagation(X)predicted = cache['A3'] # A3即是output层# 转为0|1predicted = np.round(predicted)predicted = predicted.astype(np.int)return predicted
测试
import numpy as np
import matplotlib.pyplot as pltfrom neural_networks_logistic_regression import NeuralNetworksLogisticRegression
# from logistic_regression import LogisticRegression# 绘制决策边界
def plot_decision_boundary(model, X, y):# Set min and max values and give it some paddingx_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1h = 0.01# Generate a grid of points with distance h between themxx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))# Predict the function value for the whole gridZ = model(np.c_[xx.ravel(), yy.ravel()])Z = Z.reshape(xx.shape)# Plot the contour and training examplesplt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)plt.ylabel('x2')plt.xlabel('x1')plt.scatter(X[0, :], X[1, :], c=np.squeeze(y), cmap=plt.cm.Spectral) # show dataset points; y -> np.squeeze(y)# 加载数据
def load_planar_dataset():np.random.seed(1)m = 400 # 样本数量N = int(m / 2) # 每个类别的样本量D = 2 # 维度数X = np.zeros((m, D)) # 初始化XY = np.zeros((m, 1), dtype='uint8') # 初始化Ya = 4 # 花儿的最大长度for j in range(2):ix = range(N * j, N * (j + 1))t = np.linspace(j * 3.12, (j + 1) * 3.12, N) + np.random.randn(N) * 0.2 # thetar = a * np.sin(4 * t) + np.random.randn(N) * 0.2 # radiusX[ix] = np.c_[r * np.sin(t), r * np.cos(t)]Y[ix] = jX = X.TY = Y.Treturn X, Yif __name__ == "__main__":# 加载数据X, Y = load_planar_dataset()N = 20000 # 迭代次数# # 1.逻辑回归# lr = LogisticRegression()# lr.initialize_parameters(X, Y)# lr.train(0.05, N)# predicted = lr.predict(X) # 预测结果# print(f'逻辑回归的准确率:{np.mean(np.equal(Y, predicted)) * 100}%')# # 边界图# plt.subplot(2,2,1)# plot_decision_boundary(lambda x:lr.predict(x.T), X, Y) # 边界是用等高线绘制的# # 迭代代价图# plt.subplot(2,2,2)# x = [xx for xx in range(N)]# plt.plot(x, lr.costs)# 2.神经网络的逻辑回归nn_lr = NeuralNetworksLogisticRegression()nn_lr.initialize_parameters(X, Y, n_h=4)nn_lr.train(learning_rate=0.05, num_iter=N)predicted = nn_lr.predict(X)print(f'神经网络逻辑归回的准确率为:{np.mean(np.equal(Y, predicted)) * 100} %')# 边界图plt.subplot(2,2,3)plot_decision_boundary(lambda x:nn_lr.predict(x.T), X, Y) # 边界是用等高线绘制的# 迭代代价图plt.subplot(2,2,4)plt.plot([x for x in range(N)], nn_lr.costs)plt.show()
neural networks logistic regression 神经网络逻辑回归相关推荐
- Logistic Regression(逻辑回归)原理及公式推导
Logistic Regression(逻辑回归)原理及公式推导 Logistic Regression(逻辑回归)是机器学习中一个非常非常常见的模型,在实际生产环境中也常常被使用,是一种经典的分类模 ...
- 【算法】Logistic regression (逻辑回归) 概述
Logistic regression (逻辑回归)是当前业界比较常用的机器学习方法,用于估计某种事物的可能性.比如某用户购买某商品的可能性,某病人患有某种疾病的可能性,以及某广告被用户点击的可能性等 ...
- Logistic regression (逻辑回归) 概述
http://hi.baidu.com/grandyang/item/e1df4ecf195eb816b77a240e Logistic regression (逻辑回归)是当前业界比较常用的机器学习 ...
- Logistic Regression Classifier逻辑回归
Logistic Regression Classifier逻辑回归主要思想就是用最大似然概率方法构建出方程,为最大化方程,利用牛顿梯度上升求解方程参数. 优点:计算代价不高,易于理解和实现. 缺点: ...
- 转:Logistic regression (逻辑回归) 概述
Logistic regression (逻辑回归)是当前业界比较常用的机器学习方法,用于估计某种事物的可能性.比如某用户购买某商品的可能性,某病人患有某种疾病的可能性,以及某广告被用户点击的可能性等 ...
- 【转】Logistic regression (逻辑回归) 概述
原文出处:苏冉旭,http://hi.baidu.com/hehehehello/item/40025c33d7d9b7b9633aff87 Logistic regression (逻辑回归)是当前 ...
- 【李宏毅2020 ML/DL】P11 Logistic Regression | 由逻辑回归中的特征转换巧妙引出“神经网络”的概念
我已经有两年 ML 经历,这系列课主要用来查缺补漏,会记录一些细节的.自己不知道的东西. 已经有人记了笔记(很用心,强烈推荐): https://github.com/Sakura-gh/ML-not ...
- scikit-learn学习笔记(五)Logistic regression(逻辑回归)
逻辑函数(logistic function) 为了更好地解释逻辑回归,让我们首先了解一下逻辑函数.逻辑函数由于它的S形,有时也被称为sigmoid函数. 现在我要引入比值比(odds ratio)的 ...
- (转载)—— Logistic Regression(逻辑回归)模型实现二分类和多分类
-------------------------------------------- ** 本文转载于https://blog.csdn.net/u011734144/article/detail ...
- Logistic Regression(逻辑回归)模型实现二分类和多分类
一.逻辑回归 二.判定边界 当将训练集的样本以其各个特征为坐标轴在图中进行绘制时,通常可以找到某一个 判定边界 去将样本点进行分类.例如: 线性判定边界: 非线性判定边界: 三.二分类和sigmoid ...
最新文章
- ubuntu安装openssh-server 报依赖错误的解决过程
- WEBPACK 入门
- Database之SQL:自定义创建数据库的各种表demo集合(以方便理解和分析sql的各种增删改查语法的具体用法)
- Coursera吴恩达《神经网络与深度学习》课程笔记(3)-- 神经网络基础之Python与向量化
- 日常生活 -- 嵌入式面试
- RedHat7.0 设置weblogic开机自启动
- 锁定机制和数据并发管理(笔记)
- Java BigInteger类| nextProbablePrime()方法与示例
- input hidden的值存储在哪儿_kafka内核:消息存储模块的工作机制
- ffmpeg编译的静态链接库问题
- linux中date命令y与Y区别,Linux命令之date
- 计算机usb速度设置,怎样提升USB2.0的速度 注册列表修改法【详解】
- FastDFS存储目录迁移方案
- 2021-09-08
- 渗透测试的本质与沉思
- python3d动画效果_使用Matplotlib 3D实现三维波浪动画
- STM32F103C8T6实现串口通信
- POJ 计算几何入门题目推荐(转)
- HTML+CSS个人笔记
- python 语言与numpy库
热门文章
- 等保知识|云计算问题的通俗解释
- 手机,电脑都能用的,整人,恶搞代码连接,“你不会百度一下吗”教你用百度
- 第二章 实例研究:设计一个文档编辑器--《设计模式-可复用面向对象软件的基础》Erich Gamma
- 数据库并发控制、悲观锁(Pessimistic Lock)、乐观锁(Optimistic Lock)、排他锁(Exclusivelocks X锁)、共享锁(Shared Lock S锁)
- 百度推广系列之优化之笔
- adc网络语什么意思_王者荣耀adc什么意思 王者荣耀游戏术语全解
- 关于IE浏览器加载图片报206错误的一个解决方法
- 折下我的翅膀,送你飞翔
- 零基础学摄影nbsp;人像摄影调节光…
- 如何使用GOOGLE高级搜索技巧