def performNN(all_extracted_features, all_targets):
    from pyneurgen.neuralnet import NeuralNet
    #from pyneurgen.nodes import BiasNode, Connection
    net = NeuralNet()
    net.init_layers(len(all_extracted_features[0]), [2], 1)
    
    net.randomize_network()
    net.set_halt_on_extremes(True)
    
    #   Set to constrain beginning weights to -5 to 5
    #       Just to show we can
    #net.set_random_constraint(.5)
    net.set_learnrate(.001)
    
    net.set_all_inputs(all_extracted_features)
    net.set_all_targets(all_targets)
    
    length = len(all_extracted_features)
    learn_end_point = int(length * .8)
    
    net.set_learn_range(0, learn_end_point)
    net.set_test_range(learn_end_point + 1, length - 1)
    
    net.layers[1].set_activation_type('tanh')
    net.learn(epochs=150, show_epoch_results=True, random_testing=True)
    mse = net.test()
    print mse
def buildIrisNetwork(all_inputs, all_targets):
	net = NeuralNet()
	net.init_layers(4, [6], 3)

	net.randomize_network()
	net.set_halt_on_extremes(True)

	#   Set to constrain beginning weights to -.5 to .5
	#       Just to show we can
	#net.set_random_constraint(.5)
	net.set_learnrate(.1)

	net.set_all_inputs(all_inputs)
	net.set_all_targets(all_targets)

	length = len(all_inputs)
	learn_end_point = int(length * .5)

	net.set_learn_range(0, learn_end_point)
	net.set_test_range(learn_end_point + 1, length-1)

	net.layers[0].set_activation_type('tanh')
	net.layers[1].set_activation_type('tanh')
	net.layers[2].set_activation_type('threshold')
	return net
def buildIrisNetwork(all_inputs, all_targets):
    net = NeuralNet()
    net.init_layers(4, [6], 3)

    net.randomize_network()
    net.set_halt_on_extremes(True)

    #   Set to constrain beginning weights to -.5 to .5
    #       Just to show we can
    #net.set_random_constraint(.5)
    net.set_learnrate(.1)

    net.set_all_inputs(all_inputs)
    net.set_all_targets(all_targets)

    length = len(all_inputs)
    learn_end_point = int(length * .5)

    net.set_learn_range(0, learn_end_point)
    net.set_test_range(learn_end_point + 1, length - 1)

    net.layers[0].set_activation_type('tanh')
    net.layers[1].set_activation_type('tanh')
    net.layers[2].set_activation_type('threshold')
    return net
Example #4
0
class TestNeuralNet(unittest.TestCase):
    """
    Tests NeuralNet

    """
    def setUp(self):

        self.net = NeuralNet()

        layer = Layer(0, 'input')
        layer.add_nodes(1, 'input')
        self.net.layers.append(layer)

        layer = Layer(1, 'hidden')
        layer.add_nodes(1, 'hidden')
        self.net.layers.append(layer)

        layer = Layer(2, 'output')
        layer.add_nodes(1, 'output')
        self.net.layers.append(layer)

        #   Specify connections
        self.net.layers[1].nodes[0].add_input_connection(
            Connection(self.net.layers[0].nodes[0],
                       self.net.layers[1].nodes[0], 1.00))

        self.net.layers[2].nodes[0].add_input_connection(
            Connection(self.net.layers[1].nodes[0],
                       self.net.layers[2].nodes[0], .75))

        self.net._epochs = 1
        self.net.copy_levels = 0
        self.net._allinputs = [[.1], [.2], [.3], [.4], [.5]]
        self.net._alltargets = [[.2], [.4], [.6], [.8], [1.0]]

        self.net.input_layer = self.net.layers[0]
        self.net.output_layer = self.net.layers[-1]

    def test_set_halt_on_extremes(self):

        self.net._halt_on_extremes = 'fail'
        self.net.set_halt_on_extremes(True)
        self.assertEqual(True, self.net._halt_on_extremes)

        self.net._halt_on_extremes = 'fail'
        self.net.set_halt_on_extremes(False)
        self.assertEqual(False, self.net._halt_on_extremes)

        self.net._halt_on_extremes = 'fail'
        self.failUnlessRaises(ValueError, self.net.set_halt_on_extremes, 'a')

        self.net._halt_on_extremes = 'fail'
        self.failUnlessRaises(ValueError, self.net.set_halt_on_extremes, 3)

    def test_get_halt_on_extremes(self):

        self.net.set_halt_on_extremes(True)
        self.assertEqual(True, self.net.get_halt_on_extremes())

        self.net.set_halt_on_extremes(False)
        self.assertEqual(False, self.net.get_halt_on_extremes())

    def test_set_random_constraint(self):

        self.net._random_constraint = 'fail'
        self.net.set_random_constraint(.1)
        self.assertEqual(.1, self.net._random_constraint)

        self.failUnlessRaises(ValueError, self.net.set_random_constraint, 3)
        self.failUnlessRaises(ValueError, self.net.set_random_constraint, 1)
        self.failUnlessRaises(ValueError, self.net.set_random_constraint, 0.0)
        self.failUnlessRaises(ValueError, self.net.set_random_constraint, -.2)
        self.failUnlessRaises(ValueError, self.net.set_random_constraint, 'a')

    def test_get_random_constraint(self):

        self.net.set_random_constraint(.2)
        self.assertEqual(.2, self.net.get_random_constraint())

        self.net.set_random_constraint(.8)
        self.assertEqual(.8, self.net.get_random_constraint())

    def test_set_epochs(self):

        self.net._epochs = 'fail'
        self.net.set_epochs(3)
        self.assertEqual(3, self.net._epochs)

        self.failUnlessRaises(ValueError, self.net.set_epochs, .3)
        self.failUnlessRaises(ValueError, self.net.set_epochs, 0)
        self.failUnlessRaises(ValueError, self.net.set_epochs, -3)
        self.failUnlessRaises(ValueError, self.net.set_epochs, -.2)
        self.failUnlessRaises(ValueError, self.net.set_epochs, 'a')

    def test_get_epochs(self):

        self.net.set_epochs(3)
        self.assertEqual(3, self.net.get_epochs())

    def test_set_time_delay(self):

        self.net._time_delay = 'fail'
        self.net.set_time_delay(3)
        self.assertEqual(3, self.net._time_delay)

        self.failUnlessRaises(ValueError, self.net.set_time_delay, .3)
        self.failUnlessRaises(ValueError, self.net.set_time_delay, -3)
        self.failUnlessRaises(ValueError, self.net.set_time_delay, -.2)
        self.failUnlessRaises(ValueError, self.net.set_time_delay, 'a')

    def test_get_time_delay(self):

        self.net.set_time_delay(3)
        self.assertEqual(3, self.net.get_time_delay())

    def test_set_all_inputs(self):

        pass

    def test_set_all_targets(self):

        pass

    def test_set_learnrate(self):

        pass

    def test_get_learnrate(self):

        pass

    def test__set_data_range(self):

        pass

    def test_set_learn_range(self):

        pass

    def test_get_learn_range(self):

        pass

    def test__check_time_delay(self):

        pass

    def test_get_learn_data(self):

        pass

    def test_get_validation_data(self):

        pass

    def test_get_test_data(self):

        pass

    def test__get_data(self):

        pass

    def test__get_randomized_position(self):

        pass

    def test__check_positions(self):

        pass

    def test_set_validation_range(self):

        pass

    def test_get_validation_range(self):

        pass

    def test_set_test_range(self):

        pass

    def test_get_test_range(self):

        pass

    def test_init_layers(self):

        pass

    def test__init_connections(self):

        pass

    def test__connect_layer(self):

        pass

    def test__build_output_conn(self):

        pass

    def test_randomize_network(self):

        pass

    def test_learn(self):

        pass

    def test_test(self):

        pass

    def test_calc_mse(self):

        self.assertAlmostEqual(10.0 / 2.0, self.net.calc_mse(100.0, 10))

    def test_process_sample(self):

        pass

    def test__feed_forward(self):

        #   simplify activations
        self.net.layers[0].set_activation_type('sigmoid')
        self.net.layers[1].set_activation_type('sigmoid')
        self.net.layers[2].set_activation_type('sigmoid')

        #   These values should be replaced
        self.net.layers[1].nodes[0].set_value(1000.0)
        self.net.layers[2].nodes[0].set_value(1000.0)

        self.assertEqual(1000.0, self.net.layers[1].nodes[0].get_value())
        self.assertEqual(1000.0, self.net.layers[2].nodes[0].get_value())

        self.net.layers[0].load_inputs([.2])

        self.net._feed_forward()

        self.assertEqual(.2, self.net.layers[0].nodes[0].get_value())
        self.assertEqual(
            sigmoid(.2) * 1.0, self.net.layers[1].nodes[0].get_value())

        self.assertEqual(
            sigmoid(sigmoid(.2) * 1.0) * .75,
            self.net.layers[2].nodes[0].get_value())

    def test__back_propagate(self):

        pass

    def test__update_error(self):

        pass

    def test__adjust_weights(self):
        """
        This function goes through layers starting with the top hidden layer
        and working its way down to the input layer.

        At each layer, the weights are adjusted based upon the errors.

        """
        halt_on_extremes = True

        for layer_no in range(len(self.net.layers) - 2, 0, -1):
            layer = self.net.layers[layer_no + 1]
            layer.adjust_weights(self.net._learnrate, halt_on_extremes)

    def test__zero_errors(self):

        for layer in self.net.layers[1:]:
            for node in layer.nodes:
                node.error = 1000

        self.net._zero_errors()

        for layer in self.net.layers[1:]:
            for node in layer.nodes:
                self.failIfEqual(1000, node.error)

    def test_calc_output_error(self):

        pass

    def test_calc_sample_error(self):

        pass

    def test__copy_levels(self):

        pass

    def test__parse_inputfile_layer(self):

        pass

    def test__parse_inputfile_node(self):

        pass

    def test__parse_inputfile_conn(self):

        pass

    def test__parse_inputfile_copy(self):

        pass

    def test__parse_node_id(self):

        pass

    def test_load(self):

        pass

    def test_output_values(self):

        pass

    def test__node_id(self):

        pass

    def test_save(self):

        pass
from pylab import plot, legend, subplot, grid, xlabel, ylabel, show, title
from pyneurgen.neuralnet import NeuralNet
from pyneurgen.nodes import BiasNode, Connection
from pybrain.utilities import percentError

from iris import neurgenData
from src.utilities import percentError

#   Build the inputs
all_inputs, all_targets = neurgenData()

net = NeuralNet()
net.init_layers(4, [6], 3)

net.randomize_network()
net.set_halt_on_extremes(True)

#   Set to constrain beginning weights to -.5 to .5
#       Just to show we can
#net.set_random_constraint(.5)
net.set_learnrate(.1)

net.set_all_inputs(all_inputs)
net.set_all_targets(all_targets)

length = len(all_inputs)
learn_end_point = int(length * .5)

net.set_learn_range(0, learn_end_point)
net.set_test_range(learn_end_point + 1, length - 1)
Example #6
0
    random.shuffle(pop_sort)

    for item in pop_sort:
        yield item

#   Build the inputs
for position, target in population_gen(population):
    pos = float(position)
    all_inputs.append([random.random(), pos * factor])
    all_targets.append([target])

net = NeuralNet()
net.init_layers(2, [10], 1)

net.randomize_network()
net.set_halt_on_extremes(True)

#   Set to constrain beginning weights to -.5 to .5
#       Just to show we can
net.set_random_constraint(.5)
net.set_learnrate(.1)

net.set_all_inputs(all_inputs)
net.set_all_targets(all_targets)

length = len(all_inputs)
learn_end_point = int(length * .8)

net.set_learn_range(0, learn_end_point)
net.set_test_range(learn_end_point + 1, length - 1)
class TestNeuralNet(unittest.TestCase):
    """
    Tests NeuralNet

    """

    def setUp(self):

        self.net = NeuralNet()

        layer = Layer(0, 'input')
        layer.add_nodes(1, 'input')
        self.net.layers.append(layer)

        layer = Layer(1, 'hidden')
        layer.add_nodes(1, 'hidden')
        self.net.layers.append(layer)

        layer = Layer(2, 'output')
        layer.add_nodes(1, 'output')
        self.net.layers.append(layer)

        #   Specify connections
        self.net.layers[1].nodes[0].add_input_connection(
            Connection(
                self.net.layers[0].nodes[0],
                self.net.layers[1].nodes[0],
                1.00))

        self.net.layers[2].nodes[0].add_input_connection(
            Connection(
                self.net.layers[1].nodes[0],
                self.net.layers[2].nodes[0],
                .75))

        self.net._epochs = 1
        self.net.copy_levels = 0
        self.net._allinputs = [[.1], [.2], [.3], [.4], [.5]]
        self.net._alltargets = [[.2], [.4], [.6], [.8], [1.0]]

        self.net.input_layer = self.net.layers[0]
        self.net.output_layer = self.net.layers[-1]

    def test_set_halt_on_extremes(self):

        self.net._halt_on_extremes = 'fail'
        self.net.set_halt_on_extremes(True)
        self.assertEqual(True, self.net._halt_on_extremes)

        self.net._halt_on_extremes = 'fail'
        self.net.set_halt_on_extremes(False)
        self.assertEqual(False, self.net._halt_on_extremes)

        self.net._halt_on_extremes = 'fail'
        self.failUnlessRaises(ValueError, self.net.set_halt_on_extremes, 'a')

        self.net._halt_on_extremes = 'fail'
        self.failUnlessRaises(ValueError, self.net.set_halt_on_extremes, 3)

    def test_get_halt_on_extremes(self):

        self.net.set_halt_on_extremes(True)
        self.assertEqual(True, self.net.get_halt_on_extremes())

        self.net.set_halt_on_extremes(False)
        self.assertEqual(False, self.net.get_halt_on_extremes())

    def test_set_random_constraint(self):

        self.net._random_constraint = 'fail'
        self.net.set_random_constraint(.1)
        self.assertEqual(.1, self.net._random_constraint)

        self.failUnlessRaises(ValueError, self.net.set_random_constraint, 3)
        self.failUnlessRaises(ValueError, self.net.set_random_constraint, 1)
        self.failUnlessRaises(ValueError, self.net.set_random_constraint, 0.0)
        self.failUnlessRaises(ValueError, self.net.set_random_constraint, -.2)
        self.failUnlessRaises(ValueError, self.net.set_random_constraint, 'a')

    def test_get_random_constraint(self):

        self.net.set_random_constraint(.2)
        self.assertEqual(.2, self.net.get_random_constraint())

        self.net.set_random_constraint(.8)
        self.assertEqual(.8, self.net.get_random_constraint())

    def test_set_epochs(self):

        self.net._epochs = 'fail'
        self.net.set_epochs(3)
        self.assertEqual(3, self.net._epochs)

        self.failUnlessRaises(ValueError, self.net.set_epochs, .3)
        self.failUnlessRaises(ValueError, self.net.set_epochs, 0)
        self.failUnlessRaises(ValueError, self.net.set_epochs, -3)
        self.failUnlessRaises(ValueError, self.net.set_epochs, -.2)
        self.failUnlessRaises(ValueError, self.net.set_epochs, 'a')

    def test_get_epochs(self):

        self.net.set_epochs(3)
        self.assertEqual(3, self.net.get_epochs())

    def test_set_time_delay(self):

        self.net._time_delay = 'fail'
        self.net.set_time_delay(3)
        self.assertEqual(3, self.net._time_delay)

        self.failUnlessRaises(ValueError, self.net.set_time_delay, .3)
        self.failUnlessRaises(ValueError, self.net.set_time_delay, -3)
        self.failUnlessRaises(ValueError, self.net.set_time_delay, -.2)
        self.failUnlessRaises(ValueError, self.net.set_time_delay, 'a')

    def test_get_time_delay(self):

        self.net.set_time_delay(3)
        self.assertEqual(3, self.net.get_time_delay())

    def test_set_all_inputs(self):

        pass

    def test_set_all_targets(self):

        pass

    def test_set_learnrate(self):

        pass

    def test_get_learnrate(self):

        pass

    def test__set_data_range(self):

        pass

    def test_set_learn_range(self):

        pass

    def test_get_learn_range(self):

        pass

    def test__check_time_delay(self):

        pass

    def test_get_learn_data(self):

        pass

    def test_get_validation_data(self):

        pass

    def test_get_test_data(self):

        pass

    def test__get_data(self):

        pass

    def test__get_randomized_position(self):

        pass

    def test__check_positions(self):

        pass

    def test_set_validation_range(self):

        pass

    def test_get_validation_range(self):

        pass

    def test_set_test_range(self):

        pass

    def test_get_test_range(self):

        pass

    def test_init_layers(self):

        pass

    def test__init_connections(self):

        pass

    def test__connect_layer(self):

        pass

    def test__build_output_conn(self):

        pass

    def test_randomize_network(self):

        pass

    def test_learn(self):

        pass

    def test_test(self):

        pass

    def test_calc_mse(self):

        self.assertAlmostEqual(10.0 / 2.0, self.net.calc_mse(100.0, 10))

    def test_process_sample(self):

        pass

    def test__feed_forward(self):

        #   simplify activations
        self.net.layers[0].set_activation_type('sigmoid')
        self.net.layers[1].set_activation_type('sigmoid')
        self.net.layers[2].set_activation_type('sigmoid')

        #   These values should be replaced
        self.net.layers[1].nodes[0].set_value(1000.0)
        self.net.layers[2].nodes[0].set_value(1000.0)

        self.assertEqual(1000.0, self.net.layers[1].nodes[0].get_value())
        self.assertEqual(1000.0, self.net.layers[2].nodes[0].get_value())

        self.net.layers[0].load_inputs([.2])

        self.net._feed_forward()

        self.assertEqual(.2, self.net.layers[0].nodes[0].get_value())
        self.assertEqual(
                sigmoid(.2) * 1.0,
                self.net.layers[1].nodes[0].get_value())

        self.assertEqual(
                sigmoid(sigmoid(.2) * 1.0) * .75,
                self.net.layers[2].nodes[0].get_value())

    def test__back_propagate(self):

        pass

    def test__update_error(self):

        pass

    def test__adjust_weights(self):
        """
        This function goes through layers starting with the top hidden layer
        and working its way down to the input layer.

        At each layer, the weights are adjusted based upon the errors.

        """
        halt_on_extremes = True

        for layer_no in range(len(self.net.layers) - 2, 0, -1):
            layer = self.net.layers[layer_no + 1]
            layer.adjust_weights(self.net._learnrate, halt_on_extremes)

    def test__zero_errors(self):

        for layer in self.net.layers[1:]:
            for node in layer.nodes:
                node.error = 1000

        self.net._zero_errors()

        for layer in self.net.layers[1:]:
            for node in layer.nodes:
                self.failIfEqual(1000, node.error)

    def test_calc_output_error(self):

        pass

    def test_calc_sample_error(self):

        pass

    def test__copy_levels(self):

        pass

    def test__parse_inputfile_layer(self):

        pass

    def test__parse_inputfile_node(self):

        pass

    def test__parse_inputfile_conn(self):

        pass

    def test__parse_inputfile_copy(self):

        pass

    def test__parse_node_id(self):

        pass

    def test_load(self):

        pass

    def test_output_values(self):

        pass

    def test__node_id(self):

        pass

    def test_save(self):

        pass