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)
class TicaLayer(TrainableLayer):

    nSublayers = 2   # hidden representation + pooled representation

    def __init__(self, params):
        super(TicaLayer, self).__init__(params)

        self.hiddenSize = params['hiddenSize']
        self.neighborhood = params['neighborhood']
        self.lambd = params['lambd']
        self.epsilon = params['epsilon']
        self.tica = None

        assert isinstance(self.hiddenSize, tuple)
        assert len(self.neighborhood) == 4
        assert self.neighborhood[3] == 0   # shrink not supported yet (changes output size)

    def _calculateOutputSize(self, inputSize):
        return self.hiddenSize

    def _initialize(self, trainParams = None, seed = None):
        # Set up untrained TICA
        self.tica = TICA(nInputs            = prod(self.inputSize),
                         hiddenLayerShape   = self.hiddenSize,
                         neighborhoodParams = self.neighborhood,
                         lambd              = self.lambd,
                         epsilon            = self.epsilon,
                         initWW             = False)
        self.tica.initWW(seed)

    def _train(self, data, dataArrangement, trainParams, quick = False):
        maxFuncCalls = trainParams['maxFuncCalls']
        if quick:
            print 'QUICK MODE: chopping maxFuncCalls from %d to 1!' % maxFuncCalls
            maxFuncCalls = 1
            

        tic = time.time()
        self.tica.learn(data, maxFun = maxFuncCalls)
        execTime = time.time() - tic
        #if logDir:
        #    saveToFile(os.path.join(logDir, (prefix if prefix else '') + 'tica.pkl.gz'), tica)    # save learned model

        beginTotalCost, beginPoolingCost, beginReconstructionCost = self.tica.costLog[0]
        endTotalCost,   endPoolingCost,   endReconstructionCost   = self.tica.costLog[-1]

        print 'Training stats:'
        print '  begin {totalCost, poolingCost, reconCost} = %f, %f, %f' % (beginTotalCost, beginPoolingCost, beginReconstructionCost)
        print '    end {totalCost, poolingCost, reconCost} = %f, %f, %f' % (endTotalCost, endPoolingCost, endReconstructionCost)
        print '  Number of cost evals:', len(self.tica.costLog)
        print '  Training time (wall)', execTime

        # Plot some results
        #plotImageRicaWW(tica.WW, imgShape, saveDir, tileShape = hiddenLayerShape, prefix = pc('WW_iterFinal'))

    @noHint
    def _forwardProp(self, data, dataArrangement, sublayer):
        hidden, absPooledActivations = self.tica.getRepresentation(data)
        
        if sublayer == 0:
            return hidden, dataArrangement
        elif sublayer == 1:
            return absPooledActivations, dataArrangement
        else:
            raise Exception('Unknown sublayer: %s' % sublayer)
    
    def _plot(self, data, dataArrangement, saveDir = None, prefix = None):
        '''Default no-op version. Override in derived class. It is up to the layer
        what to plot.
        '''
        if saveDir:
            self.tica.plotCostLog(saveDir, prefix)
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
    plotCov(dataWhite, saveDir, pc('data_white'))
    printDataStats(dataWhite)


    #########################
    # Model
    #########################

    random.seed(randSeed)
    tica = TICA(nInputs            = prod(imgShape),
                hiddenLayerShape   = hiddenLayerShape,
                neighborhoodParams = neighborhoodParams,
                lambd              = lambd,
                epsilon            = epsilon,
                saveDir            = saveDir)

    plotImageRicaWW(tica.WW, imgShape, saveDir, tileShape = hiddenLayerShape, prefix = pc('WW_iter0'))
    plotRicaActivations(tica.WW, dataWhite, saveDir, prefix = pc('activations_iter0'))
    plotRicaReconstructions(tica, dataWhite, imgShape, saveDir, unwhitener = whiteningStage.normalized2raw,
                            tileShape = hiddenLayerShape, prefix = pc('recon_iter0'))
    
    tica.learn(dataWhite, maxFun = maxFuncCalls)
    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, dataWhite, saveDir, prefix = pc('activations_iterFinal'))
    plotRicaReconstructions(tica, dataWhite, imgShape, saveDir, unwhitener = whiteningStage.normalized2raw,
                            tileShape = hiddenLayerShape, prefix = pc('recon_iterFinal'))
    
    resman.stop()
Beispiel #5
0
class TicaLayer(TrainableLayer):

    nSublayers = 2  # hidden representation + pooled representation

    def __init__(self, params):
        super(TicaLayer, self).__init__(params)

        self.hiddenSize = params['hiddenSize']
        self.neighborhood = params['neighborhood']
        self.lambd = params['lambd']
        self.epsilon = params['epsilon']
        self.tica = None

        assert isinstance(self.hiddenSize, tuple)
        assert len(self.neighborhood) == 4
        assert self.neighborhood[
            3] == 0  # shrink not supported yet (changes output size)

    def _calculateOutputSize(self, inputSize):
        return self.hiddenSize

    def _initialize(self, trainParams=None, seed=None):
        # Set up untrained TICA
        self.tica = TICA(nInputs=prod(self.inputSize),
                         hiddenLayerShape=self.hiddenSize,
                         neighborhoodParams=self.neighborhood,
                         lambd=self.lambd,
                         epsilon=self.epsilon,
                         initWW=False)
        self.tica.initWW(seed)

    def _train(self, data, dataArrangement, trainParams, quick=False):
        maxFuncCalls = trainParams['maxFuncCalls']
        if quick:
            print 'QUICK MODE: chopping maxFuncCalls from %d to 1!' % maxFuncCalls
            maxFuncCalls = 1

        tic = time.time()
        self.tica.learn(data, maxFun=maxFuncCalls)
        execTime = time.time() - tic
        #if logDir:
        #    saveToFile(os.path.join(logDir, (prefix if prefix else '') + 'tica.pkl.gz'), tica)    # save learned model

        beginTotalCost, beginPoolingCost, beginReconstructionCost = self.tica.costLog[
            0]
        endTotalCost, endPoolingCost, endReconstructionCost = self.tica.costLog[
            -1]

        print 'Training stats:'
        print '  begin {totalCost, poolingCost, reconCost} = %f, %f, %f' % (
            beginTotalCost, beginPoolingCost, beginReconstructionCost)
        print '    end {totalCost, poolingCost, reconCost} = %f, %f, %f' % (
            endTotalCost, endPoolingCost, endReconstructionCost)
        print '  Number of cost evals:', len(self.tica.costLog)
        print '  Training time (wall)', execTime

        # Plot some results
        #plotImageRicaWW(tica.WW, imgShape, saveDir, tileShape = hiddenLayerShape, prefix = pc('WW_iterFinal'))

    @noHint
    def _forwardProp(self, data, dataArrangement, sublayer):
        hidden, absPooledActivations = self.tica.getRepresentation(data)

        if sublayer == 0:
            return hidden, dataArrangement
        elif sublayer == 1:
            return absPooledActivations, dataArrangement
        else:
            raise Exception('Unknown sublayer: %s' % sublayer)

    def _plot(self, data, dataArrangement, saveDir=None, prefix=None):
        '''Default no-op version. Override in derived class. It is up to the layer
        what to plot.
        '''
        if saveDir:
            self.tica.plotCostLog(saveDir, prefix)