#!python # CS 42 Exercise # Uncomment stuff as needed # # comments are to end-of-line # For multi-line comments, start and end with """ , same as for docstrings # Keep lines under 80 characters. # To continue a line, use \ at the end of the line before from neuron import LinearNeuron #, QuadraticNeuron #from cancer_data import cancer_input, cancer_output # create a 2-input LinearNeuron neuron2 neuron2 = LinearNeuron(2, 0.1) # Preliminary stuff print neuron2.operate([0, 0]) # neuron2.learn([1, 1], 1) # neuron2.display() # This vector lists a pair of binary inputs for each training set inputs2 = [[0, 0], [0, 1], [1, 0], [1, 1]] # For the 16 training sets below, each output corresponds to one # of the input pairs in the list inputs2 above zero_outputs = [0, 0, 0, 0] and_outputs = [0, 0, 0, 1] nimplies_outputs = [0, 0, 1, 0] id1_outputs = [0, 0, 1, 1] nimplied_outputs = [0, 1, 0, 0] id2_outputs = [0, 1, 0, 1] xor_outputs = [0, 1, 1, 0] or_outputs = [0, 1, 1, 1] nor_outputs = [1, 0, 0, 0] iff_outputs = [1, 0, 0, 1] not2_outputs = [1, 0, 1, 0] implied_outputs = [1, 0, 1, 1] not1_outputs = [1, 1, 0, 0] implies_outputs = [1, 1, 0, 1] nand_outputs = [1, 1, 1, 0] one_outputs = [1, 1, 1, 1] # Train the same LinearNeuron neuron2 on each training set # and assess its performance # limit is the limit on number of epochs limit = 100 """ neuron2.train_and_assess("zero: ", inputs2, zero_outputs, limit, False) neuron2.train_and_assess("one: ", inputs2, one_outputs, limit, False) neuron2.train_and_assess("id1: ", inputs2, id1_outputs, limit, False) neuron2.train_and_assess("id12 ", inputs2, id2_outputs, limit, False) neuron2.train_and_assess("not1: ", inputs2, not1_outputs, limit, False) neuron2.train_and_assess("not2: ", inputs2, not2_outputs, limit, False) neuron2.train_and_assess("and: ", inputs2, and_outputs, limit, False) neuron2.train_and_assess("or: ", inputs2, or_outputs, limit, False) neuron2.train_and_assess("nand: ", inputs2, nand_outputs, limit, False) neuron2.train_and_assess("nor: ", inputs2, nor_outputs, limit, False) neuron2.train_and_assess("implies: ", inputs2, implies_outputs, limit, False) neuron2.train_and_assess("nimplies:", inputs2, nimplies_outputs, limit, False) neuron2.train_and_assess("implied: ", inputs2, implied_outputs, limit, False) neuron2.train_and_assess("nimplied:", inputs2, nimplied_outputs, limit, False) neuron2.train_and_assess("xor: ", inputs2, xor_outputs, limit, False) neuron2.train_and_assess("iff: ", inputs2, iff_outputs, limit, False) """ """ # for difficult sets, try a QuadraticNeuron neuron22 = QuadraticNeuron(2, 0.1) neuron4 = QuadraticNeuron(2, 0.1) neuron22.train_and_assess("xor quadratic:", inputs2, xor_outputs, limit, False) neuron22.train_and_assess("iff quadratic:", inputs2, iff_outputs, limit, False) # Try a LinearNeuron on the cancer data neuron9 = LinearNeuron(9, 0.05) neuron9.train_and_assess("cancer:", cancer_input, cancer_output, limit, False) # Try a QuadraticNeuron on the cancer data. Use a larger limit and smaller rate neuron99 = QuadraticNeuron(9, 0.01) larger_limit = 500 neuron99.train_and_assess("quadratic cancer:", \ cancer_input, cancer_output, larger_limit, False) """