手写神经网络Python深度学习
import numpy
import scipy.special
import matplotlib.pyplot as plt
import scipy.misc
import glob
import imageio
import scipy.ndimage class neuralNetWork:
def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate):
self.inodes = inputnodes
self.hnodes = hiddennodes
self.onodes = outputnodes self.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) # 反向查询log激活函数 def train(self,inputs_list,targets_list):
inputs = numpy.array(inputs_list,ndmin=2).T
targets = numpy.array(targets_list,ndmin=2).T hidden_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_outputs
hidden_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)) def query(self,inputs_list):
inputs = numpy.array(inputs_list,ndmin=2).T hidden_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_outputs
def backquery(self, targets_list):
final_outputs = numpy.array(targets_list, ndmin=2).T final_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.98
hidden_outputs += 0.01 hidden_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.98
inputs += 0.01 return inputs input_nodes = 784
hidden_nodes = 200
output_nodes = 10
learing_rate = 0.1
n = neuralNetWork(input_nodes,hidden_nodes,output_nodes,learing_rate) train_data_file = open('mnist_train.csv', 'r')
train_data_list = train_data_file.readlines()
train_data_file.close() epochs = 5
for e in range(epochs):
for record in train_data_list:
all_values = record.split(',')
#image_array = numpy.asfarray(all_values[1:]).reshape((28,28))
#plt.imshow(image_array,cmap='Greys',interpolation='None')
#plt.show()
inputs = (numpy.asfarray(all_values[1:])/255.0 *0.99)+0.01
targets = numpy.zeros(output_nodes) + 0.01
targets[int(all_values[0])] = 0.99
n.train(inputs,targets) #手写字体倾斜10度作为测试数据
inputs_plusx_img = scipy.ndimage.interpolation.rotate(inputs.reshape(28,28), 10, cval=0.01, order=1, reshape=False)
n.train(inputs_plusx_img.reshape(784), targets)
inputs_minusx_img = scipy.ndimage.interpolation.rotate(inputs.reshape(28,28), -10, cval=0.01, order=1, reshape=False)
n.train(inputs_minusx_img.reshape(784), targets) test_data_file = open('mnist_test.csv', 'r')
test_data_list = test_data_file.readlines()
test_data_file.close()
# all_values = test_data_list[0].split(',') # # image_array = numpy.asfarray(all_values[1:]).reshape((28,28))
# # plt.imshow(image_array,cmap='Greys',interpolation='None')
# # plt.show() # output = n.query((numpy.asfarray(all_values[1:])/ 255.0 * 0.99)+0.01) scorecard = []
for record in test_data_list:
all_values = record.split(',')
correct_label = int(all_values[0])
#print(correct_label,'correct_label')
inputs = (numpy.asfarray(all_values[1:])/255.0 *0.99)+0.01
outputs = n.query(inputs)
label = numpy.argmax(outputs)
#print(label,'network answer')
if (label == correct_label):
scorecard.append(1)
else:
scorecard.append(0)
scorecard_array = numpy.asarray(scorecard)
print("performance = ",scorecard_array.sum() / scorecard_array.size) # 识别自己手写字
our_own_dataset = [] for image_file_name in glob.glob('2828_my_own_?.png'):
label = int(image_file_name[-5:-4]) print ("loading ... ", image_file_name)
img_array = imageio.imread(image_file_name, as_gray=True)
img_data = 255.0 - img_array.reshape(784) img_data = (img_data / 255.0 * 0.99) + 0.01
print(numpy.min(img_data))
print(numpy.max(img_data)) record = numpy.append(label,img_data)
our_own_dataset.append(record) item = 2
plt.imshow(our_own_dataset[item][1:].reshape(28,28), cmap='Greys', interpolation='None')
correct_label = our_own_dataset[item][0]
inputs = our_own_dataset[item][1:] outputs = n.query(inputs)
print (outputs) label = numpy.argmax(outputs)
print("network says ", label)
if (label == correct_label):
print ("match!")
else:
print ("no match!") # 反向生成图像
label = 0
targets = numpy.zeros(output_nodes) + 0.01
targets[label] = 0.99
print(targets) image_data = n.backquery(targets) plt.imshow(image_data.reshape(28,28), cmap='Greys', interpolation='None')
手写神经网络Python深度学习的更多相关文章
- mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)
前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...
- python手写神经网络实现识别手写数字
写在开头:这个实验和matlab手写神经网络实现识别手写数字一样. 实验说明 一直想自己写一个神经网络来实现手写数字的识别,而不是套用别人的框架.恰巧前几天,有幸从同学那拿到5000张已经贴好标签的手 ...
- 【神经网络与深度学习】【python开发】caffe-windows使能python接口使用draw_net.py绘制网络结构图过程
[神经网络与深度学习][python开发]caffe-windows使能python接口使用draw_net.py绘制网络结构图过程 标签:[神经网络与深度学习] [python开发] 主要是想用py ...
- (转)神经网络和深度学习简史(第一部分):从感知机到BP算法
深度|神经网络和深度学习简史(第一部分):从感知机到BP算法 2016-01-23 机器之心 来自Andrey Kurenkov 作者:Andrey Kurenkov 机器之心编译出品 参与:chen ...
- 7大python 深度学习框架的描述及优缺点绍
Theano https://github.com/Theano/Theano 描述: Theano 是一个python库, 允许你定义, 优化并且有效地评估涉及到多维数组的数学表达式. 它与GPUs ...
- [DeeplearningAI笔记]神经网络与深度学习人工智能行业大师访谈
觉得有用的话,欢迎一起讨论相互学习~Follow Me 吴恩达采访Geoffrey Hinton NG:前几十年,你就已经发明了这么多神经网络和深度学习相关的概念,我其实很好奇,在这么多你发明的东西中 ...
- 好书推荐计划:Keras之父作品《Python 深度学习》
大家好,我禅师的助理兼人工智能排版住手助手条子.可能非常多人都不知道我.由于我真的难得露面一次,天天给禅师做底层工作. wx_fmt=jpeg" alt="640? wx_fmt= ...
- 【吴恩达课后测验】Course 1 - 神经网络和深度学习 - 第一周测验【中英】
[吴恩达课后测验]Course 1 - 神经网络和深度学习 - 第一周测验[中英] 第一周测验 - 深度学习简介 和“AI是新电力”相类似的说法是什么? [ ]AI为我们的家庭和办公室的个人设备供电 ...
- 关于python深度学习网站
大数据文摘作品,转载要求见文末 编译团队|姚佳灵 裴迅 简介 ▼ 深度学习,是人工智能领域的一个突出的话题,被众人关注已经有相当长的一段时间了.它备受关注是因为在计算机视觉(Computer Vi ...
随机推荐
- django -- ORM实现出版社增删改查
前戏 我们来完成一个图书管理系统的增删改查 表结构设计 1. 出版社 id name 2. 作者 id name 3. 书 id title 出版社_id 4. 作者_书_关系表 id 书 ...
- 洛谷[SHOI2002]滑雪题解
什么破题啊 简直就是浪费我时间! 我每天还被我xf定目标了不知道嘛! 题目 朴素的搜索只能得90分 #include <cstdio> #include <iostream> ...
- 洛谷 P2253 好一个一中腰鼓! 题解
P2253 好一个一中腰鼓! 题目背景 话说我大一中的运动会就要来了,据本班同学剧透(其实早就知道了),我萌萌的初二年将要表演腰鼓[喷],这个无厘头的题目便由此而来. Ivan乱入:"忽一人 ...
- Kafka中的HW、LEO、LSO等分别代表什么?
HW . LEO 等概念和上一篇文章所说的 ISR有着紧密的关系,如果不了解 ISR 可以先看下ISR相关的介绍. HW (High Watermark)俗称高水位,它标识了一个特定的消息偏移量(of ...
- haproxy 配置文件详解 之 backend
配置示例: backend htmpool mode http option redispatch option abortonclose balance static-rr cookie SESSI ...
- 第10组 Beta冲刺(4/4)
队名:凹凸曼 组长博客 作业博客 组员实践情况 童景霖 过去两天完成了哪些任务 文字/口头描述 继续学习Android studio和Java 制作剩余界面前端 展示GitHub当日代码/文档签入记录 ...
- .NET 微服务 2 架构设计理论(一)
SOA体系架构 面向服务的体系结构 (SOA) ,通过将应用程序分解为多个服务(通常为 HTTP 服务,WCF服务等),将其分为不同类型(例如子系统或层),从而来划分应用程序的结构. 微服务源自 SO ...
- sts问题合集
背景:用来记录在使用sts过程中遇到的相关问题 Version 当前jdk版本号 of the JVM is not suitable for the this product.Version:1.8 ...
- Jenkins工具学习(一)
Jenkins的下载及安装 Jenkins下载地址:https://jenkins.io/download/ 下载后的直接解压安装 根据自己的喜好选择一种方式安装: 如果选择推荐安装,会自动下载一些插 ...
- ssh服务器安装测试
ssh服务器搭建 作用:用于远程登录到服务器 (1)服务器端 安装ssh: $ sudo apt-get install openssh-server 查看ssh是否已经安装: $ sudo apti ...