简单的多层神经网络实现

时间:2013-03-13 20:29:13

标签: python machine-learning artificial-intelligence neural-network

前段时间我开始了机器学习的冒险(在我学习的最后两年)。我已经阅读了很多书,并用机器学习算法EXCEPT神经网络编写了大量代码,这些算法超出了我的范围。我对这个话题很感兴趣,但是我有一个很大的问题: 我读过的所有书都有两个主要问题:

  1. 包含数学方程的音调。讲课后,我非常熟悉它们,并且在纸上我可以进行计算。
  2. 包含嵌入在某些复杂环境中的大型示例(例如调查网店销售率)并进入神经网络实现,我必须编写大量代码来重现上下文。 缺少什么 - 没有大量背景和方程式的简单直接实现。
  3. 您能告诉我,在哪里可以找到SIMPLE实现的多层感知(神经网络)?我不需要理论知识,也不想要上下文嵌入的例子。我更喜欢一些脚本语言来节省时间和精力 - 我以前的99%的作品都是用Python完成的。

    以下是我之前阅读过的书籍清单(但未找到我想要的内容):

    1. 机器学习在行动
    2. 编程集体智慧
    3. 机器学习:算法视角
    4. Java中的神经网络简介
    5. C#神经网络简介

5 个答案:

答案 0 :(得分:32)

一个简单的实现

这是一个使用Python中的类的可读实现。这种实现方式提高了可理解性的效率:

    import math
    import random

    BIAS = -1

    """
    To view the structure of the Neural Network, type
    print network_name
    """

    class Neuron:
        def __init__(self, n_inputs ):
            self.n_inputs = n_inputs
            self.set_weights( [random.uniform(0,1) for x in range(0,n_inputs+1)] ) # +1 for bias weight

        def sum(self, inputs ):
            # Does not include the bias
            return sum(val*self.weights[i] for i,val in enumerate(inputs))

        def set_weights(self, weights ):
            self.weights = weights

        def __str__(self):
            return 'Weights: %s, Bias: %s' % ( str(self.weights[:-1]),str(self.weights[-1]) )

    class NeuronLayer:
        def __init__(self, n_neurons, n_inputs):
            self.n_neurons = n_neurons
            self.neurons = [Neuron( n_inputs ) for _ in range(0,self.n_neurons)]

        def __str__(self):
            return 'Layer:\n\t'+'\n\t'.join([str(neuron) for neuron in self.neurons])+''

    class NeuralNetwork:
        def __init__(self, n_inputs, n_outputs, n_neurons_to_hl, n_hidden_layers):
            self.n_inputs = n_inputs
            self.n_outputs = n_outputs
            self.n_hidden_layers = n_hidden_layers
            self.n_neurons_to_hl = n_neurons_to_hl

            # Do not touch
            self._create_network()
            self._n_weights = None
            # end

        def _create_network(self):
            if self.n_hidden_layers>0:
                # create the first layer
                self.layers = [NeuronLayer( self.n_neurons_to_hl,self.n_inputs )]

                # create hidden layers
                self.layers += [NeuronLayer( self.n_neurons_to_hl,self.n_neurons_to_hl ) for _ in range(0,self.n_hidden_layers)]

                # hidden-to-output layer
                self.layers += [NeuronLayer( self.n_outputs,self.n_neurons_to_hl )]
            else:
                # If we don't require hidden layers
                self.layers = [NeuronLayer( self.n_outputs,self.n_inputs )]

        def get_weights(self):
            weights = []

            for layer in self.layers:
                for neuron in layer.neurons:
                    weights += neuron.weights

            return weights

        @property
        def n_weights(self):
            if not self._n_weights:
                self._n_weights = 0
                for layer in self.layers:
                    for neuron in layer.neurons:
                        self._n_weights += neuron.n_inputs+1 # +1 for bias weight
            return self._n_weights

        def set_weights(self, weights ):
            assert len(weights)==self.n_weights, "Incorrect amount of weights."

            stop = 0
            for layer in self.layers:
                for neuron in layer.neurons:
                    start, stop = stop, stop+(neuron.n_inputs+1)
                    neuron.set_weights( weights[start:stop] )
            return self

        def update(self, inputs ):
            assert len(inputs)==self.n_inputs, "Incorrect amount of inputs."

            for layer in self.layers:
                outputs = []
                for neuron in layer.neurons:
                    tot = neuron.sum(inputs) + neuron.weights[-1]*BIAS
                    outputs.append( self.sigmoid(tot) )
                inputs = outputs   
            return outputs

        def sigmoid(self, activation,response=1 ):
            # the activation function
            try:
                return 1/(1+math.e**(-activation/response))
            except OverflowError:
                return float("inf")

        def __str__(self):
            return '\n'.join([str(i+1)+' '+str(layer) for i,layer in enumerate(self.layers)])

更有效的实施(学习)

如果您正在寻找一个更有效的学习神经网络示例(反向传播),请查看我的neural network Github repository here

答案 1 :(得分:7)

嗯,这很棘手。我之前遇到过同样的问题,但是我找不到好的但很重要的数学加载解释和准备使用的实现。

准备使用实现(例如PyBrain)的问题在于它们隐藏了细节,因此有兴趣学习如何实现ANN的人需要其他东西。阅读此类解决方案的代码也具有挑战性,因为他们经常使用启发式方法来提高性能,这使得代码更难以遵循启动器。

但是,您可以使用一些资源:

http://msdn.microsoft.com/en-us/magazine/jj658979.aspx

http://itee.uq.edu.au/~cogs2010/cmc/chapters/BackProp/

http://www.codeproject.com/Articles/19323/Image-Recognition-with-Neural-Networks

http://freedelta.free.fr/r/php-code-samples/artificial-intelligence-neural-network-backpropagation/

答案 2 :(得分:6)

以下是如何使用numpy实现前馈神经网络的示例。首先导入numpy并指定输入和目标的尺寸。

import numpy as np

input_dim = 1000
target_dim = 10

我们现在将构建网络结构。正如Bishop伟大的“模式识别和机器学习”中所建议的那样,你可以简单地将你的numpy矩阵的最后一行视为偏差权重,将你激活的最后一列视为偏见神经元。然后,第一个/最后一个权重矩阵的输入/输出维数需要大1。

dimensions = [input_dim+1, 500, 500, target_dim+1]

weight_matrices = []
for i in range(len(dimensions)-1):
  weight_matrix = np.ones((dimensions[i], dimensions[i]))
  weight_matrices.append(weight_matrix)

如果您的输入存储在2d numpy矩阵中,其中每行对应一个样本,并且列对应于样本的属性,则可以通过网络传播,如下所示:(假设logistic sigmoid函数作为激活函数)

def activate_network(inputs):
  activations = [] # we store the activations for each layer here
  a = np.ones((inputs.shape[0], inputs.shape[1]+1)) #add the bias to the inputs
  a[:,:-1] = inputs

  for w in weight_matrices:
    x = a.dot(w) # sum of weighted inputs
    a = 1. / (1. - np.exp(-x)) # apply logistic sigmoid activation
    a[:,-1] = 1. # bias for the next layer.
    activations.append(a)

  return activations

activations中的最后一个元素将是您网络的输出,但要小心,您需要省略偏差的附加列,因此您的输出将为activations[-1][:,:-1]

要训练网络,您需要实现反向传播,这需要一些额外的代码行。基本上,您需要从activations的最后一个元素循环到第一个元素。在每个反向传播步骤之前,确保将错误信号中的偏置列设置为零。

答案 3 :(得分:5)

Here是本机python中的backprop算法。

答案 4 :(得分:0)

你试过PyBrain吗?看起来很顺利documented

相关问题