def run(run_on_loop, num_iterations): """ handles running perceptron in two ways, on a loop or just once preliminary setup work is done independent of the mode args: run_on_loop is a boolean indicating whether to loop or not; num_iterations is how many times to train """ # create feature space features = Process.process_features() # convert train data to vectors train_data = Process.make_vectors("train.positive", features, 1) train_data.extend(Process.make_vectors("train.negative", features, -1)) # convert test data to vectors test_data = Process.make_vectors("test.positive", features, 1) test_data.extend(Process.make_vectors("test.negative", features, -1)) # train perceptron - returns weights, biases and errors from each iteration print "Training..." train_results = Perceptron.train(train_data, num_iterations) print "End of training." print "----" # grab the information returned by the training weights_arr = train_results.get("weights") bias_arr = train_results.get("bias") train_errors = train_results.get("errors") test_errors = [] print "Testing..." if (run_on_loop): # loop and graph mode, runs perceptron until reaches num_iterations # each iteration uses a different set of weights and bias, corresponding to those produced in training n = 0 while n < num_iterations: print "Iteration [{0}]".format(n+1) # run test perceptron and save results in array for plotting test_errors.append(Perceptron.test(test_data, weights_arr[n], bias_arr[n])) n += 1 # plot graph of train and test errors Process.plot_results(train_errors, test_errors) else: # simple mode, runs perceptron once # uses only the final weight vector and bias test_errors = Perceptron.test(test_data, weights_arr[-1], bias_arr[-1]) print "End of testing." print "----" pass