import csv
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
from numpy import matrix
from math import pow
from collections import namedtuple
import math
import random
import os
import json
"""
This class does some initial training of a neural network for predicting drawn
digits based on a data set in data_matrix and data_labels. It can then be used to
train the network further by calling train() with any array of data or to predict
what a drawn digit is by calling predict().
The weights that define the neural network can be saved to a file, NN_FILE_PATH,
to be reloaded upon initilization.
"""
class OCRNeuralNetwork:
LEARNING_RATE = 0.1
WIDTH_IN_PIXELS = 20
NN_FILE_PATH = 'nn.json'
def __init__(self, num_hidden_nodes, data_matrix, data_labels, training_indices, use_file=True): #True
self.sigmoid = np.vectorize(self._sigmoid_scalar)
self.sigmoid_prime = np.vectorize(self._sigmoid_prime_scalar)
self._use_file = use_file
self.data_matrix = data_matrix
self.data_labels = data_labels
# if (not os.path.isfile(OCRNeuralNetwork.NN_FILE_PATH) or not use_file):
if (not os.path.isfile(OCRNeuralNetwork.NN_FILE_PATH) or not use_file):
# Step 1: Initialize weights to small numbers
self.theta1 = self._rand_initialize_weights(400, num_hidden_nodes)
self.theta2 = self._rand_initialize_weights(num_hidden_nodes, 10)
self.input_layer_bias = self._rand_initialize_weights(1, num_hidden_nodes)
self.hidden_layer_bias = self._rand_initialize_weights(1, 10)
# Train using sample data
TrainData = namedtuple('TrainData', ['y0', 'label'])
self.train([TrainData(self.data_matrix[i], int(self.data_labels[i])) for i in training_indices])
self.save()
print("the if is run in ocr.py.")
else:
print("the else is run in ocr.py.")
self._load()
def _rand_initialize_weights(self, size_in, size_out):
return [((x * 0.12) - 0.06) for x in np.random.rand(size_out, size_in)]
# The sigmoid activation function. Operates on scalars.
def _sigmoid_scalar(self, z):
return 1 / (1 + math.e ** -z)
def _sigmoid_prime_scalar(self, z):
return self.sigmoid(z) * (1 - self.sigmoid(z))
def _draw(self, sample):
pixelArray=np.array(sample).reshape(20,20)
plt.imshow(zip(*pixelArray), cmap=cm.Greys_r, interpolation = "nearest")
# pixelArray = [sample[j:j+self.WIDTH_IN_PIXELS] for j in xrange(0, len(sample), self.WIDTH_IN_PIXELS)]
# plt.imshow(zip(*pixelArray), cmap = cm.Greys_r)
# , interpolation = "nearest" ,norm="Normalize"
plt.show()
def train(self, training_data_array):
for data in training_data_array:
# Step 2: Forward propagation
y1 = np.dot(np.mat(self.theta1), np.mat(data.y0).T)
sum1 = y1 + np.mat(self.input_layer_bias) # Add the bias
y1 = self.sigmoid(sum1)
y2 = np.dot(np.array(self.theta2), y1)
y2 = np.add(y2, self.hidden_layer_bias) # Add the bias
y2 = self.sigmoid(y2)
# Step 3: Back propagation
actual_vals = [0] * 10 # actual_vals is a python list for easy initialization and is later turned into an np matrix (2 lines down).
actual_vals[data.label] = 1
output_errors = np.mat(actual_vals).T - np.mat(y2)
hidden_errors = np.multiply(np.dot(np.mat(self.theta2).T, output_errors), self.sigmoid_prime(sum1))
# Step 4: Update weights
self.theta1 += self.LEARNING_RATE * np.dot(np.mat(hidden_errors), np.mat(data.y0))
self.theta2 += self.LEARNING_RATE * np.dot(np.mat(output_errors), np.mat(y1).T)
self.hidden_layer_bias += self.LEARNING_RATE * output_errors
self.input_layer_bias += self.LEARNING_RATE * hidden_errors
def predict(self, test):
y1 = np.dot(np.mat(self.theta1), np.mat(test).T)
y1 = y1 + np.mat(self.input_layer_bias) # Add the bias
y1 = self.sigmoid(y1)
y2 = np.dot(np.array(self.theta2), y1)
y2 = np.add(y2, self.hidden_layer_bias) # Add the bias
y2 = self.sigmoid(y2)
results = y2.T.tolist()[0]
return results.index(max(results))
def save(self):
# if not self._use_file:
# return
json_neural_network = {
"theta1":[np_mat.tolist()[0] for np_mat in self.theta1],
"theta2":[np_mat.tolist()[0] for np_mat in self.theta2],
"b1":self.input_layer_bias[0].tolist()[0],
"b2":self.hidden_layer_bias[0].tolist()[0]
};
with open(OCRNeuralNetwork.NN_FILE_PATH,'w') as nnFile:
json.dump(json_neural_network, nnFile)
def _load(self):
if not self._use_file:
return
with open(OCRNeuralNetwork.NN_FILE_PATH) as nnFile:
nn = json.load(nnFile)
self.theta1 = [np.array(li) for li in nn['theta1']]
self.theta2 = [np.array(li) for li in nn['theta2']]
self.input_layer_bias = [np.array(nn['b1'][0])]
self.hidden_layer_bias = [np.array(nn['b2'][0])]

宇内虹游
- 粉丝: 1163
最新资源
- 能源信息化管理系统培训讲义.ppt
- “辅导员之家”网站设计与开发毕设论文.doc
- 基于移动网络的防盗系统设计毕业设计论文.doc
- 维Logistic映射研究分析.doc
- 解析水利水电施工企业项目管理论文.doc
- 施工阶段项目管理.ppt
- 基于web办公电子文档设计毕业(论文)设计.doc
- 学习子情境电阻应变式力传感器单片机接口课件.pptx
- 科研事业单位项目管理探讨的论文.doc
- 通信、路灯工程施工方案.doc
- “微生活”网站设计与开发毕业(论文)设计.doc
- 中小企业ERP项目管理实践的分析论文.doc
- 计算机网络广播电视多媒体技术研究论文.doc
- aspose-cells-25.6 for java去水印
- 软件项目监理通用表.doc
- 基于单片机的家电远程控制系统的研究.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


