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")
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")
class OutputLayer: """ Represents the output layer of the network. """ def __init__(self, inputLayer): """" Constructor for OutputLayer class. :param inputLayer: the input layer in the network. """ self.inputLayer = inputLayer # Initialize weights as zeros weights = const.INIT_WEIGHTS if self.inputLayer.bias: weights.append(0) # Create output neuron self.neuron = Neuron(neighbors=inputLayer.neurons, weights=weights, output=True) def add_vector_to_weights(self, vector): """ Changes the weights of a neuron by adding a vector to the weights vector. :param vector: vector to add to the weights :returns: None """ for index in range(len(vector)): self.neuron.weights[index] += vector[index] def get_output(self, inputs): """ Feeds the inputs to the input layer and then its output to the output layer. :param inputs: the inputs to the network. :returns: Returns the networks output. """ output = self.inputLayer.get_outputs(inputs) return self.neuron.get_output(output)