def train( self, trainingData, trainingLabels, validationData, validationLabels, showPlot=True, showPacmanPlot=True): """ Stochastic gradient descent to learn self.weights """ numDimensions = trainingData[0].size # Initializes weights to zero self.weights = np.zeros(numDimensions) if showPlot: # Initialize list to store loss per iteration for plotting later trainingLossPerIteration = [] # Initial loss trainingLoss = self.classificationLoss(trainingData, trainingLabels) trainingLossPerIteration.append(trainingLoss) # Check for offset term plotDims = numDimensions-1 for datum in trainingData: if datum[-1] != 1: plotDims += 1 break if showPacmanPlot and plotDims <=2: if plotDims == 2: pacmanDisplay = pacmanPlot.PacmanPlotClassification2D(); pacmanDisplay.plot(trainingData[:,:plotDims], trainingLabels) else: pacmanDisplay = pacmanPlot.PacmanPlotLogisticRegression1D(); pacmanDisplay.plot(trainingData[:,0], trainingLabels) graphicsUtils.sleep(0.1) # Stochastic gradient descent for itr in xrange(self.max_iterations): for (datum, label) in zip(trainingData, trainingLabels): self.weights = stochasticGradientDescentUpdate(datum, label, self.weights, self.alpha, self.der_loss_dw) if showPlot: predictions = self.classify(validationData) accuracyCount = [predictions[i] == validationLabels[i] for i in range(len(validationLabels))].count(True) print "Performance on validation set for iteration= %d: (%.1f%%)" % (itr, 100.0*accuracyCount/len(validationLabels)) trainingLoss = self.classificationLoss(trainingData, trainingLabels) trainingLossPerIteration.append(trainingLoss) if plotDims <= 2: if showPacmanPlot: pacmanDisplay.setWeights(self.weights) graphicsUtils.sleep(0.1) else: if plotDims == 2: plotUtil.plotClassification2D(trainingData[:,:plotDims],trainingLabels, self.weights, 1) else: plotUtil.plotLogisticRegression1D(trainingData[:,:plotDims],trainingLabels, self.weights, 1) plotUtil.plotCurve(range(len(trainingLossPerIteration)), trainingLossPerIteration, 2, "Training Loss") if showPlot and showPacmanPlot: graphicsUtils.end_graphics()
def trainGradient(self, trainingData, regressionData, numIterations, showPlot=True, showPacmanPlot=True): print 'Training with gradient ...' if showPlot: # Initialize list to store loss per iteration for plotting later trainingLossPerIteration = [] if showPacmanPlot: pacmanDisplay = pacmanPlot.PacmanPlotRegression() pacmanDisplay.plot(trainingData, regressionData) graphicsUtils.sleep(0.1) # Initializes weights to zero numDimensions = trainingData[0].size self.weights = np.zeros(numDimensions) # Stochastic gradient descent for i in xrange(numIterations): if i + 1 % 10 == 0: print "Iteration " + str(i + 1) + " of " + str(numIterations) for (datum, label) in zip(trainingData, regressionData): self.weights = stochasticGradientDescentUpdate( datum, label, self.weights, self.alpha, self.der_loss_dw) if showPlot: trainingLoss = self.regressionLoss(trainingData, regressionData) trainingLossPerIteration.append(trainingLoss) if showPacmanPlot: pacmanDisplay.setWeights(self.weights) graphicsUtils.sleep(0.05) else: plotUtil.plotRegression(trainingData, regressionData, self.weights, 1) plotUtil.plotCurve(range(len(trainingLossPerIteration)), trainingLossPerIteration, 2, "Training Loss") if showPlot and showPacmanPlot: graphicsUtils.end_graphics()
def trainGradient(self, trainingData, regressionData, numIterations, showPlot=True, showPacmanPlot=True): print 'Training with gradient ...' if showPlot: # Initialize list to store loss per iteration for plotting later trainingLossPerIteration = [] if showPacmanPlot: pacmanDisplay = pacmanPlot.PacmanPlotRegression(); pacmanDisplay.plot(trainingData, regressionData) graphicsUtils.sleep(0.1) # Initializes weights to zero numDimensions = trainingData[0].size self.weights = np.zeros(numDimensions) # Stochastic gradient descent for i in xrange(numIterations): if i+1 % 10 == 0: print "Iteration " + str(i+1) + " of "+ str(numIterations) for (datum, label) in zip(trainingData, regressionData): self.weights = stochasticGradientDescentUpdate(datum, label, self.weights, self.alpha, self.der_loss_dw) if showPlot: trainingLoss = self.regressionLoss(trainingData, regressionData) trainingLossPerIteration.append(trainingLoss) if showPacmanPlot: pacmanDisplay.setWeights(self.weights) graphicsUtils.sleep(0.05) else: plotUtil.plotRegression(trainingData,regressionData, self.weights, 1) plotUtil.plotCurve(range(len(trainingLossPerIteration)), trainingLossPerIteration, 2, "Training Loss") if showPlot and showPacmanPlot: graphicsUtils.end_graphics()
def runRegressor(args, options): regressor = args['regressor'] # Load data numIter = options.iterations if (options.data != "BerkeleyHousing"): print "Loading simple dataset: " + options.data + " ..." data = np.load(options.data + ".npz") regressor.setLearningRate(0.01) trainingData = data["data"] trainingRegressionResult = data["regressionResults"] validationData = data["dataVali"] validationRegressionResult = data["regressionResultsVali"] testingData = data["dataTest"] testingRegressionResult = data["regressionResultsTest"] paras = data["paras"] else: print "Loading Berkeley housing dataset ..." regressor.setLearningRate(0.00000001) data = np.load("BerkeleyHousing.npz") # dataTest = np.load("berkeleyHousingTest.npz") trainingData = data["data"] trainingRegressionResult = data["regressionResults"] validationData = [] validationRegressionResult = [] testingData = data["dataTest"] testingRegressionResult = data["regressionResults"] # Append 1 to all data points to allow for a bias offset (and convert to Nx2 matrix) trainingData = np.hstack( (trainingData[:, None], np.ones((trainingData.size, 1)))) if options.data != "BerkeleyHousing": validationData = np.hstack( (validationData[:, None], np.ones((validationData.size, 1)))) testingData = np.hstack( (testingData[:, None], np.ones((testingData.size, 1)))) # Conduct training and testing print "Training..." if (options.method == "analytical"): regressor.trainAnalytical(trainingData, trainingRegressionResult) else: regressor.trainGradient(trainingData, trainingRegressionResult, numIter, options.Print) if options.Print: if options.ghosts: pacmanDisplay = pacmanPlot.PacmanPlotRegression() pacmanDisplay.plot(trainingData, trainingRegressionResult, regressor.weights, title='Training: Linear Regression') graphicsUtils.sleep(3) else: plotUtil.plotRegression(trainingData, trainingRegressionResult, regressor.weights, 1, True, False, 'Training: Linear Regression') if len(validationData) > 0: print "Validating..." if options.Print: if options.ghosts: pacmanDisplay = pacmanPlot.PacmanPlotRegression() pacmanDisplay.plot(validationData, validationRegressionResult, regressor.weights, title='Validating: Linear Regression') graphicsUtils.sleep(3) else: plotUtil.plotRegression(validationData, validationRegressionResult, regressor.weights, 1, True, False, 'Validating: Linear Regression') validationLoss = regressor.regressionLoss(validationData, validationRegressionResult) print "Validation loss: " + str(validationLoss) else: print "No validation data provided" print "Testing..." if options.Print: if options.ghosts: pacmanDisplay = pacmanPlot.PacmanPlotRegression() pacmanDisplay.plot(testingData, testingRegressionResult, regressor.weights, title='Testing: Linear Regression') graphicsUtils.sleep(3) else: plotUtil.plotRegression(testingData, testingRegressionResult, regressor.weights, 1, True, False, 'Testing: Linear Regression') testingLoss = regressor.regressionLoss(testingData, testingRegressionResult) print "Testing loss: " + str(testingLoss) if options.Print: if options.ghosts: # pacmanDisplay.takeControl() graphicsUtils.end_graphics()
def finish(self): gU.end_graphics()
def finish(self): end_graphics()
def endGraphics(): graphicsUtils.end_graphics()
def runRegressor(args, options): regressor = args['regressor'] # Load data numIter = options.iterations if(options.data!="BerkeleyHousing"): print "Loading simple dataset: " + options.data + " ..." data = np.load(options.data + ".npz") regressor.setLearningRate(0.01) trainingData = data["data"] trainingRegressionResult = data["regressionResults"] validationData = data["dataVali"] validationRegressionResult = data["regressionResultsVali"] testingData = data["dataTest"] testingRegressionResult = data["regressionResultsTest"] paras = data["paras"] else: print "Loading Berkeley housing dataset ..." regressor.setLearningRate(0.00000001) data = np.load("BerkeleyHousing.npz") # dataTest = np.load("berkeleyHousingTest.npz") trainingData = data["data"] trainingRegressionResult = data["regressionResults"] validationData = [] validationRegressionResult = [] testingData = data["dataTest"] testingRegressionResult = data["regressionResults"] # Append 1 to all data points to allow for a bias offset (and convert to Nx2 matrix) trainingData = np.hstack((trainingData[:,None], np.ones( (trainingData.size,1) ))) if options.data!="BerkeleyHousing": validationData = np.hstack((validationData[:,None], np.ones( (validationData.size,1) ))) testingData = np.hstack((testingData[:,None], np.ones( (testingData.size,1) ))) # Conduct training and testing print "Training..." if(options.method == "analytical"): regressor.trainAnalytical(trainingData, trainingRegressionResult) else: regressor.trainGradient(trainingData, trainingRegressionResult, numIter, options.Print) if options.Print: if options.ghosts: pacmanDisplay = pacmanPlot.PacmanPlotRegression(); pacmanDisplay.plot(trainingData, trainingRegressionResult, regressor.weights, title='Training: Linear Regression') graphicsUtils.sleep(3) else: plotUtil.plotRegression(trainingData,trainingRegressionResult, regressor.weights,1,True,False,'Training: Linear Regression') if len(validationData) > 0: print "Validating..." if options.Print: if options.ghosts: pacmanDisplay = pacmanPlot.PacmanPlotRegression(); pacmanDisplay.plot(validationData, validationRegressionResult, regressor.weights, title='Validating: Linear Regression') graphicsUtils.sleep(3) else: plotUtil.plotRegression(validationData,validationRegressionResult, regressor.weights,1,True,False, 'Validating: Linear Regression') validationLoss = regressor.regressionLoss(validationData, validationRegressionResult) print "Validation loss: " + str(validationLoss) else: print "No validation data provided" print "Testing..." if options.Print: if options.ghosts: pacmanDisplay = pacmanPlot.PacmanPlotRegression(); pacmanDisplay.plot(testingData, testingRegressionResult, regressor.weights, title='Testing: Linear Regression') graphicsUtils.sleep(3) else: plotUtil.plotRegression(testingData, testingRegressionResult, regressor.weights,1,True,False, 'Testing: Linear Regression') testingLoss = regressor.regressionLoss(testingData, testingRegressionResult) print "Testing loss: " + str(testingLoss) if options.Print: if options.ghosts: # pacmanDisplay.takeControl() graphicsUtils.end_graphics()