def train(self): print "[INFO] Training .." grad_history = np.zeros(self.num_parameters) theta = self.theta_init # Loop over epochs for epochid in range(self.max_epochs): # create a shuffled copy of the data X_shuffled = random.sample(self.X_train, self.num_data) # reset grad history per each epoch grad_history = np.zeros(self.num_parameters) # Loop over batches for batch_id in range(self.num_batches): start_i = batch_id * self.batch_size end_i = (batch_id + 1) * self.batch_size if end_i + self.batch_size > self.num_data: end_i = self.num_data X_batch = X_shuffled[start_i:end_i] theta, grad_history = self.trainOneBatch( theta, X_batch, grad_history, batch_id) print "Finished epoch %d." % epochid # Save the model at every 5 epochs if epochid % self.epoch_save_freq == 0: filename = "optResult-RNTN-" + \ time.strftime("%Y%m%d-%H%M%S") + "-epoch-" + str(epochid) with open(filename, 'wb') as output: pickle.dump(theta, output, -1) # Evaluate on train, test set testObj_train = Test(self.dictionary, self.X_train) tree_accuracy_train, root_accuracy_train = testObj_train.test( theta) print "[Train accuracy] tree: %.2f, root: %.2f" %\ (tree_accuracy_train, root_accuracy_train) # Test on test data testObj_test = Test(self.dictionary, self.X_test) tree_accuracy_test, root_accuracy_test = testObj_test.test(theta) print "[Test accuracy] tree: %.2f, root: %.2f" %\ (tree_accuracy_test, root_accuracy_test) sys.stdout.flush() return theta
def train(self): print "[INFO] Training .." grad_history = np.zeros(self.num_parameters) theta = self.theta_init # Loop over epochs for epochid in range(self.max_epochs): # create a shuffled copy of the data X_shuffled = random.sample(self.X_train, self.num_data) # reset grad history per each epoch grad_history = np.zeros(self.num_parameters) # Loop over batches for batch_id in range(self.num_batches): start_i = batch_id * self.batch_size end_i = (batch_id+1) * self.batch_size if end_i + self.batch_size > self.num_data: end_i = self.num_data X_batch = X_shuffled[start_i:end_i] theta, grad_history = self.trainOneBatch( theta, X_batch, grad_history, batch_id) print "Finished epoch %d." % epochid # Save the model at every 5 epochs if epochid % self.epoch_save_freq == 0: filename = "optResult-RNTN-" + \ time.strftime("%Y%m%d-%H%M%S") + "-epoch-" + str(epochid) with open(filename, 'wb') as output: pickle.dump(theta, output, -1) # Evaluate on train, test set testObj_train = Test(self.dictionary, self.X_train) tree_accuracy_train, root_accuracy_train = testObj_train.test(theta) print "[Train accuracy] tree: %.2f, root: %.2f" %\ (tree_accuracy_train, root_accuracy_train) # Test on test data testObj_test = Test(self.dictionary, self.X_test) tree_accuracy_test, root_accuracy_test = testObj_test.test(theta) print "[Test accuracy] tree: %.2f, root: %.2f" %\ (tree_accuracy_test, root_accuracy_test) sys.stdout.flush() return theta
from Test import Test from Train import Train from tfHelper import tfHelper import model import os te = Test() tr = Train() if os.path.exists("model.h5"): model = tfHelper.load_model("model") else: model = model.model() while True: te.test(model) model = tr.train(model)
# Load subset of the data and check gradient if args.checkGradient: trees_debug = readPTB.parser(args.debugTreePath) dictionary_debug = utils.constructCompactDictionary(trees_debug) normdiff = utils.checkGradient_MiniBatch(dictionary_debug, trees_debug) # Train and test every 5 or so epochs trainObj = Train_LBFGS(dictionary, trees_train) optResult = trainObj.train() # Save final model with a different name savefilename = "optResult-RNTN-LBFGS-Final-" + time.strftime( "%Y%m%d-%H%M%S") with open(savefilename, 'wb') as output: pickle.dump(optResult, output, -1) # Test on train data theta_opt = optResult.x testObj_train = Test(dictionary, trees_train) tree_accuracy_train, root_accuracy_train = testObj_train.test(theta_opt) print "[Train accuracy] tree: %.2f, root: %.2f" %\ (tree_accuracy_train, root_accuracy_train) # Test on test data testObj_test = Test(dictionary, trees_test) tree_accuracy_test, root_accuracy_test = testObj_test.test(theta_opt) print "[Test accuracy] tree: %.2f, root: %.2f" %\ (tree_accuracy_test, root_accuracy_test)
from Test import Test if __name__ == '__main__': check = Test() try: score = check.test() print score except Exception: print 125 else: pass
class Classifier: def __init__(self,learningRate, epochs, imageSize,numBiasNodes,numOuputNodes): self.learningRate = float(learningRate) self.epochs = int(epochs) self.imageSize = int(imageSize) self.numBiasNodes = int(numBiasNodes) self.numOuputNodes = int(numOuputNodes) self.Trainer = [] self.Tester = [] self.testResults = [] self.runTimes = [] """Main method that runs the Perceptron algorithm. Initializes a Perceptron, trains the Perceptron, and tests it against known dataset.""" def run(self): TrainImages,TrainAnswers,TestImages,TestAnswers = self.getImageSets() self.Trainer = Train(TrainImages,TrainAnswers,self.learningRate) self.Tester = Test(TestImages,TestAnswers) #build defulat perceptron Percep = Perceptron(self.imageSize**2, self.numBiasNodes,self.numOuputNodes,self.learningRate) Percep.init() i = 0 self.testResults = [] self.runTimes = [] startTime = time.process_time() while i < self.epochs: Percep,TestResults = self.epoch(Percep) self.testResults += [TestResults] currentTime = time.process_time() - startTime self.runTimes += [currentTime] i += 1 return self.testResults,self.runTimes """Helper method that that runs a single training epoch""" def epoch(self,perceptron): TrainedPerceptron = self.Trainer.train(perceptron) TestResults = self.Tester.test(TrainedPerceptron) return TrainedPerceptron,TestResults def getImageSets(self): trainFile = "files/train" + str(imageSize) + ".txt" testFile = "files/test" + str(imageSize) + ".txt" TrainReader = ImageReader(trainFile,self.imageSize) TestReader = ImageReader(testFile,self.imageSize) #only reads images if it hasn't been called before TrainReader.readImages() TestReader.readImages() TrainImages,TrainAnswers = TrainReader.getImages() TestImages,TestAnswers = TestReader.getImages() return TrainImages,TrainAnswers,TestImages,TestAnswers def graphResults(self): #make test results percents testResults = [100*result for result in self.testResults] plt.plot(self.runTimes,testResults) plt.title("Run Time (s) vs. % Correct") plt.xlabel("Run Time (s)") plt.ylabel("% Correct") plt.show() plt.cla() epochs = np.arange(1,self.epochs + 1) ax = plt.figure().gca() ax.xaxis.set_major_locator(MaxNLocator(integer=True)) plt.plot(epochs,testResults) plt.title("Epochs vs. % Correct") plt.xlabel("Epoch") plt.ylabel("% Correct") plt.show()
print '[Read] built dictionary of size %d' % (len(dictionary)) # Load subset of the data and check gradient if args.checkGradient: trees_debug = readPTB.parser(args.debugTreePath) dictionary_debug = utils.constructCompactDictionary(trees_debug) normdiff = utils.checkGradient_MiniBatch(dictionary_debug, trees_debug) # Train and test every 5 or so epochs trainObj = Train_LBFGS(dictionary, trees_train) optResult = trainObj.train() # Save final model with a different name savefilename = "optResult-RNTN-LBFGS-Final-" + time.strftime("%Y%m%d-%H%M%S") with open(savefilename, 'wb') as output: pickle.dump(optResult, output, -1) # Test on train data theta_opt = optResult.x testObj_train = Test(dictionary, trees_train) tree_accuracy_train, root_accuracy_train = testObj_train.test(theta_opt) print "[Train accuracy] tree: %.2f, root: %.2f" %\ (tree_accuracy_train, root_accuracy_train) # Test on test data testObj_test = Test(dictionary, trees_test) tree_accuracy_test, root_accuracy_test = testObj_test.test(theta_opt) print "[Test accuracy] tree: %.2f, root: %.2f" %\ (tree_accuracy_test, root_accuracy_test)