Beispiel #1
0
import numpy as np
from sam.math_util import avk, deriv_avk
from sam.optimize import check_grad

from sam.corpus.corpus import CorpusReader
from sam.vem.model import VEMModel

import sam.log as log

CORPUS_FILENAME = r"./testdata/20news-all.h5"

reader = CorpusReader(CORPUS_FILENAME, data_series='sam')
model = VEMModel(reader)

while True:
    model.run_one_iteration()
    #model.write_topics()
    model.write_parameters()


def check_grads(model):
    assert np.isfinite(model.l_alpha())
    assert np.isfinite(model.l_valpha())

    x = model.grad_l_vmu()
    assert np.isfinite(x).all()

    import pdb
    try:
        # Main update rules
        log.info('xi update:',
Beispiel #2
0
    def run(self, options):
        if os.path.exists(options.model):
            log.info('Loading model snapshot from %s' % options.model)
            model = VEMModel.load(options.model)
        else:
            # Initialize a model from scratch
            log.info('Initializing new model on %s [T=%d]' % (options.corpus, options.T))
            reader = CorpusReader(options.corpus, data_series='sam')
            model = VEMModel(reader=reader, T=options.T)

        while model.iteration < options.iterations:
            log.info('** Iteration %d / %d **' % (model.iteration + 1, options.iterations))
            model.run_one_iteration()

            if model.iteration % SAVE_MODEL_INTERVAL == 0:
                log.info('Saving model snapshot...')
                model.save(options.model)

            if model.iteration % SAVE_TOPICS_INTERVAL == 0:
                if options.write_topics:
                    log.info('Saving topics to %s' % options.write_topics)
                    with open(options.write_topics, 'w') as f:
                        model.write_topics(f)

                if options.write_topic_weights:
                    log.info('Saving topic weights to %s' % options.write_topic_weights)
                    with open(options.write_topic_weights, 'w') as f:
                        model.write_topic_weights_arff(f)

        if options.write_topics:
            log.info('Saving topics to %s' % options.write_topics)
            with open(options.write_topics, 'w') as f:
                model.write_topics(f)

        if options.write_topic_weights:
            log.info('Saving topic weights to %s' % options.write_topic_weights)
            with open(options.write_topic_weights, 'w') as f:
                model.write_topic_weights_arff(f)
        model.save(options.model)
Beispiel #3
0
import numpy as np
from sam.math_util import avk, deriv_avk
from sam.optimize import check_grad

from sam.corpus.corpus import CorpusReader
from sam.vem.model import VEMModel

import sam.log as log

CORPUS_FILENAME = 'nips-425D.h5'

reader = CorpusReader(CORPUS_FILENAME, data_series='sam')
model = VEMModel(reader)

while True:
    model.run_one_iteration()
    model.print_topics()



def check_grads(model):
    assert np.isfinite(model.l_alpha())
    assert np.isfinite(model.l_valpha())

    x = model.grad_l_vmu()
    assert np.isfinite(x).all()

    import pdb
    try:
        # Main update rules
        log.info('xi update:', check_grad(model, 'xi', model.l_xi, model.grad_l_xi))
Beispiel #4
0
    def run(self, options):
        if os.path.exists(options.model):
            log.info('Loading model snapshot from %s' % options.model)
            model = VEMModel.load(options.model)
        else:
            # Initialize a model from scratch
            log.info('Initializing new model on %s [T=%d]' %
                     (options.corpus, options.T))
            reader = CorpusReader(options.corpus, data_series='sam')
            model = VEMModel(reader=reader, T=options.T)

        while model.iteration < options.iterations:
            log.info('** Iteration %d / %d **' %
                     (model.iteration + 1, options.iterations))
            model.run_one_iteration()

            if model.iteration % SAVE_MODEL_INTERVAL == 0:
                log.info('Saving model snapshot...')
                model.save(options.model)

            if model.iteration % SAVE_TOPICS_INTERVAL == 0:
                if options.write_topics:
                    log.info('Saving topics to %s' % options.write_topics)
                    with open(options.write_topics, 'w') as f:
                        model.write_topics(f)

                if options.write_topic_weights:
                    log.info('Saving topic weights to %s' %
                             options.write_topic_weights)
                    with open(options.write_topic_weights, 'w') as f:
                        model.write_topic_weights_arff(f)

        if options.write_topics:
            log.info('Saving topics to %s' % options.write_topics)
            with open(options.write_topics, 'w') as f:
                model.write_topics(f)

        if options.write_topic_weights:
            log.info('Saving topic weights to %s' %
                     options.write_topic_weights)
            with open(options.write_topic_weights, 'w') as f:
                model.write_topic_weights_arff(f)
        model.save(options.model)