我试图将自己的神经网络编写为学习练习。具体来说,我试图创建一个神经网络来识别手写数字。我使用了sklearn的digits dataset,但我自己编写了神经网络。
简单的测试是成功的,即OR门或AND门,所以我确信反向传播已成功实施,但我发现在训练后,网络在工作时仍然工作得非常糟糕手写数字的8x8像素图像。我目前有64个输入(8x8图像)和10个输出(每个数字一个),有2个隐藏层,每个大小为4.我怀疑它是导致问题的多个输出,因为网络通常会达到[0.1,0.1,0.1 ...]的激活(即平均0.0 * 9 + 1.0 * 1)。
可能的想法:
1)多个输出是否会导致问题?
2)是否需要更好的错误功能?
3)我是否只需要以较小的学习率训练系统更长时间?
Image showing the error over iterations
Image showing the prediction of a 1 (i.e. output should be ~[0,1,0,0,0,0,0,0,0,0]) after training
有没有人遇到类似的问题?或者可以建议我可能出错的地方?如果有人问过这个问题我是否有耐心,而且我没有找到它!代码如下:
编辑: charlesreid1和jdehesa都是对的,事实上我的网络架构太简单了,无法处理这项任务。更具体地说,我有2层4个神经元,每个神经元试图处理64个输入。将我的隐藏层更改为3层100个神经元,每个允许我达到90%的准确度分数(假设输出> 0.7被认为是阳性结果)。
# Import our dependencies
import numpy as np
from sklearn import datasets
class Neural_Network():
#Initalising function
def __init__(self, input_size, output_size, niteration = 100000):
np.random.seed(1)
self.niteration = niteration
self.layer_sizes = np.array([input_size, output_size])
self.weights = list()
self.error = np.array([])
# initialise random weights
self._recreate_weights()
def _recreate_weights(self):
# Recreate the weights after adding a hidden layer
self.weights = list()
for i in np.arange(len(self.layer_sizes) - 1):
weights = np.random.rand(self.layer_sizes[i], self.layer_sizes[i+1]) * 2 - 1
self.weights.append(weights)
self.momentum = [i * 0 for i in self.weights]
def add_hidden_layer(self,size):
# Add a new hidden layer to our neural network
self.layer_sizes = np.insert(self.layer_sizes, -1, size)
self._recreate_weights()
def _sigmoid(self, x, deriv=False):
if deriv:
return self._sigmoid(x, deriv=False)*(1-self._sigmoid(x, deriv=False))
else:
return 1.0/(1+np.exp(-x))
def predict(self, input_single, deriv=False, layer_output = False):
data_current_layer = input_single
output_list = list()
output_list.append(np.array([data_current_layer]))
for i in np.arange(len(self.layer_sizes) - 1):
data_current_layer = self._sigmoid(np.dot(data_current_layer, self.weights[i]), deriv)
output_list.append(np.array([data_current_layer]))
return(output_list)
def train2(self, input_training_data, input_training_labels):
for iterations in np.arange(self.niteration):
# Loop over all training sets niteration times
updates = [i * 0 for i in network.weights] # Used for storing the update to the weights
mean_error = np.array([]) # used for calculating the mean error
for i in np.arange(len(input_training_data)): # For each training example
activations = list() # Store all my activations in a list
activations.append(np.array([input_training_data[i]]))
for j in np.arange(len(self.layer_sizes) - 1):
# Calculate all the activations for every layer
z = np.dot(activations[-1], self.weights[j])
a = self._sigmoid(z, deriv = False)
activations.append(a)
error = list()
error.append(a[-1] - np.array([input_training_labels[i]]))
for j in np.arange(len(self.layer_sizes) - 2):
# Calculate the error term for each layer
j2 = (-1 * j) - 1
j3 = j2 - 1
d = np.dot(error[j], self.weights[j2].T) * activations[j3] * (1 - activations[j3])
error.append(d)
for j in np.arange(len(self.layer_sizes) - 1):
# calculate the gradient for the error with respect to the weights
j2 = (-1 * j) - 1
updates[j] += np.dot(activations[j].T, error[j2])
mean_error = np.append(mean_error, np.sum(np.abs(error[0])))
updates = [0.001*i/len(input_training_data) for i in updates] # Add in a learning rate
self.error = np.append(self.error,np.mean(mean_error))
for i in np.arange(len(self.weights)):
# update using a momentum term
self.momentum[i] -= updates[i]
self.weights[i] += self.momentum[i]
self.momentum[i] *= 0.9
if np.mod(iterations, 1000) == 0:
# Visually keep track of the error
print(iterations, self.error[-1])
# Main Loop
# Read in the dataset and divide into a training and test set
data = datasets.load_digits()
images = data.images
labels = data.target
targets = data.target_names
training_images = images[:int(len(labels*0.8))]
training_labels = labels[:int(len(labels*0.8))]
training_images = images[:10]
training_labels = labels[:10]
test_images = images[int(len(labels*0.8)):]
test_labels = labels[int(len(labels*0.8)):]
# Flatten the training and test images using ravel. CAN PROBABLY DO THIS BEFORE DIVIDING THEM UP.
training_images_list = list()
for i in training_images:
training_images_list.append(np.ravel(i))
test_images_list = list()
for i in test_images:
test_images_list.append(np.ravel(i))
# Change the training and test labels into a more usable format.
training_labels_temp=np.zeros([np.size(training_labels), 10])
for i in np.arange(np.size(training_labels)):
training_labels_temp[i, training_labels[i]] = 1
training_labels = training_labels_temp
test_labels_temp=np.zeros([np.size(test_labels), 10])
for i in np.arange(np.size(test_labels)):
test_labels_temp[i, test_labels[i]] = 1
test_labels = test_labels_temp
# Build a 3 layered neural network, input - hidden - output
if True:
network = Neural_Network(input_size=64, output_size=10)
network.add_hidden_layer(size=4)
network.add_hidden_layer(size=4)
network.add_hidden_layer(size=4)
# Train the network on our training set
#print(network.weights)
network.train2(input_training_data = training_images_list, input_training_labels = training_labels)
#print(network.weights)
# Calculate the error on our test set
#network.calculate_error(test_set = test_images, test_labels = test_labels)
答案 0 :(得分:3)
问题肯定在于您的网络架构 - 特别是第一个隐藏层。您将8x8输入馈送到具有4个神经元的隐藏层。首先,没有足够的神经元,64个像素中包含的信息通过仅通过四个神经元而被淘汰。另一个问题(可能会消除足够的神经元)是由于你predict()
函数使用了点积,每个神经元都完全连接到输入。
识别手写数字的任务本质上与像素的空间配置相关联,因此您的网络应该利用这些知识。您应该将输入图像的不同部分提供给第一层中的不同神经元。这为这些神经元提供了基于图像中像素排列来放大或抑制较弱信号的机会(例如,如果你在角落看到一个大信号,它就不可能是1,如果你看到的话在中心有一个大信号,它不太可能是0等。)
概括这个想法是convolutional neural networks的全部意义 - 以及它们为什么能够很好地用于图像识别任务。 O' Reilly Publishers的另一篇很好的文章称为Not Another MNIST Tutorial,实际上不是另一个教程,但显示了一些非常有用的可视化来理解正在发生的事情。
它的长短是:AND / OR是一项非常简单的任务,但是您已经跳到了一项非常复杂的任务 - 您的神经网络架构应该具有相应的跳转所需的架构复杂。卷积神经网络通常遵循架构模式:
用于更复杂任务的更大CNN将这些层组合成更大的嵌套体系结构和子网络。知道要使用的层的组合是一门艺术,可以进行大量的实验(因此GPU的流行 - 使得迭代和实验的速度更快)。但对于灰度手写数字,你应该看到一个很大的改进,只需利用你已经掌握的有关手头任务的信息 - 即它应该利用空间结构。