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 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()