def sampleModel(self, params=None, callback=None, mp=True): """Given parameters to run the Gibbs sampling with this does the sampling, and returns the resulting Model object. If params is not provided it uses the default. callback can be a function to report progress, and mp can be set to False if you don't want to make use of multiprocessing.""" if params==None: params = Params() state = State(self, params) if mp and params.runs>1 and hasattr(solvers,'gibbs_all_mp'): solvers.gibbs_all_mp(state, callback) else: solvers.gibbs_all(state, callback) return state.getModel()
def gibbs_doc(model, doc, params = None, callback = None): """Runs Gibbs iterations on a single document, by sampling with a prior constructed from each sample in the given Model. params applies to each sample, so should probably be much more limited than usual - the default if its undefined is to use 1 run and 1 sample and a burn in of only 500. Returns a DocModel with all the relevant samples in.""" # Initialisation stuff - handle params, create the state and the DocModel object, plus a reporter... if params==None: params = Params() params.runs = 1 params.samples = 1 params.burnIn = 500 state = State(doc, params) dm = DocModel() reporter = ProgReporter(params,callback,model.sampleCount()) # Iterate and run for each sample in the model... for sample in model.sampleList(): tempState = State(state) tempState.setGlobalParams(sample) tempState.addPrior(sample) gibbs_run(tempState,reporter.next) dm.addFrom(tempState.getModel()) # Return... return dm
def gibbs_doc(model, doc, params=None, callback=None): """Runs Gibbs iterations on a single document, by sampling with a prior constructed from each sample in the given Model. params applies to each sample, so should probably be much more limited than usual - the default if its undefined is to use 1 run and 1 sample and a burn in of only 500. Returns a DocModel with all the relevant samples in.""" # Initialisation stuff - handle params, create the state and the DocModel object, plus a reporter... if params == None: params = Params() params.runs = 1 params.samples = 1 params.burnIn = 500 state = State(doc, params) dm = DocModel() reporter = ProgReporter(params, callback, model.sampleCount()) # Iterate and run for each sample in the model... for sample in model.sampleList(): tempState = State(state) tempState.setGlobalParams(sample) tempState.addPrior(sample) gibbs_run(tempState, reporter.next) dm.addFrom(tempState.getModel()) # Return... return dm