Package: perceptron
Perceptron implements amultilayer perceptronnetwork written in Python.
This type of network consists of multiple layers of neurons, the first
of which takes the input. The last layer gives the ouput. There can be
multiple middle layers but in this case, it just uses a single one.For further information about multilayer perceptron networks, please
readthisentry on the Wikipedia.Requirementspython>= 2.7InstallationYou can install the package viaeasy_installorpip:easy_installperceptronpipinstallperceptronFeeding ForwardThe neural network uses thehyperbolic tangent (tanh)function.Hyperbolic tangentThe x-axis is the total input to the node. As the input aproaches to 0,
the output starts to climb quickly. With an input of 2, the output is
almos at 1 and doesn’t get much higher. This is a type ofsigmoidfunctions to calculate the output of the neurons.Note: Before runing thefeedforwardalgorithm, the network will
have to query the nodes and connections, and build, in memory, the
position of the network that is relevant to a specific input.Training with BackpropagationThe backpropagation algorithm then performs the following steps.For each node in the output layer:Calculate the difference between the node’s current output and what it should be.Use thedtanhfunction to determine how much the node’s total input has to change.Change the strenght of every inconming input in proportion to the input’s current strength and the learning rate.For each node in the hidden layer:Change the output of the node by the sum of the strength of each output value multiplied by how much its targets has to change.Use thedtanhfunction to determine how much the node’s total input has to change.Change the strenght of every inconming input in proportion to the input’s current strength and the learning rate.The implementation of this algorithm actually calculates all the errors
in advance and then adjusts the weigths, because all the calculations
rely on knowing the current weights rather than the updated weights.Note: Before runingbackpropagationmethod, it’s necessary to
runfeedforwardso that the current output of every node will be
stored in the instance variables.UsageImport the module at the beginning of your file:fromperceptronimportmlpInit theneural network:n=mlp.Net()ExampleIn this example the neurons in the first layer respont to the ids that
are used as input. If a id is present, then the neurons that are strongly
connected to that word become active. The second layer is fed by the
first layer, so it responds to combinations of ids. Finally, the neurons
feed their result to the outputs, and particular combinations may be
strongly or weakly associated with the possible results. In the end,
the final decision is whichever output is strongest classifying an id.fromperceptronimportmlpdefmain():n=mlp.Net()foriinrange(30):n.train([101,103],[201,202,203],201)n.train([102,103],[201,202,203],202)n.train([101],[201,202,203],203)printn.eval([101,103,],[201,202,203])printn.eval([102,103],[201,202,203])printn.eval([103],[201,202,203])if__name__=='__main__':main()That will give the following output.[0.8435967735300776, 0.011059223531796199, 0.017992770688108367]
[-0.028282207517584094, 0.8775955174169334, 0.0032322039490162353]
[0.8459277961565395, -0.011590385221469553, -0.8361964445052618]LicenceCopyright © 2016 Roger Fernandez Guri. It is free software, and may be
redistributed under the terms specified in theLICENCEfile.