Beispiel #1
0
def learn(dkf,
          dataset,
          mask,
          epoch_start=0,
          epoch_end=1000,
          batch_size=200,
          shuffle=True,
          savefreq=None,
          savefile=None,
          dataset_eval=None,
          mask_eval=None,
          replicate_K=None,
          normalization='frame'):
    """
                                            Train DKF
    """
    assert not dkf.params['validate_only'], 'cannot learn in validate only mode'
    assert len(dataset.shape) == 3, 'Expecting 3D tensor for data'
    assert dataset.shape[2] == dkf.params[
        'dim_observations'], 'Dim observations not valid'
    N = dataset.shape[0]
    idxlist = range(N)
    batchlist = np.split(idxlist, range(batch_size, N, batch_size))

    bound_train_list,bound_valid_list,bound_tsbn_list,nll_valid_list = [],[],[],[]
    p_norm, g_norm, opt_norm = None, None, None

    #Lists used to track quantities for synthetic experiments
    mu_list_train, cov_list_train, mu_list_valid, cov_list_valid = [],[],[],[]
    model_params = {}

    #Start of training loop
    if 'synthetic' in dkf.params['dataset']:
        epfreq = 10
    else:
        epfreq = 1

    #Set data
    dkf.resetDataset(dataset, mask)
    for epoch in range(epoch_start, epoch_end):
        #Shuffle
        if shuffle:
            np.random.shuffle(idxlist)
            batchlist = np.split(idxlist, range(batch_size, N, batch_size))
        #Always shuffle order the batches are presented in
        np.random.shuffle(batchlist)

        start_time = time.time()
        bound = 0
        for bnum, batch_idx in enumerate(batchlist):
            batch_idx = batchlist[bnum]
            batch_bound, p_norm, g_norm, opt_norm, negCLL, KL, anneal = dkf.train_debug(
                idx=batch_idx)

            #Number of frames
            M_sum = mask[batch_idx].sum()
            #Correction for replicating batch
            if replicate_K is not None:
                batch_bound, negCLL, KL = batch_bound / replicate_K, negCLL / replicate_K, KL / replicate_K,
                M_sum = M_sum / replicate_K
            #Update bound
            bound += batch_bound
            ### Display ###
            if epoch % epfreq == 0 and bnum % 10 == 0:
                if normalization == 'frame':
                    bval = batch_bound / float(M_sum)
                elif normalization == 'sequence':
                    bval = batch_bound / float(X.shape[0])
                else:
                    assert False, 'Invalid normalization'
                dkf._p((
                    'Bnum: %d, Batch Bound: %.4f, |w|: %.4f, |dw|: %.4f, |w_opt|: %.4f'
                ) % (bnum, bval, p_norm, g_norm, opt_norm))
                dkf._p(('-veCLL:%.4f, KL:%.4f, anneal:%.4f') %
                       (negCLL, KL, anneal))
        if normalization == 'frame':
            bound /= (float(mask.sum()) / replicate_K)
        elif normalization == 'sequence':
            bound /= float(N)
        else:
            assert False, 'Invalid normalization'
        bound_train_list.append((epoch, bound))
        end_time = time.time()
        if epoch % epfreq == 0:
            dkf._p(('(Ep %d) Bound: %.4f [Took %.4f seconds] ') %
                   (epoch, bound, end_time - start_time))

        #Save at intermediate stages
        if savefreq is not None and epoch % savefreq == 0:
            assert savefile is not None, 'expecting savefile'
            dkf._p(('Saving at epoch %d' % epoch))
            dkf._saveModel(fname=savefile + '-EP' + str(epoch))
            intermediate = {}
            if dataset_eval is not None and mask_eval is not None:
                tmpMap = {}
                bound_valid_list.append(
                    (epoch,
                     DKF_evaluate.evaluateBound(dkf,
                                                dataset_eval,
                                                mask_eval,
                                                batch_size=batch_size,
                                                additional=tmpMap,
                                                normalization=normalization)))
                bound_tsbn_list.append((epoch, tmpMap['tsbn_bound']))
                nll_valid_list.append(
                    DKF_evaluate.impSamplingNLL(dkf,
                                                dataset_eval,
                                                mask_eval,
                                                batch_size,
                                                normalization=normalization))
            intermediate['valid_bound'] = np.array(bound_valid_list)
            intermediate['train_bound'] = np.array(bound_train_list)
            intermediate['tsbn_bound'] = np.array(bound_tsbn_list)
            intermediate['valid_nll'] = np.array(nll_valid_list)
            if 'synthetic' in dkf.params['dataset']:
                mu_train, cov_train, mu_valid, cov_valid, learned_params = _syntheticProc(
                    dkf, dataset, mask, dataset_eval, mask_eval)
                if dkf.params['dim_stochastic'] == 1:
                    mu_list_train.append(mu_train)
                    cov_list_train.append(cov_train)
                    mu_list_valid.append(mu_valid)
                    cov_list_valid.append(cov_valid)
                    intermediate['mu_posterior_train'] = np.concatenate(
                        mu_list_train, axis=2)
                    intermediate['cov_posterior_train'] = np.concatenate(
                        cov_list_train, axis=2)
                    intermediate['mu_posterior_valid'] = np.concatenate(
                        mu_list_valid, axis=2)
                    intermediate['cov_posterior_valid'] = np.concatenate(
                        cov_list_valid, axis=2)
                else:
                    mu_list_train.append(mu_train[None, :])
                    cov_list_train.append(cov_train[None, :])
                    mu_list_valid.append(mu_valid[None, :])
                    cov_list_valid.append(cov_valid[None, :])
                    intermediate['mu_posterior_train'] = np.concatenate(
                        mu_list_train, axis=0)
                    intermediate['cov_posterior_train'] = np.concatenate(
                        cov_list_train, axis=0)
                    intermediate['mu_posterior_valid'] = np.concatenate(
                        mu_list_valid, axis=0)
                    intermediate['cov_posterior_valid'] = np.concatenate(
                        cov_list_valid, axis=0)
                for k in dkf.params_synthetic[dkf.params['dataset']]['params']:
                    if k in model_params:
                        model_params[k].append(learned_params[k])
                    else:
                        model_params[k] = [learned_params[k]]
                for k in dkf.params_synthetic[dkf.params['dataset']]['params']:
                    intermediate[k + '_learned'] = np.array(
                        model_params[k]).squeeze()
            saveHDF5(savefile + '-EP' + str(epoch) + '-stats.h5', intermediate)
            ### Update X in the computational flow_graph to point to training data
            dkf.resetDataset(dataset, mask)
    #Final information to be collected
    retMap = {}
    retMap['train_bound'] = np.array(bound_train_list)
    retMap['valid_bound'] = np.array(bound_valid_list)
    retMap['tsbn_bound'] = np.array(bound_tsbn_list)
    retMap['valid_nll'] = np.array(nll_valid_list)
    if 'synthetic' in dkf.params['dataset']:
        if dkf.params['dim_stochastic'] == 1:
            retMap['mu_posterior_train'] = np.concatenate(mu_list_train,
                                                          axis=2)
            retMap['cov_posterior_train'] = np.concatenate(cov_list_train,
                                                           axis=2)
            retMap['mu_posterior_valid'] = np.concatenate(mu_list_valid,
                                                          axis=2)
            retMap['cov_posterior_valid'] = np.concatenate(cov_list_valid,
                                                           axis=2)
        else:
            retMap['mu_posterior_train'] = np.concatenate(mu_list_train,
                                                          axis=0)
            retMap['cov_posterior_train'] = np.concatenate(cov_list_train,
                                                           axis=0)
            retMap['mu_posterior_valid'] = np.concatenate(mu_list_valid,
                                                          axis=0)
            retMap['cov_posterior_valid'] = np.concatenate(cov_list_valid,
                                                           axis=0)
        for k in dkf.params_synthetic[dkf.params['dataset']]['params']:
            retMap[k + '_learned'] = np.array(model_params[k])
    return retMap
Beispiel #2
0
def learn(dmm,
          dataset,
          epoch_start=0,
          epoch_end=1000,
          batch_size=200,
          shuffle=True,
          savefreq=None,
          savefile=None,
          dataset_eval=None):
    """ Train a DMM using data
    dmm: DMM object
    dataset, dataset_eval: <dict> object with data
    epoch_start, epoch_end, batch_size, savefreq: <int> 
    savefile: <str> Savefile for intermediate results
    """
    assert not dmm.params[
        'validate_only'], 'cannot run function in validate only mode'
    N = dataset['tensor'].shape[0]
    idxlist = range(N)
    batchlist = np.split(idxlist, range(batch_size, N, batch_size))
    bound_train_list, bound_valid_list = [], []
    epfreq = 1
    dmm.resetDataset(dataset)
    for epoch in range(epoch_start, epoch_end):
        #Shuffle and reset batches
        if shuffle:
            np.random.shuffle(idxlist)
            batchlist = np.split(idxlist, range(batch_size, N, batch_size))
        start_time = time.time()
        bound = 0
        for bnum, batch_idx in enumerate(batchlist):
            batch_idx = batchlist[bnum]
            batch_bound, p_norm, g_norm, opt_norm, negCLL, KL, anneal = dmm.train_debug(
                idx=batch_idx)
            if np.any(
                    np.isnan(
                        np.array([
                            batch_bound, p_norm, g_norm, opt_norm, negCLL, KL,
                            anneal
                        ]))):
                print('Warning: you have encountered a NaN')
                import ipdb
                ipdb.set_trace()
            bound += batch_bound
            if epoch % epfreq == 0 and bnum % 10 == 0:
                bval = batch_bound / float(dataset['mask'][batch_idx].sum())
                dmm._p((
                    'Bnum: %d, Batch Bound: %.4f, |w|: %.4f, |dw|: %.4f, |w_opt|: %.4f'
                ) % (bnum, bval, p_norm, g_norm, opt_norm))
                dmm._p(('-veCLL:%.4f, KL:%.4f, anneal:%.4f') %
                       (negCLL, KL, anneal))
        bound /= float(dataset['mask'].sum())
        bound_train_list.append((epoch, bound))
        end_time = time.time()
        if epoch % epfreq == 0:
            dmm._p(('(Ep %d) Bound: %.4f [Took %.4f seconds] ') %
                   (epoch, bound, end_time - start_time))
        if savefreq is not None and epoch % savefreq == 0:
            assert savefile is not None, 'expecting savefile'
            dmm._p(('Saving at epoch %d' % epoch))
            dmm._saveModel(fname=savefile + '-EP' + str(epoch))
            intermediate = {}
            if dataset_eval is not None:
                tmpMap = {}
                bound_valid_list.append(
                    (epoch,
                     DMM_evaluate.evaluateBound(dmm,
                                                dataset_eval,
                                                batch_size=batch_size)))
            intermediate['valid_bound'] = np.array(bound_valid_list)
            intermediate['train_bound'] = np.array(bound_train_list)
            saveHDF5(savefile + '-EP' + str(epoch) + '-stats.h5', intermediate)
            dmm.resetDataset(dataset)
    retMap = {}
    retMap['train_bound'] = np.array(bound_train_list)
    retMap['valid_bound'] = np.array(bound_valid_list)
    return retMap
def learn(dkf, dataset, mask, epoch_start=0, epoch_end=1000, 
          batch_size=200, shuffle=False,
          savefreq=None, savefile = None, 
          dataset_eval = None, mask_eval = None, 
          replicate_K = None,
          normalization = 'frame'):
    """
                                            Train DKF
    """
    assert not dkf.params['validate_only'],'cannot learn in validate only mode'
    assert len(dataset.shape)==3,'Expecting 3D tensor for data'
    assert dataset.shape[2]==dkf.params['dim_observations'],'Dim observations not valid'
    N = dataset.shape[0]
    idxlist   = range(N)
    batchlist = np.split(idxlist, range(batch_size,N,batch_size))

    bound_train_list,bound_valid_list,bound_tsbn_list,nll_valid_list = [],[],[],[]
    p_norm, g_norm, opt_norm = None, None, None

    #Lists used to track quantities for synthetic experiments
    mu_list_train, cov_list_train, mu_list_valid, cov_list_valid = [],[],[],[]

    #Start of training loop
    for epoch in range(epoch_start, epoch_end):
        #Shuffle
        if shuffle:
            np.random.shuffle(idxlist)
            batchlist = np.split(idxlist, range(batch_size,N,batch_size))
        #Always shuffle order the batches are presented in
        np.random.shuffle(batchlist)

        start_time = time.time()
        bound = 0
        for bnum, batch_idx in enumerate(batchlist):
            batch_idx  = batchlist[bnum]
            X       = dataset[batch_idx,:,:].astype(config.floatX)
            M       = mask[batch_idx,:].astype(config.floatX)
            U       = None

            #Tack on 0's if the matrix size (optimization->theano doesnt have to redefine matrices)
            if X.shape[0]<batch_size:
                Nremaining = int(batch_size-X.shape[0])
                X   = np.concatenate([X,np.zeros((Nremaining,X.shape[1],
                                                  X.shape[2]))],axis=0).astype(config.floatX)
                M   = np.concatenate([M,np.zeros((Nremaining,X.shape[1]))],axis=0).astype(config.floatX)

            #Reduce the dimensionality of the tensors based on the maximum size of the mask
            maxT    = int(np.max(M.sum(1)))
            X       = X[:,:maxT,:]
            M       = M[:,:maxT]


            eps     = np.random.randn(X.shape[0],maxT,dkf.params['dim_stochastic']).astype(config.floatX)
            batch_bound, p_norm, g_norm, opt_norm, negCLL, KL, anneal = dkf.train_debug(X=X, M=M, eps=eps)

            #Number of frames
            M_sum = M.sum()
            #Correction for replicating batch
            if replicate_K is not None:
                batch_bound, negCLL, KL = batch_bound/replicate_K, negCLL/replicate_K, KL/replicate_K, 
                M_sum   = M_sum/replicate_K
            #Update bound
            bound  += batch_bound
            ### Display ###
            if bnum%10==0:
                if normalization=='frame':
                    bval = batch_bound/float(M_sum)
                elif normalization=='sequence':
                    bval = batch_bound/float(X.shape[0])
                else:
                    assert False,'Invalid normalization'
                dkf._p(('Bnum: %d, Batch Bound: %.4f, |w|: %.4f, |dw|: %.4f, |w_opt|: %.4f')%(bnum,bval,p_norm, g_norm, opt_norm)) 
                dkf._p(('-veCLL:%.4f, KL:%.4f, anneal:%.4f')%(negCLL, KL, anneal))
        if normalization=='frame':
            bound /= float(mask.sum())
        elif normalization=='sequence':
            bound /= float(N)
        else:
            assert False,'Invalid normalization'
        bound_train_list.append((epoch,bound))
        end_time   = time.time()
        dkf._p(('(Ep %d) Bound: %.4f [Took %.4f seconds] ')%(epoch, bound, end_time-start_time))
        
        #Save at intermediate stages
        if savefreq is not None and epoch%savefreq==0:
            assert savefile is not None, 'expecting savefile'
            dkf._p(('Saving at epoch %d'%epoch))
            dkf._saveModel(fname = savefile+'-EP'+str(epoch))

            intermediate = {}
            if dataset_eval is not None and mask_eval is not None:
                tmpMap = {}
                bound_valid_list.append(
                    (epoch, 
                     DKF_evaluate.evaluateBound(dkf, dataset_eval, mask_eval, batch_size=batch_size, 
                                              additional = tmpMap, normalization=normalization)))
                bound_tsbn_list.append((epoch, tmpMap['tsbn_bound']))
                nll_valid_list.append(
                    DKF_evaluate.impSamplingNLL(dkf, dataset_eval, mask_eval, batch_size,
                                                                  normalization=normalization))
            intermediate['valid_bound'] = np.array(bound_valid_list)
            intermediate['train_bound'] = np.array(bound_train_list)
            intermediate['tsbn_bound']  = np.array(bound_tsbn_list)
            intermediate['valid_nll']  = np.array(nll_valid_list)
            if 'synthetic' in dkf.params['dataset']:
                mu_train, cov_train, mu_valid, cov_valid = _syntheticProc(dkf, dataset, dataset_eval)
                mu_list_train.append(mu_train)
                cov_list_train.append(cov_train)
                mu_list_valid.append(mu_valid)
                cov_list_valid.append(cov_valid)
                intermediate['mu_posterior_train']  = np.concatenate(mu_list_train, axis=2)
                intermediate['cov_posterior_train'] = np.concatenate(cov_list_train, axis=2)
                intermediate['mu_posterior_valid']  = np.concatenate(mu_list_valid, axis=2)
                intermediate['cov_posterior_valid'] = np.concatenate(cov_list_valid, axis=2)
            saveHDF5(savefile+'-EP'+str(epoch)+'-stats.h5', intermediate)
    #Final information to be collected
    retMap = {}
    retMap['train_bound']   = np.array(bound_train_list)
    retMap['valid_bound']   = np.array(bound_valid_list)
    retMap['tsbn_bound']   = np.array(bound_tsbn_list)
    retMap['valid_nll']  = np.array(nll_valid_list)
    if 'synthetic' in dkf.params['dataset']:
        retMap['mu_posterior_train']  = np.concatenate(mu_list_train, axis=2)
        retMap['cov_posterior_train'] = np.concatenate(cov_list_train, axis=2)
        retMap['mu_posterior_valid']  = np.concatenate(mu_list_valid, axis=2)
        retMap['cov_posterior_valid'] = np.concatenate(cov_list_valid, axis=2)
    return retMap