def b_3(plot=False): units = [1, 2, 3, 10, 20, 40] lrs = [0.09, 0.09, 0.1, 0.1, 0.1, 0.01] # lrs = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1] for unit, lr in zip(units, lrs): print("\nNeural_Network") model = Neural_Network(len(train_data[0]), [unit], activation="sigmoid") print(model) model.train(train_data, train_labels, max_iter=10000, eeta=lr, batch_size=len(train_data), threshold=1e-6, decay=False) pred = model.predict(train_data) train_acc = accuracy_score(train_labels, pred) * 100 print("Train Set Accuracy: ", train_acc) pred = model.predict(test_data) test_acc = accuracy_score(test_labels, pred) * 100 print("Test Set Accuracy: ", test_acc) if plot: plot_decision_boundary( model.predict, np.array(test_data), np.array(test_labels), "Neural_Network Test Set\n Units in Hidden layers: %s\nAccuracy: %f" % (str(model.hidden_layer_sizes), test_acc))
def b_2(plot=False, units=[5], eeta=0.1, threshold=1e-6): print("\nNeural_Network") model = Neural_Network(len(train_data[0]), units, activation="sigmoid") print(model) model.train(train_data, train_labels, max_iter=5000, eeta=eeta, batch_size=len(train_data), threshold=threshold, decay=False) pred = model.predict(train_data) train_acc = accuracy_score(train_labels, pred) * 100 print("Train Set Accuracy: ", train_acc) pred = model.predict(test_data) test_acc = accuracy_score(test_labels, pred) * 100 print("Test Set Accuracy: ", test_acc) if plot: plot_decision_boundary( model.predict, np.array(train_data), np.array(train_labels), "Neural_Network Train Set\n Units in Hidden layers: %s\nAccuracy: %f" % (str(model.hidden_layer_sizes), train_acc)) plot_decision_boundary( model.predict, np.array(test_data), np.array(test_labels), "Neural_Network Test Set\n Units in Hidden layers: %s\nAccuracy: %f" % (str(model.hidden_layer_sizes), test_acc))
def main(): #make a neural network with set architecture arch = (2,4,1) nn = Neural_Network(arch) #XOR input data X_train = np.array( [ [0,0], [0,1], [1,0], [1,1] ] ) #XOR output data y_train = np.array( [[0],[1],[1],[0]] ) #set max iterations, learning rate, and convergence threshold iters, lr, threshold = 5000, 1, 0.00001 #train the network J_Hist = nn.train(X_train, y_train, alpha = lr, maxIter = iters, convergenceThreshold = threshold) #forward propagate to get a prediction from the network result = nn.forwardProp(X_train) #print some nice information print("\nUnfiltered Prediction:\n", result) print("Final Prediction:\n", result >= 0.5, '\n') print("Random init cost: ", round(J_Hist[0], 5), ", Final cost: ", round(J_Hist[-1], 5)) print("Cost reduction from random init: ", round(J_Hist[0] - J_Hist[-1], 5), '\n') #set up subplots for the cost history and decision boundary figure, plots = plt.subplots(ncols=2) figure.suptitle('Neural Network Learning of XOR') #supertitle figure.tight_layout(pad=2.5, w_pad=1.5, h_pad=0) #fix margins drawCostHistory(J_Hist, plots[0]) drawDecisionBoundary(nn, plots[1], seperation_coefficient = 50, square_size = 1, allowNegatives = False) #show the cool graphs :) plt.show()
def c_2(plot=False, units=[100], activation="sigmoid", eeta=0.1): print("\nNeural_Network MNIST") model = Neural_Network(len(mnist_trd[0]), units, activation=activation) print(model) model.train(mnist_trd, mnist_trl, max_iter=300, eeta=eeta, batch_size=100, decay=True, threshold=1e-3) pred = model.predict(mnist_trd) train_acc = accuracy_score(mnist_trl, pred) * 100 print("Train Set Accuracy: ", train_acc) pred = model.predict(mnist_ted) test_acc = accuracy_score(mnist_tel, pred) * 100 print("Test Set Accuracy: ", test_acc)
class Classifier: def __init__(self, classifier_type, **kwargs): """ Initializer. Classifier_type should be a string which refers to the specific algorithm the current classifier is using. Use keyword arguments to store parameters specific to the algorithm being used. E.g. if you were making a neural net with 30 input nodes, hidden layer with 10 units, and 3 output nodes your initalization might look something like this: neural_net = Classifier(weights = [], num_input=30, num_hidden=10, num_output=3) Here I have the weight matrices being stored in a list called weights (initially empty). """ self.classifier_type = classifier_type self.params = kwargs """ The kwargs you inputted just becomes a dictionary, so we can save that dictionary to be used in other methods. """ def train(self, training_data): """ Data should be nx(m+1) numpy matrix where n is the number of examples and m is the number of features (recall that the first element of the vector is the label). I recommend implementing the specific algorithms in a seperate module and then determining which method to call based on classifier_type. E.g. if you had a module called neural_nets: if self.classifier_type == 'neural_net': import neural_nets neural_nets.train_neural_net(self.params, training_data) Note that your training algorithms should be modifying the parameters so make sure that your methods are actually modifying self.params You should print the accuracy, precision, and recall on the training data. """ if self.classifier_type == 'neural_network': #change num_input, num_output based upon the data self.nn = Neural_Network("neural_network",weights = [], num_input=self.params['num_input'], num_hidden=1000, num_output=self.params['num_output'], alt_weight=self.params['one']=='1', momentum=self.params['two']=='1') self.nn.train(training_data) elif self.classifier_type == 'naive_bayes': self.nb = Naive_Bayes("naive_bayes") self.nb.train(training_data) elif self.classifier_type =='decision_tree': self.dt = Decision_Tree("decision_tree", pruning=self.params['one']=='1', info_gain_ratio=self.params['two']=='1') self.dt.train(training_data) def predict(self, data): """ Predict class of a single data vector Data should be 1x(m+1) numpy matrix where m is the number of features (recall that the first element of the vector is the label). I recommend implementing the specific algorithms in a seperate module and then determining which method to call based on classifier_type. This method should return the predicted label. """ def test(self, test_data): """ Data should be nx(m+1) numpy matrix where n is the number of examples and m is the number of features (recall that the first element of the vector is the label). You should print the accuracy, precision, and recall on the test data. """ #pdb.set_trace() #Accuracy, Recall, and Precision relevant_and_retrieved, relevant, retrieved, total, hit = 0, 0, 0, 0, 0 for person in test_data: predict = 0 if self.classifier_type == 'neural_network': predict = self.nn.predict(person) elif self.classifier_type == 'naive_bayes': predict = self.nb.predict(person) elif self.classifier_type == 'decision_tree': predict = self.dt.predict(person) if predict == person[0]: if predict == 0: relevant_and_retrieved += 1 hit += 1 if person[0] == 0: relevant += 1 if predict == 0: retrieved += 1 total += 1 accuracy = hit/float(total) recall = relevant_and_retrieved/float(relevant) precision = relevant_and_retrieved/float(retrieved) print "Accuracy: ", accuracy print "Precision ", precision print "Recall: " , recall
# fix random seed for reproducibility seed = 7 numpy.random.seed(seed) #writing a circle of pixels to the array according to middle coordinates of mouse position plotter=Plotter() NN=Neural_Network() if input("Wanna train model? y/N ") =='y': layers = [800,400] NN.initialize(layers) NN.train(20) if input("Wanna save model? Y/n ") !='n' : NN.save_model() elif input("Wanna load from file? Y/n ")!='n' : NN.load_model() else : print("Exit") exit() # Fit the model # Final evaluation of the model
from neural_network import Neural_Network import load_data as ld import pdb nb = Neural_Network("neural_network",weights = [], num_input=16, num_hidden=1000, num_output=2) #neural_net = Classifier(weights = [], num_input=30, num_hidden=10, num_output=3) data = ld.load_congress_data(.85) #data = ld.load_iris(.75) #data = ld.load_monks(3) classify = nb.train(data[0]) #nb.train(iris[0]) #pdb.set_trace() #nb.test(congress[1]) tot, hit = 0, 0 ones = 0 zeros = 0 twos = 0 for person in data[1]: predict = nb.predict(person) if predict == person[0]: hit += 1 tot += 1 if predict == 1: ones += 1 elif predict == 0: zeros += 1
from neural_network import Neural_Network from plotter import Plotter # fix random seed for reproducibility seed = 7 numpy.random.seed(seed) #writing a circle of pixels to the array according to middle coordinates of mouse position plotter = Plotter() NN = Neural_Network() if input("Wanna train model? y/N ") == 'y': layers = [800, 400] NN.initialize(layers) NN.train(10) if input("Wanna save model? Y/n ") != 'n': NN.save_model() elif input("Wanna load from file? Y/n ") != 'n': NN.load_model() else: print("Exit") exit() # Fit the model # Final evaluation of the model if input("Wanna test model? y/N ") == 'y': NN.test()