Ejemplo n.º 1
0
Archivo: dmc.py Proyecto: hakillha/dmc
def main(train, test, out):
  
  TRAIN_FILE = train
  TEST_FILE  = test
  OUT_FILE   = out
  
  img = Image.open(TRAIN_FILE) # read double moons image from .png file
  pixels = img.load()          # generate pixel map
  width = img.size[0]
  height = img.size[1]
  
  training_set = dict()
  
  for i in range(width):
    for j in range(height):
      if pixels[i,j] == BLUE:   # if pixel is blue
        training_set[i,j] = BOT # set value to bottom
      elif pixels[i,j] == RED:  # if pixel is red
        training_set[i,j] = TOP # set value to top
  
  
  # create neuron with 2 input nodes
  n = Neuron(2) # x-input, y-input
  print "Neuron created."
  # training
  print "Training..."
  counter = 0
  while True:
    errors = 0
    for p in training_set:
      errors += n.train_step(p, training_set[p])
      counter += 1
      print "====="
      
    if errors < n.get_margin() * len(training_set):
      break
  
  print "Length of training set: " + str(len(training_set))
  print "Iterations: " + str(counter)
  
  # test cases
  img = Image.open(TEST_FILE)
  pixels = img.load()
  width = img.size[0]
  height = img.size[1]
  
  for i in range(width):
    for j in range(height):
      if pixels[i,j] == BLACK:
        n.set_input(0, i)
        n.set_input(1, j)
        n.activate()
        ans = n.get_output()
        if ans == TOP:
          pixels[i,j] = RED
        elif ans == BOT:
          pixels[i,j] = BLUE
  
  img.save(OUT_FILE, "PNG")
Ejemplo n.º 2
0
Archivo: dmc.py Proyecto: hakillha/dmc
def main(train, test, out):

    TRAIN_FILE = train
    TEST_FILE = test
    OUT_FILE = out

    img = Image.open(TRAIN_FILE)  # read double moons image from .png file
    pixels = img.load()  # generate pixel map
    width = img.size[0]
    height = img.size[1]

    training_set = dict()

    for i in range(width):
        for j in range(height):
            if pixels[i, j] == BLUE:  # if pixel is blue
                training_set[i, j] = BOT  # set value to bottom
            elif pixels[i, j] == RED:  # if pixel is red
                training_set[i, j] = TOP  # set value to top

    # create neuron with 2 input nodes
    n = Neuron(2)  # x-input, y-input
    print "Neuron created."
    # training
    print "Training..."
    counter = 0
    while True:
        errors = 0
        for p in training_set:
            errors += n.train_step(p, training_set[p])
            counter += 1
            print "====="

        if errors < n.get_margin() * len(training_set):
            break

    print "Length of training set: " + str(len(training_set))
    print "Iterations: " + str(counter)

    # test cases
    img = Image.open(TEST_FILE)
    pixels = img.load()
    width = img.size[0]
    height = img.size[1]

    for i in range(width):
        for j in range(height):
            if pixels[i, j] == BLACK:
                n.set_input(0, i)
                n.set_input(1, j)
                n.activate()
                ans = n.get_output()
                if ans == TOP:
                    pixels[i, j] = RED
                elif ans == BOT:
                    pixels[i, j] = BLUE

    img.save(OUT_FILE, "PNG")
Ejemplo n.º 3
0
class Network:
    """
    Represents a neural network using sigmoidal activations.

    """

    def __init__(self, layerCounts, activation=sigmoid.Tanh):
        """Constructs a neural network with a set of layers.
        >>> n = Network([2,3,2])
        >>> len(n.neurons)
        3
        >>> len(n.neurons[0])
        2
        >>> len(n.neurons[1])
        3
        >>> len(n.neurons[2])
        2
        >>> len(n.connections)
        2
        >>> len(n.connections[0])
        9
        >>> len(n.connections[1])
        8
        """
        self.bias = Neuron(sigmoid.Constant)

        self.neurons = []
        self.connections = []

        for layer in range(len(layerCounts)):
            neuron_layer = []
            connection_layer = []
            for i in range(layerCounts[layer]):
                # Input neurons shouldn't activate their input.

                cur_neuron = None
                if layer is 0:
                    # input neurons do not activate.
                    cur_neuron = Neuron(sigmoid.Linear)
                else:
                    # hidden and output neurons use normal sigmoids
                    cur_neuron = Neuron(activation)

                    # for every neuron to the left
                    for anterior in self.neurons[layer-1]:

                        #create an anterior connection to CUR_NEURON
                        connection = Connection(anterior, cur_neuron)
                        connection_layer.append(connection)

                        # add this connection to the posterior connections
                        # of ANTERIOR.
                        anterior.posteriors.append(connection)
                        cur_neuron.anteriors.append(connection)

                    # do the same for the BIAS connection.
                    bias_connection = Connection(self.bias, cur_neuron)
                    connection_layer.append(bias_connection)
                    self.bias.posteriors.append(bias_connection)

                # add the current neuron.
                neuron_layer.append(cur_neuron)

            #if connections were made
            if layer != 0:
                self.connections.append(connection_layer)

            # append the neural layer.
            self.neurons.append(neuron_layer)

    def train(self, datapair, rate):
        """Trains the network with a certain learning rate on a datapair.
        Returns the net error across ouytput neurons.
        Assunmes input matches network size."""
        inp = datapair[0]
        desired = datapair[1]

        self.feedforward(inp)
        error = self.backpropagate(desired, rate)

        return error

    def feedforward(self, inputs):
        """ Passes the input data through
        the network and creates the output """

        assert len(inputs) == len(self.neurons[0]), \
            "Input vector does not match the network intut layer"
        for i in range(len(inputs)):
            self.neurons[0][i].feed(inputs[i])

        self.bias.activate()

        for layer in self.neurons:
            for neuron in layer:
                neuron.activate()

        return [x.output for x in self.neurons[-1]]

    def backpropagate(self, desired, learning_rate):
        """ Updates the weights based on the desired output and
        the learning rate using error backpropagation."""
        outputs = [n.output for n in self.neurons[-1]]
        losses = [x[0] - x[1] for x in zip(outputs, desired)]
        error = sum(map(lambda x: x ** 2, losses))

        # manually set the error coefficients for the output error.
        for output_neuron, loss in zip(self.neurons[-1], losses):
            output_neuron.set_error(loss)

        for n_layer in self.neurons[:-1][::-1]:
            for neuron in n_layer:
                error_coef = 0
                for con in neuron.posteriors:
                    error_coef += con.posterior.error * con.weight

                    con.update_weight(learning_rate)

                neuron.set_error(error_coef)

        # fix bias error
        for con in self.bias.posteriors:
            con.update_weight(learning_rate)

        return error

    def set_weight(self, layer, left, right, value):
        """Sets a weight from a neuron indexed LEFT in
        LAYER to a neuron indexed RIGHT on the posterior neuron
        with a new weight VALUE."""
        self.neurons[layer][left].posteriors[right].weight = value
        return value

    def get_weight(self, layer, left, right):
        """Gets a weight from a neuron indexed LEFT in
        LAYER to a neuron indexed RIGHT on the posterior neuron"""
        return self.neurons[layer][left].posteriors[right].weight

    def save(self, filename):
        """Requires json pickle."""
        f = open(filename, "wb")
        
        pickle.dump(self, f)

    def load(filename):
        """Requires json pickle."""
        f = open(filename, "rb")
        
        return pickle.load(f)
Ejemplo n.º 4
0
    master.update()
    radius_range[0] = r1Slider.get() * 10
    radius_range[1] = r2Slider.get() * 10
    distance = distanceSlider.get() * 10

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill(black)

    xyPoints = getMofifiedPoints(points)

    pygame.draw.line(
        screen, (0, 255, 0),
        [0, (-neuron.w[1] * (-250) + neuron.w[0]) / neuron.w[2] + center[1]],
        [500, (-neuron.w[1] * (250) + neuron.w[0]) / neuron.w[2] + center[1]])

    for x in xyPoints:
        color = (0, 255, 255)
        if neuron.activate(x) and x[2] == 1:
            color = (255, 0, 0)
        elif not neuron.activate(x) and x[2] == 0:
            color = (0, 0, 255)
        elif neuron.activate(x) and x[2] == 0:
            color = (255, 0, 255)
        pygame.draw.circle(screen, color,
                           [int(x[0] + center[0]),
                            int(x[1] + center[1])], 1)
    pygame.display.flip()
Ejemplo n.º 5
0
class MLP:
    # Class Constructor
    # This method initializes all properties of this class.
    # @param iterations: The number of iterations.
    # @param architecture: (Number of hidden neurons, Number of input Neurons)
    # @param patterns: Collection of training patterns
    def __init__(self, patterns, iterations, architecture):
        self.hidden_dims, self.input_dims = architecture
        self.iteration = 0
        self.iterations = iterations
        self.patterns = list(patterns)
        self.absolute_error = 0.0
        self.squared_error = 0.0
        self.hidden_layer = None
        self.input_layer = None
        self.output = None
        self.theta = 0.01
        self.rand = random.Random()
        self.initialize()

    # Initialize the network based on its architecture.
    def initialize(self):
        self.input_layer = Layer(self.input_dims)
        self.hidden_layer = Layer(self.hidden_dims, self.input_layer,
                                  self.rand)
        self.output = Neuron(self.hidden_layer, self.rand)
        self.iteration = 0

    # Adjust the network weights.
    def adjust_weights(self, delta):
        self.output.adjust_weights(delta)
        for neuron in self.hidden_layer.get_layer():
            neuron.adjust_weights(self.output.error_feedback(neuron))

    # Propagates the perceptron and calculate the output.
    def activate(self, pattern):
        for i in range(len(pattern[0])):
            self.input_layer.get_layer()[i].set_output_neuron(pattern[0][i])
        for neuron in self.hidden_layer.get_layer():
            neuron.activate()
        self.output.activate()
        return self.output.output_neuron()

    # Do the training of the perceptron network.
    def train(self):
        error = 1.0
        while error > self.theta:
            error = 0.0
            for pattern in self.patterns:
                delta = pattern[1] - self.activate(pattern)
                self.absolute_error += delta
                self.adjust_weights(delta)
                error += math.pow(delta, 2)
                self.squared_error += error
            self.iteration += 1
            if self.iteration > self.iterations:
                self.initialize()
        self.absolute_error = self.absolute_error / self.iteration
        self.squared_error = self.squared_error / self.iteration

    # Test the network after trained.
    def execute(self, pattern):
        return self.activate(pattern)