Example #1
0
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
Example #2
0
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