HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Artificial Neural Network implementaion

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
neuralimplementaionartificialnetwork

Problem

I'm looking for some general tips on code practices, doing things properly in idiomatic pythonic way. But mostly I want to know if this was build correctly. I tried making neural nets in the past, but failed horrible, but this one seems to be working fine.

The backpropagation method is still not developed because I still haven't figured out the math behind it (if anyone have a good material for a non-engineer I would be very glad).

```
#! /usr/bin/env python
"""
This module is a framework for a Artificial Neural Network.

:param NeuralNetwork: See this documentation for how to use this module..
:type NeuralNetwork: NeuralNetwork

Author: Fernando Rodrigues dos Santos
"""
import random
import math

class NeuralNetwork:
"""USAGE:
- Create a NeuralNetwork object and set it's parameters at your will.
- Use the method start_net(inputs) with the right amount of inputs to calculate the first iteration of the net.
- Use the get_result() method to extract the result of each output node.
- Use the get_weights() method to extract the weight of each node.
- Use the set_weights(weights) method to set the new weight for each node in the net.

# NOT IMPLEMENTED

- Use the back_propagate_error(error) method to propagate back the result with the quantified error
"""
def __init__(self, n_input, n_hidden_layer, n_hidden_nodes, n_output):
"""Creates each layer of the net (input, hidden and output) based on the set parameters
:param n_input: Number of inputs nodes of the net
:type n_input: int
:param n_hidden_layer: Number of hidden layers
:type n_hidden_layer: int
:param n_hidden_nodes: Number of nodes per hidden layer
:type n_hidden_nodes: int
:param n_output: Number of output nodes of the net
:type n_output: int
"""
self.input_layer = Layer(n_input, "Input")

self.hidden_layers = []
for n i

Solution

First off, the below section of code here:

self.hidden_layers = []
for n in xrange(n_hidden_layer):
    self.hidden_layers.append(
        Layer(n_hidden_nodes, "Hidden %i" % (n+1))
    )


Can be shortened to the below, using a generator expression:

self.hidden_layers = [
    Layer(n_hidden_nodes, "Hidden {0}".format(n + 1)) for n in xrange(n_hidden_layer)
]


You also have other places where generator expressions could be used, like here, as a simple example:

for node in self.output_layer.nodes:
    out_weights.append(node.weights)


Do note the use of str.format as well. Using % for string formatting in any version of Python after 2.6 is deprecated, and str.format should be used instead. Here's an example of how str.format is used:

# str.format without positional or named parameters
print "{} {}".format("Hello", "world")

# str.format with positional parameters
print "{1} {0}".format("world", "Hello")

# str.format with named parameters
print "{word1} {word2}".format(word1="Hello", word2="world")


Finally, in any version of Python 2.x, you need to explicitly have all classes inherit from object, like this: class MyClass(object):. If you're using Python 3.x or above, it's okay to just type stuff like clas MyClass:.

Code Snippets

self.hidden_layers = []
for n in xrange(n_hidden_layer):
    self.hidden_layers.append(
        Layer(n_hidden_nodes, "Hidden %i" % (n+1))
    )
self.hidden_layers = [
    Layer(n_hidden_nodes, "Hidden {0}".format(n + 1)) for n in xrange(n_hidden_layer)
]
for node in self.output_layer.nodes:
    out_weights.append(node.weights)
# str.format without positional or named parameters
print "{} {}".format("Hello", "world")

# str.format with positional parameters
print "{1} {0}".format("world", "Hello")

# str.format with named parameters
print "{word1} {word2}".format(word1="Hello", word2="world")

Context

StackExchange Code Review Q#88824, answer score: 2

Revisions (0)

No revisions yet.