梯度下降算法推导与实现

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd#Some helper functions for plotting and drawing linesdef plot_points(X, y):admitted = X[np.argwhere(y==1)]rejected = X[np.argwhere(y==0)]plt.scatter([s[0][0] for s in rejected], [s[0][1] for s in rejected], s = 25, color = 'blue', edgecolor = 'k')plt.scatter([s[0][0] for s in admitted], [s[0][1] for s in admitted], s = 25, color = 'red', edgecolor = 'k')def display(m, b, color='g--'):plt.xlim(-0.05,1.05)plt.ylim(-0.05,1.05)x = np.arange(-10, 10, 0.1)plt.plot(x, m*x+b, color)
#读取与绘制数据
data = pd.read_csv('data.csv', header=None)
X = np.array(data[[0,1]])
y = np.array(data[2])
plot_points(X,y)
plt.show()# Implement the following functions# Activation (sigmoid) function
def sigmoid(x):return 1/(1+np.exp(-x))# Output (prediction) formula
def output_formula(features, weights, bias):sigmoid(np.dot(features, weights) + bias)# Error (log-loss) formula
def error_formula(y, output):return - y*np.log(output) - (1 - y) * np.log(1-output)# Gradient descent step
def update_weights(x, y, weights, bias, learnrate):output = output_formula(x, weights, bias)d_error = -(y - output)weights -= learnrate * d_error * xbias -= learnrate * d_errorreturn weights, bias
np.random.seed(44)epochs = 100
learnrate = 0.01def train(features, targets, epochs, learnrate, graph_lines=False):errors = []n_records, n_features = features.shapelast_loss = Noneweights = np.random.normal(scale=1 / n_features**.5, size=n_features)bias = 0for e in range(epochs):del_w = np.zeros(weights.shape)for x, y in zip(features, targets):output = output_formula(x, weights, bias)error = error_formula(y, output)weights, bias = update_weights(x, y, weights, bias, learnrate)# Printing out the log-loss error on the training setout = output_formula(features, weights, bias)loss = np.mean(error_formula(targets, out))errors.append(loss)if e % (epochs / 10) == 0:print("\n========== Epoch", e,"==========")if last_loss and last_loss < loss:print("Train loss: ", loss, "  WARNING - Loss Increasing")else:print("Train loss: ", loss)last_loss = losspredictions = out > 0.5accuracy = np.mean(predictions == targets)print("Accuracy: ", accuracy)if graph_lines and e % (epochs / 100) == 0:display(-weights[0]/weights[1], -bias/weights[1])
# Plotting the solution boundaryplt.title("Solution boundary")display(-weights[0]/weights[1], -bias/weights[1], 'black')# Plotting the dataplot_points(features, targets)plt.show()# Plotting the errorplt.title("Error Plot")plt.xlabel('Number of epochs')plt.ylabel('Error')plt.plot(errors)plt.show()
#训练算法
train(X, y, epochs, learnrate, True)

反向传播

反向传播流程如下:

  • 进行前向反馈运算。
  • 将模型的输出与期望的输出进行比较。
  • 计算误差。
  • 向后运行前向反馈运算(反向传播),将误差分散到每个权重上。
  • 更新权重,并获得更好的模型。
  • 继续此流程,直到获得很好的模型。

实战演练:利用神经网络来预测学生录取情况

数据集来源: http://www.ats.ucla.edu/

# Importing pandas and numpy
import pandas as pd
import numpy as np# Reading the csv file into a pandas DataFrame
data = pd.read_csv('student_data.csv')# Printing out the first 10 rows of our data
data[:10]
#绘制数据
# Importing matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
# Function to help us plot
def plot_points(data):X = np.array(data[["gre","gpa"]])y = np.array(data["admit"])admitted = X[np.argwhere(y==1)]rejected = X[np.argwhere(y==0)]plt.scatter([s[0][0] for s in rejected], [s[0][1] for s in rejected], s = 25, color = 'red', edgecolor = 'k')plt.scatter([s[0][0] for s in admitted], [s[0][1] for s in admitted], s = 25, color = 'cyan', edgecolor = 'k')plt.xlabel('Test (GRE)')plt.ylabel('Grades (GPA)')# Plotting the points
plot_points(data)
plt.show()
# Separating the ranks
data_rank1 = data[data["rank"]==1]
data_rank2 = data[data["rank"]==2]
data_rank3 = data[data["rank"]==3]
data_rank4 = data[data["rank"]==4]# Plotting the graphs
plot_points(data_rank1)
plt.title("Rank 1")
plt.show()
plot_points(data_rank2)
plt.title("Rank 2")
plt.show()
plot_points(data_rank3)
plt.title("Rank 3")
plt.show()
plot_points(data_rank4)
plt.title("Rank 4")
plt.show()
#将评级进行one-shot编码
# TODO:  Make dummy variables for rank
one_hot_data = pd.concat([data, pd.get_dummies(data['rank'], prefix='rank')], axis=1)# TODO: Drop the previous rank column
one_hot_data = one_hot_data.drop('rank', axis=1)# Print the first 10 rows of our data
one_hot_data[:10]
#缩放数据
# Making a copy of our data
processed_data = one_hot_data[:]# TODO: Scale the columns
processed_data['gre']=processed_data['gre']/800
processed_data['gpa']=processed_data['gpa']/4.0# Printing the first 10 rows of our procesed data
processed_data[:10]
#将数据分成训练集和测试集
sample = np.random.choice(processed_data.index, size=int(len(processed_data)*0.9), replace=False)
train_data, test_data = processed_data.iloc[sample], processed_data.drop(sample)print("Number of training samples is", len(train_data))
print("Number of testing samples is", len(test_data))
print(train_data[:10])
print(test_data[:10])
#将数据分成特征和目标
features = train_data.drop('admit', axis=1)
targets = train_data['admit']
features_test = test_data.drop('admit', axis=1)
targets_test = test_data['admit']print(features[:10])
print(targets[:10])
#训练二层神经网络Activation (sigmoid) function
def sigmoid(x):return 1 / (1 + np.exp(-x))
def sigmoid_prime(x):return sigmoid(x) * (1-sigmoid(x))
def error_formula(y, output):return - y*np.log(output) - (1 - y) * np.log(1-output)
#误差反向传播
# TODO: Write the error term formula
def error_term_formula(y, output):return (y-output)*sigmoid_prime(x)
def error_term_formula(x, y, output):return (y-output) * output * (1 - output)
# Neural Network hyperparameters
epochs = 1000
learnrate = 0.5# Training function
def train_nn(features, targets, epochs, learnrate):# Use to same seed to make debugging easiernp.random.seed(42)n_records, n_features = features.shapelast_loss = None# Initialize weightsweights = np.random.normal(scale=1 / n_features**.5, size=n_features)for e in range(epochs):del_w = np.zeros(weights.shape)for x, y in zip(features.values, targets):# Loop through all records, x is the input, y is the target# Activation of the output unit#   Notice we multiply the inputs and the weights here #   rather than storing h as a separate variable output = sigmoid(np.dot(x, weights))# The error, the target minus the network outputerror = error_formula(y, output)# The error term#   Notice we calulate f'(h) here instead of defining a separate#   sigmoid_prime function. This just makes it faster because we#   can re-use the result of the sigmoid function stored in#   the output variableerror_term = error_term_formula(x,y, output)# The gradient descent step, the error times the gradient times the inputsdel_w += error_term * x# Update the weights here. The learning rate times the # change in weights, divided by the number of records to averageweights += learnrate * del_w / n_records# Printing out the error on the training setif e % (epochs / 10) == 0:out = sigmoid(np.dot(features, weights))loss = np.mean((out - targets) ** 2)print("Epoch:", e)if last_loss and last_loss < loss:print("Train loss: ", loss, "  WARNING - Loss Increasing")else:print("Train loss: ", loss)last_loss = lossprint("=========")print("Finished training!")return weightsweights = train_nn(features, targets, epochs, learnrate)
#计算测试数据的准确度
# Calculate accuracy on test data
tes_out = sigmoid(np.dot(features_test, weights))
predictions = tes_out > 0.5
accuracy = np.mean(predictions == targets_test)
print("Prediction accuracy: {:.3f}".format(accuracy))

优达学城深度学习之五——卷积神经网络相关推荐

  1. 优达学城 深度学习 任务1

    这几天刚好有环境,打算学习一下深度学习 看了一圈介绍,发现优达学城的深度学习课程作为入门课程还是不错的 今天看了第一章节的视频,顺便做了任务1 任务1难度不大,按照网站上的说明可以完成下载.打包等工作 ...

  2. 优达学城-深度学习笔记(一)

    优达学城-深度学习笔记(一) 标签: 机器学习 优达学城-深度学习笔记一 一 神经网络简介 最大似然概率 交叉熵Cross entropy 1交叉熵代码实现 2多类别交叉熵 对数几率回归的误差函数co ...

  3. 优达学城 深度学习 任务2

    不得不说优达学城的课程作为入门还真是不错,打算明年买一个纳米课程试一下. 说明 任务2可以说是真正开始进入深度学习的领域 还是用任务1处理好的数据集 任务分为3个阶段 梯度下降算法(线性分类器) 批随 ...

  4. 优达学城深度学习之六——TensorFlow卷积神经网络

    TensorFlow卷积层 TensorFlow 提供了 tf.nn.conv2d() 和 tf.nn.bias_add() 函数来创建你自己的卷积层. # Output depth k_output ...

  5. 优达学城深度学习之七——TensorFlow卷积神经网络

    一.胶囊网络 池化运算会丢失一些图像信息.这是因为为了获得更小的特征级图像表示,池化会丢弃像素信息.与池化层相比,有一些分类方法不会丢弃空间信息,而是学习各个部分之间的关系(例如眼睛.鼻子和嘴之间的空 ...

  6. 优达学城深度学习之三(上)——卷积神经网络

    学习如何用神经网络来解决分类问题. 开始都会说什么是机器学习?机器学习的应用是什么?用机器在海量数据中学习得到可以解决一类问题的办法,这就是我的理解.图像处理.文本处理.无人驾驶.等,深度学习最热门的 ...

  7. 优达学城深度学习之三(下)——卷积神经网络

    一.One-Hot编码 计算机在表示多结果的分类时,使用One-Hot编码是比较常见的处理方式.即每个对象都有对应的列. 二.最大似然率 下面是两幅图像,比较两幅图像,试通过概率的方法来讨论一下为什么 ...

  8. 优达学城深度学习(之四)——jupyter notebook

    Jupyter notebook 是什么? 欢迎学习本课程--如何使用 Jupyter notebook.Jupyter notebook 是一种 Web 应用,能让用户将说明文本.数学方程.代码和可 ...

  9. 优达学城深度学习之二——矩阵数学和Numpy复习

    一.数据维度 维度(scalar),张量(Tensor).3表示零维张量,[1 2 3]表示一维张量,矩阵表示二维张量,任何大于二维张量就叫张量(Tensor).如下图所示: 二.Numpy简介 2. ...

最新文章

  1. Android5.0如何正确启用isLoggable(二) 理分析
  2. etcd 访问 锁_在系统中用etcd实现服务注册和发现
  3. Word2007 设置Tab键的默认缩进距离
  4. 北斗导航 | 基于卡尔曼滤波的IMU+GNSS的组合导航(附Matlab源代码)
  5. 多核导航模块(Multicore Navigator)
  6. linux驱动中地址空间转换
  7. Cobertura和Sonar 5.1的问题
  8. 永无止境_永无止境地死:
  9. Jenkins修改管理员密码
  10. 深度学习框架PyTorch一书的学习-第四章-神经网络工具箱nn
  11. Linux学习笔记---使用tftp命令下载文件
  12. 整数类型及整数类型的显示转换
  13. 年薪 80w 的程序员被鄙视了!
  14. redis-bitmap 命令使用的一些帖子
  15. Kafka:Configured broker.id 2 doesn't match stored broker.id 0 in meta.properties.
  16. 离散数学期末复习概念_复习第1部分中的基本概念
  17. 12.逻辑运算符与、或、非、双感叹号、双问号
  18. 黑客攻击手段揭秘(转)
  19. PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的clicked信号参数QAbstractButton *解决办法
  20. 安装Pytorch-gpu版本(第一次安装 或 已经安装Pytorch-cpu版本后)

热门文章

  1. 使用jsp实现文件上传的功能
  2. [Java][内存模型]
  3. Vue.JS学习笔记
  4. python 面试题2
  5. Google code jam 2008, Qualification Round:Save the Universe, 翻译
  6. 贪心算法——洛谷(P1094)纪念品分组
  7. java定时器小程序_【微信小程序】使用setTimeout试试定时器
  8. 【连载】如何掌握openGauss数据库核心技术?秘诀一:拿捏SQL引擎(4)
  9. PostgreSQL 13隐藏杀手锏特性
  10. 资源放送丨《 先睹为快!Oracle 20c新特性解析》PPT视频