def learnNextLayer(self, params): nLayers = len(self.ticas) print 'StackedTica currently has %d layers, learning next' % nLayers # TODO: only works for one extra layer! Nw = 10 Nwshift = 5 Nsamples = 50000 #Nsamples = 1000; print 'HACK!' # Get data and norm it nextLayerData = cached(makeNewData, self.ticas[-1], self.l1whitener, seed = 0, isColor = self.isColor, Nw = Nw, Nwshift = Nwshift, Nsamples = Nsamples) colNorms = sqrt(sum(nextLayerData**2, 0) + (1e-8)) nextLayerData = nextLayerData / colNorms # Parameters if params['dataCrop']: print '\nWARNING: Cropping data from %d examples to only %d for debug\n' % (nextLayerData.shape[1], params['dataCrop']) nextLayerData = nextLayerData[:,:params['dataCrop']] if params['hiddenISize'] != params['hiddenJSize']: raise Exception('hiddenISize and hiddenJSize must be the same') hiddenLayerShape = (params['hiddenISize'], params['hiddenJSize']) neighborhoodParams = ('gaussian', params['neighborhoodSize'], 0, 0) if self.saveDir: layerLogDir = os.path.join(self.saveDir, 'layer_%02d' % (nLayers+1)) os.makedirs(layerLogDir) else: layerLogDir = '' # Print/plot data stats if layerLogDir: pseudoImgShape = (int(sqrt(nextLayerData.shape[0])), int(sqrt(nextLayerData.shape[0]))) plotImageData(nextLayerData, pseudoImgShape, layerLogDir, pc('data_raw')) printDataStats(nextLayerData) # Learn model tica = TICA(nInputs = nextLayerData.shape[0], hiddenLayerShape = hiddenLayerShape, neighborhoodParams = neighborhoodParams, lambd = params['lambd'], epsilon = 1e-5, saveDir = layerLogDir) manuallySkipExpensivePart = False if not manuallySkipExpensivePart: beginTotalCost, beginPoolingCost, beginReconstructionCost, grad = tica.cost(tica.WW, nextLayerData) tic = time.time() tica.learn(nextLayerData, maxFun = params['maxFuncCalls']) execTime = time.time() - tic if layerLogDir: saveToFile(os.path.join(layerLogDir, 'tica.pkl.gz'), tica) # save learned model endTotalCost, endPoolingCost, endReconstructionCost, grad = tica.cost(tica.WW, nextLayerData) print 'beginTotalCost, beginPoolingCost, beginReconstructionCost, endTotalCost, endPoolingCost, endReconstructionCost, execTime =' print [beginTotalCost, beginPoolingCost, beginReconstructionCost, endTotalCost, endPoolingCost, endReconstructionCost, execTime] else: pdb.set_trace() # Plot some results #plotImageRicaWW(tica.WW, imgShape, saveDir, tileShape = hiddenLayerShape, prefix = pc('WW_iterFinal')) if layerLogDir: self.plotResults(layerLogDir, tica, nextLayerData, pseudoImgShape, hiddenLayerShape) self.ticas.append(tica)
def runTest(saveDir, params): import time, os, sys from numpy import * from GitResultsManager import GitResultsManager #raise Exception('path is %s' % sys.path) #raise Exception('version is %s' % sys.version_info) #raise Exception('cwd is %s' % os.getcwd()) from tica import TICA from util.misc import MakePc, Counter from visualize import plotImageData, plotCov, plotImageRicaWW, plotRicaActivations, plotRicaReconstructions from util.dataPrep import PCAWhiteningDataNormalizer, printDataStats from util.dataLoaders import loadAtariData, loadUpsonData, loadUpsonData3, loadRandomData, saveToFile #counter = Counter() #pc = lambda st : makePc(st, counter = counter) pc = MakePc(Counter()) ######################### # Parameters ######################### hiddenISize = params['hiddenISize'] hiddenJSize = params['hiddenJSize'] neighborhoodParams = ('gaussian', params['neighborhoodSize'], 0, 0) lambd = params['lambd'] epsilon = 1e-5 maxFuncCalls = params['maxFuncCalls'] randSeed = params['randSeed'] whiten = params['whiten'] dataCrop = params['dataCrop'] dataLoader = locals().get( params['dataLoader']) # Convert string to actual function dataPath = params['dataPath'] imgShape = params['imgShape'] hiddenLayerShape = (hiddenISize, hiddenJSize) ######################### # Data ######################### # Load data #data = loadAtariData('../data/atari/mspacman_train_15_50000_3c.pkl.gz'); imgShape = (15,15,3) #data = loadAtariData('../data/atari/space_invaders_train_15_50000_3c.pkl.gz'); imgShape = (15,15,3) loaded = dataLoader(dataPath) if type(loaded) is tuple: data, labels, labelStrings = loaded print 'Data has labels:', labelStrings else: data = loaded labels, labelStrings = None, None print 'Data does not have labels.' if dataCrop: print '\nWARNING: Cropping data from %d examples to only %d for debug\n' % ( data.shape[1], dataCrop) data = data[:, :dataCrop] nInputs = data.shape[0] isColor = len(imgShape) > 2 print '\nParameters:' for key in [ 'nInputs', 'hiddenISize', 'hiddenJSize', 'neighborhoodParams', 'lambd', 'epsilon', 'maxFuncCalls', 'randSeed', 'dataCrop', 'dataLoader', 'dataPath', 'imgShape', 'whiten' ]: print ' %20s: %s' % (key, locals()[key]) print skipVis = True if whiten: if not skipVis: # Visualize before prep plotImageData(data, imgShape, saveDir, pc('data_raw')) plotCov(data, saveDir, pc('data_raw')) printDataStats(data) # Whiten with PCA whiteningStage = PCAWhiteningDataNormalizer(data, saveDir=saveDir) dataWhite, junk = whiteningStage.raw2normalized(data, unitNorm=True) #dataOrig = whiteningStage.normalized2raw(dataWhite) dataOrig = data data = dataWhite if not skipVis: # Visualize after prep plotImageData(data, imgShape, saveDir, pc('data_white')) plotCov(data, saveDir, pc('data_white')) printDataStats(data) ######################### # Model ######################### random.seed(randSeed) tica = TICA(nInputs=prod(imgShape), hiddenLayerShape=hiddenLayerShape, neighborhoodParams=neighborhoodParams, lambd=lambd, epsilon=epsilon, saveDir=saveDir) beginTotalCost, beginPoolingCost, beginReconstructionCost, grad = tica.cost( tica.WW, data) tic = time.time() tica.learn(data, maxFun=maxFuncCalls) execTime = time.time() - tic saveToFile(os.path.join(saveDir, 'tica.pkl.gz'), tica) # save learned model plotImageRicaWW(tica.WW, imgShape, saveDir, tileShape=hiddenLayerShape, prefix=pc('WW_iterFinal')) plotRicaActivations(tica.WW, data, saveDir, prefix=pc('activations_iterFinal')) unwhitener = whiteningStage.normalized2raw if whiten else None plotRicaReconstructions(tica, data, imgShape, saveDir, unwhitener=unwhitener, tileShape=hiddenLayerShape, prefix=pc('recon_iterFinal'), number=20) endTotalCost, endPoolingCost, endReconstructionCost, grad = tica.cost( tica.WW, data) print 'beginTotalCost, beginPoolingCost, beginReconstructionCost, endTotalCost, endPoolingCost, endReconstructionCost, execTime =' print[ beginTotalCost, beginPoolingCost, beginReconstructionCost, endTotalCost, endPoolingCost, endReconstructionCost, execTime ] results = { 'beginTotalCost': beginTotalCost, 'beginPoolingCost': beginPoolingCost, 'beginReconstructionCost': beginReconstructionCost, 'endTotalCost': endTotalCost, 'endPoolingCost': endPoolingCost, 'endReconstructionCost': endReconstructionCost, 'execTime': execTime } # Save locally just in case of exception in main program myResults = {'params': params, 'results': results} saveToFile(os.path.join(saveDir, 'myresults.pkl.gz'), myResults) return results