示例#1
0
def run(model, data, parallel=True):

    num_subparts = data["data"].shape[
        0]  # mmm the first dimension of data represents each subpart?? interesting.
    num_resources = len(model["learns"])

    result = E_step.run(data, model, 1, int(parallel))
    for j in range(num_resources):
        result['all_trans_softcounts'][j] = result['all_trans_softcounts'][
            j].transpose()
    for j in range(num_subparts):
        result['all_emission_softcounts'][j] = result[
            'all_emission_softcounts'][j].transpose()

    state_predictions = predict_onestep_states.run(data, model,
                                                   result['alpha'],
                                                   int(parallel))
    p = state_predictions.shape
    state_predictions = state_predictions.flatten(order='C').reshape(p,
                                                                     order='F')
    # multiguess solution, should work
    correct_emission_predictions = np.expand_dims(
        model["guesses"], axis=1) @ np.expand_dims(
            state_predictions[0, :], axis=0) + np.expand_dims(
                1 - model["slips"], axis=1) @ np.expand_dims(
                    state_predictions[1, :], axis=0)
    #correct_emission_predictions = model['guesses'] * np.asarray([state_predictions[0,:]]).T + (1 - model['slips']) * np.asarray([state_predictions[1,:]]).T
    flattened_predictions = np.take_along_axis(
        correct_emission_predictions,
        (data['data'] != 0).argmax(axis=0)[:, None].T,
        axis=0)
    return (flattened_predictions.ravel(), state_predictions)
示例#2
0
def EM_fit(model, data, tol=None, maxiter=None, parallel=True):

    if tol is None:
        tol = 1e-3
    if maxiter is None:
        maxiter = 100

    num_subparts = data["data"].shape[
        0]  #mmm the first dimension of data represents each subpart?? interesting.
    num_resources = len(model["learns"])
    log_likelihoods = np.zeros((maxiter, 1))

    for i in range(maxiter):
        result = E_step.run(data, model, 1, int(parallel))
        for j in range(num_resources):
            result['all_trans_softcounts'][j] = result['all_trans_softcounts'][
                j].transpose()
        for j in range(num_subparts):
            result['all_emission_softcounts'][j] = result[
                'all_emission_softcounts'][j].transpose()

        log_likelihoods[i][0] = result['total_loglike']

        if (i > 1 and
                abs(log_likelihoods[i][0] - log_likelihoods[i - 1][0]) < tol):
            break

        model = M_step.run(model, result['all_trans_softcounts'],
                           result['all_emission_softcounts'],
                           result['all_initial_softcounts'])

    return (model, log_likelihoods[:i + 1])
示例#3
0
def run(model, data):

    num_subparts = data["data"].shape[
        0]  # mmm the first dimension of data represents each subpart?? interesting.
    num_resources = len(model["learns"])

    trans_softcounts = np.zeros((num_resources, 2, 2))
    emission_softcounts = np.zeros((num_subparts, 2, 2))
    init_softcounts = np.zeros((2, 1))

    result = {}
    result['all_trans_softcounts'] = trans_softcounts
    result['all_emission_softcounts'] = emission_softcounts
    result['all_initial_softcounts'] = init_softcounts

    result = E_step.run(data, model, result['all_trans_softcounts'],
                        result['all_emission_softcounts'],
                        result['all_initial_softcounts'], 1)
    for j in range(num_resources):
        result['all_trans_softcounts'][j] = result['all_trans_softcounts'][
            j].transpose()
    for j in range(num_subparts):
        result['all_emission_softcounts'][j] = result[
            'all_emission_softcounts'][j].transpose()

    state_predictions = predict_onestep_states.run(data, model,
                                                   result['alpha'])

    correct_emission_predictions = []
    for i in range(len(model["guesses"])):
        correct_emission_predictions.append(
            model["guesses"][i] * state_predictions[0, :] +
            (1 - model["slips"][i]) * state_predictions[1, :])

    return (correct_emission_predictions, state_predictions)
示例#4
0
def run(model, data):

    num_subparts = data["data"].shape[0]  # mmm the first dimension of data represents each subpart?? interesting.
    num_resources = len(model["learns"])

    trans_softcounts = np.zeros((num_resources, 2, 2))
    emission_softcounts = np.zeros((num_subparts, 2, 2))
    init_softcounts = np.zeros((2, 1))

    result = {}
    result['all_trans_softcounts'] = trans_softcounts
    result['all_emission_softcounts'] = emission_softcounts
    result['all_initial_softcounts'] = init_softcounts

    result = E_step.run(data, model, result['all_trans_softcounts'], result['all_emission_softcounts'], result['all_initial_softcounts'], 1)
    for j in range(num_resources):
        result['all_trans_softcounts'][j] = result['all_trans_softcounts'][j].transpose()
    for j in range(num_subparts):
        result['all_emission_softcounts'][j] = result['all_emission_softcounts'][j].transpose()

    state_predictions = predict_onestep_states.run(data, model, result['alpha'])
    p = state_predictions.shape
    state_predictions = state_predictions.flatten(order = 'C').reshape(p, order = 'F')
    # multiguess solution, should work
    correct_emission_predictions = np.expand_dims(model["guesses"], axis = 1) @ np.expand_dims(state_predictions[0,:], axis = 0) + np.expand_dims(1-model["slips"], axis = 1) @ np.expand_dims(state_predictions[1,:], axis = 0)
    #correct_emission_predictions = model['guesses'] * np.asarray([state_predictions[0,:]]).T + (1 - model['slips']) * np.asarray([state_predictions[1,:]]).T
    flattened_predictions = np.zeros((len(correct_emission_predictions[0]),))
    for i in range(len(correct_emission_predictions)):
        for j in range(len(correct_emission_predictions[0])):
            if data["data"][i][j] != 0:
                flattened_predictions[j] = correct_emission_predictions[i][j]
    return (flattened_predictions, state_predictions)
示例#5
0
文件: EM_fit.py 项目: bikong2/pyBKT
def EM_fit(model, data, tol = None, maxiter = None):

    if tol is None: tol = 1e-3
    if maxiter is None: maxiter = 100

    check_data.check_data(data)

    #print(data["data"])
    #print(data["data"].shape)
    num_subparts = data["data"].shape[0] #mmm the first dimension of data represents each subpart?? interesting.
    num_resources = len(model["learns"])

    trans_softcounts = np.zeros((num_resources,2,2))
    emission_softcounts = np.zeros((num_subparts,2,2))
    init_softcounts = np.zeros((2, 1))
    log_likelihoods = np.zeros((maxiter, 1))

    result = {}
    result['all_trans_softcounts'] = trans_softcounts
    result['all_emission_softcounts'] = emission_softcounts
    result['all_initial_softcounts'] = init_softcounts
    #print(result)
    
    #data["data"] = np.asarray(data["data"], dtype='int32')
    #print(data)

    for i in range(maxiter):
        result = E_step.run(data, model, result['all_trans_softcounts'], result['all_emission_softcounts'], result['all_initial_softcounts'], 1)
        for j in range(num_resources):
            result['all_trans_softcounts'][j] = result['all_trans_softcounts'][j].transpose()
        for j in range(num_subparts):
            result['all_emission_softcounts'][j] = result['all_emission_softcounts'][j].transpose()

        log_likelihoods[i][0] = result['total_loglike']

        if(i > 1 and abs(log_likelihoods[i][0] - log_likelihoods[i-1][0]) < tol):
            break

        model = M_step.run(model, result['all_trans_softcounts'], result['all_emission_softcounts'], result['all_initial_softcounts'])

    return(model, log_likelihoods[:i+1])