Python使用numpy实现BP神经网络
作者:哇哇小仔 发布时间:2021-11-26 22:16:32
标签:python,神经网络
本文完全利用numpy实现一个简单的BP神经网络,由于是做regression而不是classification,因此在这里输出层选取的激励函数就是f(x)=x。BP神经网络的具体原理此处不再介绍。
import numpy as np
class NeuralNetwork(object):
def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
# Set number of nodes in input, hidden and output layers.设定输入层、隐藏层和输出层的node数目
self.input_nodes = input_nodes
self.hidden_nodes = hidden_nodes
self.output_nodes = output_nodes
# Initialize weights,初始化权重和学习速率
self.weights_input_to_hidden = np.random.normal(0.0, self.hidden_nodes**-0.5,
( self.hidden_nodes, self.input_nodes))
self.weights_hidden_to_output = np.random.normal(0.0, self.output_nodes**-0.5,
(self.output_nodes, self.hidden_nodes))
self.lr = learning_rate
# 隐藏层的激励函数为sigmoid函数,Activation function is the sigmoid function
self.activation_function = (lambda x: 1/(1 + np.exp(-x)))
def train(self, inputs_list, targets_list):
# Convert inputs list to 2d array
inputs = np.array(inputs_list, ndmin=2).T # 输入向量的shape为 [feature_diemension, 1]
targets = np.array(targets_list, ndmin=2).T
# 向前传播,Forward pass
# TODO: Hidden layer
hidden_inputs = np.dot(self.weights_input_to_hidden, inputs) # signals into hidden layer
hidden_outputs = self.activation_function(hidden_inputs) # signals from hidden layer
# 输出层,输出层的激励函数就是 y = x
final_inputs = np.dot(self.weights_hidden_to_output, hidden_outputs) # signals into final output layer
final_outputs = final_inputs # signals from final output layer
### 反向传播 Backward pass,使用梯度下降对权重进行更新 ###
# 输出误差
# Output layer error is the difference between desired target and actual output.
output_errors = (targets_list-final_outputs)
# 反向传播误差 Backpropagated error
# errors propagated to the hidden layer
hidden_errors = np.dot(output_errors, self.weights_hidden_to_output)*(hidden_outputs*(1-hidden_outputs)).T
# 更新权重 Update the weights
# 更新隐藏层与输出层之间的权重 update hidden-to-output weights with gradient descent step
self.weights_hidden_to_output += output_errors * hidden_outputs.T * self.lr
# 更新输入层与隐藏层之间的权重 update input-to-hidden weights with gradient descent step
self.weights_input_to_hidden += (inputs * hidden_errors * self.lr).T
# 进行预测
def run(self, inputs_list):
# Run a forward pass through the network
inputs = np.array(inputs_list, ndmin=2).T
#### 实现向前传播 Implement the forward pass here ####
# 隐藏层 Hidden layer
hidden_inputs = np.dot(self.weights_input_to_hidden, inputs) # signals into hidden layer
hidden_outputs = self.activation_function(hidden_inputs) # signals from hidden layer
# 输出层 Output layer
final_inputs = np.dot(self.weights_hidden_to_output, hidden_outputs) # signals into final output layer
final_outputs = final_inputs # signals from final output layer
return final_outputs
来源:http://blog.csdn.net/zhangyang10d/article/details/54804053


猜你喜欢
- 外联接。外联接可以是左向外联接、右向外联接或完整外部联接。 在 FROM 子句中指定外联接时,可以由下列几组关键字中的一组指定:LEFT J
- IE5及其以后版本支持在CSS中使用expression,用来把CSS属性和Javascript表达式关联起来,这里的CSS属性可以是元素固
- 这里的内容以Linux进程基础和Linux文本流为基础。subprocess包主要功能是执行外部的命令和程序。比如说,我需要使用wget下载
- 相信大家都用过 jupyter,也用过里面的魔法命令,这些魔法命令都以 % 或者 %% 开
- 前言优化器的选择关乎参数更新的方法,合理的方法可以帮助机器学习更好的寻找到全局最佳值。那我们快点开始学习吧tensorflow常见的Opti
- sql2000安全很重要将有安全问题的SQL过程删除.比较全面.一切为了安全!删除了调用shell,注册表,COM组件的破坏权限use&nb
- 有这样的情形,django个人头像在model中是:class UserProfile(AbstractUser): ""
- ASP里两种常用的生成文件的方式是:利用ADODB.Stream生成文件和利用Scripting.FileSystemObject生成文件1
- Python 常见字节码LOAD_CONST这个指令用于将一个常量加载到栈中。常量可以是数字、字符串、元组、列表、字典等对象。例如:>
- image.jsp------------------------------生成随机验证码图片的Jsp页面 代码如下: <
- 一、APC缓存简介APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”。它为我们提供了缓存和优化PHP的中
- 使用pyinstaller可以把.py文件打包为.exe可执行文件,命令为:pyinstaller hello.py打包后有两个文件夹,一个
- 题目:给定两个自然数,求这两个数的最大公约数。分析:单看题目的话,非常简单,我们可以循环遍历自然数,如果能够整除两个自然数,就把这个数记下来
- 注意,在改变数值之前锁定应用,确保一段时间里只有一个客户执行该语句。<SCRIPT LANGUAGE="VBScr
- 最近这几天,学习了一下python,对于爬虫比较感兴趣,就做了一个简单的爬虫项目,使用Python的库Tkinsert做了一个界面,感觉这个
- 首先初始化页面$(function(){ $('#archives-table').bootstrapTable
- 例如: 修改(列名前 要有column关键字) ALTER TABLE [USER] ALTER&n
- 0. 前言本节中,我们使用策略梯度算法解决 CartPole 问题。虽然在这个简单问题中,使用随机搜索策略和爬山算法就足
- int(整型)在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647在6
- 获取不带扩展名的文件的名称:import osprintos.path.splitext("path_to_file")