王权富贵:通过BP的反向传输查看神经网络最匹配的特征图--《Python神经网络编程》的学习笔记

本文主要是介绍王权富贵:通过BP的反向传输查看神经网络最匹配的特征图--《Python神经网络编程》的学习笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这是使用BP反向还原机器认为最匹配的图案

(使用MNIST手写体数据库:https://download.csdn.net/download/a1103688841/10867644)

比如:0

下面开始代码介绍:

这里的类中最后一个函数backquery()是重点。这里有个问题应为使用S函数和S反函数所以值的范围不一样,需要校准。

import numpy
import scipy.special
import matplotlib.pyplot
class neuralNetwork:def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):self.inodes = inputnodesself.hnodes = hiddennodesself.onodes = outputnodesself.wih = numpy.random.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes))self.who = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.onodes, self.hnodes))self.lr = learningrate#激活函数不可以随意更改,反向传输这里求导是固定的self.activation_function = lambda x: scipy.special.expit(x)self.inverse_activation_function = lambda x: scipy.special.logit(x)passdef train(self, inputs_list, targets_list):inputs = numpy.array(inputs_list, ndmin=2).Ttargets = numpy.array(targets_list, ndmin=2).Thidden_inputs = numpy.dot(self.wih, inputs)hidden_outputs = self.activation_function(hidden_inputs)final_inputs = numpy.dot(self.who, hidden_outputs)final_outputs = self.activation_function(final_inputs)output_errors = targets - final_outputshidden_errors = numpy.dot(self.who.T, output_errors) self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs))self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs))passdef query(self, inputs_list):inputs = numpy.array(inputs_list, ndmin=2).Thidden_inputs = numpy.dot(self.wih, inputs)hidden_outputs = self.activation_function(hidden_inputs)final_inputs = numpy.dot(self.who, hidden_outputs)final_outputs = self.activation_function(final_inputs)return final_outputsdef backquery(self, targets_list):#这里是重点 主要是有效值的部分需要校准final_outputs = numpy.array(targets_list, ndmin=2).Tfinal_inputs = self.inverse_activation_function(final_outputs)hidden_outputs = numpy.dot(self.who.T, final_inputs)hidden_outputs -= numpy.min(hidden_outputs)hidden_outputs /= numpy.max(hidden_outputs)hidden_outputs *= 0.98hidden_outputs += 0.01hidden_inputs = self.inverse_activation_function(hidden_outputs)inputs = numpy.dot(self.wih.T, hidden_inputs)inputs -= numpy.min(inputs)inputs /= numpy.max(inputs)inputs *= 0.98inputs += 0.01return inputs

这里是开始训练,得到需要的权重。

input_nodes = 784
hidden_nodes = 200
output_nodes = 10
learning_rate = 0.1n = neuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)
training_data_file = open("mnist_train.csv", 'r')
training_data_list = training_data_file.readlines()
training_data_file.close()epochs = 5
for e in range(epochs):for record in training_data_list:all_values = record.split(',')inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01targets = numpy.zeros(output_nodes) + 0.01targets[int(all_values[0])] = 0.99n.train(inputs, targets)passpasstest_data_file = open("mnist_test.csv", 'r')
test_data_list = test_data_file.readlines()
test_data_file.close()scorecard = []
for record in test_data_list:all_values = record.split(',')correct_label = int(all_values[0])inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01outputs = n.query(inputs)label = numpy.argmax(outputs)if (label == correct_label):scorecard.append(1)else:scorecard.append(0)passpassscorecard_array = numpy.asarray(scorecard)
print ("performance = ", scorecard_array.sum() / scorecard_array.size)

下面正式调用反向推理函数。

label = 1
# create the output signals for this label
targets = numpy.zeros(output_nodes) + 0.01
# all_values[0] is the target label for this record
targets[label] = 0.99
print(targets)# get image data
image_data = n.backquery(targets)# plot image data
matplotlib.pyplot.imshow(image_data.reshape(28,28), cmap='Greys', interpolation='None')

下面是还原图片。

 

 

 

这篇关于王权富贵:通过BP的反向传输查看神经网络最匹配的特征图--《Python神经网络编程》的学习笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/628148

相关文章

Java调用Python的四种方法小结

《Java调用Python的四种方法小结》在现代开发中,结合不同编程语言的优势往往能达到事半功倍的效果,本文将详细介绍四种在Java中调用Python的方法,并推荐一种最常用且实用的方法,希望对大家有... 目录一、在Java类中直接执行python语句二、在Java中直接调用Python脚本三、使用Run

使用Python开发Markdown兼容公式格式转换工具

《使用Python开发Markdown兼容公式格式转换工具》在技术写作中我们经常遇到公式格式问题,例如MathML无法显示,LaTeX格式错乱等,所以本文我们将使用Python开发Markdown兼容... 目录一、工具背景二、环境配置(Windows 10/11)1. 创建conda环境2. 获取XSLT

Python如何调用指定路径的模块

《Python如何调用指定路径的模块》要在Python中调用指定路径的模块,可以使用sys.path.append,importlib.util.spec_from_file_location和exe... 目录一、sys.path.append() 方法1. 方法简介2. 使用示例3. 注意事项二、imp

PyQt5+Python-docx实现一键生成测试报告

《PyQt5+Python-docx实现一键生成测试报告》作为一名测试工程师,你是否经历过手动填写测试报告的痛苦,本文将用Python的PyQt5和python-docx库,打造一款测试报告一键生成工... 目录引言工具功能亮点工具设计思路1. 界面设计:PyQt5实现数据输入2. 文档生成:python-

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例