Exemplo n.º 1
0
def getLogLikelihood(w1, w2, a1, a2, model):
    belief, ran, duration = beliefUpdate(w1, w2, model)

    LL = 0

    for i in xrange(len(w1)):
        assignedLetter = a1[i]
        LL += belief[i][assignmentToNumber(assignedLetter)]

    for i in xrange(len(w2)):
        assignedLetter = a2[i]
        LL += belief[i + len(w1)][assignmentToNumber(assignedLetter)]

    return LL, duration
Exemplo n.º 2
0
def marginalize(belief, separator, varsInBelief, typeOfFactor):
    if typeOfFactor=='o':
        return belief
    else:
        msg = {x:0 for x in xrange(10)}
        n = int(log10(len(belief.keys())))
        for i in belief.keys():
            assignment = numberToAssignment(i,n)
            assignmentToSep = assignment[varsInBelief.index(separator)]
            msg[assignmentToNumber(assignmentToSep)] += exp(belief[i])
        return {x:log(msg[x]) for x in msg.keys()}
Exemplo n.º 3
0
def marginalize(belief, separator, varsInBelief, typeOfFactor):
    if typeOfFactor=='o':
        return belief
    else:
        tempMaxFinder = {x:[] for x in xrange(10)}
        msg = {x:0 for x in xrange(10)}
        n = int(log10(len(belief.keys())))
        for i in belief.keys():
            assignment = numberToAssignment(i,n)
            assignmentToSep = assignment[varsInBelief.index(separator)]
            tempMaxFinder[assignmentToNumber(assignmentToSep)].append(belief[i])

        for i in tempMaxFinder.keys():
            msg[i] = max(tempMaxFinder[i])
        return msg
Exemplo n.º 4
0
def updateBelief(oldBelief, msg, separator, varsInBelief, edgeBelief, typeOfFactor):
    if typeOfFactor=='o':
        newBelief = {}
        for i in oldBelief.keys():
            newBelief[i] = oldBelief[i] + msg[i] - edgeBelief[i]
        return newBelief

    else:
        n = int(log10(len(oldBelief.keys())))
        newBelief = {}
        for i in oldBelief.keys():
            assignment = numberToAssignment(i, n)
            assignmentToSeparator = assignment[varsInBelief.index(separator)]
            indexSeparator = assignmentToNumber(assignmentToSeparator)

            newBelief[i] = oldBelief[i] + msg[indexSeparator] - edgeBelief[indexSeparator]

        return newBelief
Exemplo n.º 5
0
def beliefUpdate(w1, w2, model):
    betheGraph, betheMapping = getBetheClusterGraph(w1, w2, model)

    #TODO: Check if two clusters can be connected by more than one edge (and therefore separator). Example, trans edge and skip edge b/w letters.
    edgesInBetheGraph = {}
    numOfEdges = 0
    for i in betheGraph.keys():
        neighours = list(betheGraph[i])
        for j in neighours:
            edge = ({i,j[0]}, j[1])
            if edge not in edgesInBetheGraph.values():
                edgesInBetheGraph[numOfEdges] = edge
                numOfEdges += 1

    #print edgesInBetheGraph
    #print betheMapping

    ##############################################################################################
    #Cluster Graph Initialization

    #print betheGraph
    #print betheMapping
    #print edgesInBetheGraph
    #print '\n\n'

    beliefAtEachNode = {x:{} for x in betheMapping.keys()}
    beliefAtEachEdge = {x:{y:0 for y in xrange(10)} for x in edgesInBetheGraph.keys()}

    for x in betheMapping.keys():
        factor = betheMapping[x]
        typeOfFactor = factor[0]
        varsInFactor = factor[1]

        if typeOfFactor == 'o':
            if varsInFactor<len(w1):
                letter = w1[varsInFactor]
            else:
                letter = w2[varsInFactor - len(w1)]
            beliefAtEachNode[x] = ocrDat[letter].copy()
            beliefAtEachNode[x] = {assignmentToNumber(t): beliefAtEachNode[x][t] for t in beliefAtEachNode[x].keys()}

        elif typeOfFactor == 't':
            for i in xrange(100):
                assignment = numberToAssignment(i,2)
                beliefAtEachNode[x][i] = transDat[assignment[0]][assignment[1]]

        elif typeOfFactor == 's':
            for i in xrange(100):
                assignment = numberToAssignment(i,2)
                beliefAtEachNode[x][i] = getSkipFactor(assignment[0], assignment[1])

        else:
            for i in xrange(100):
                assignment = numberToAssignment(i,2)
                beliefAtEachNode[x][i] = getPairSkipFactor(assignment[0], assignment[1])

    ##############################################################################################
    #Initialization Complete. Now starting Belief Updates.

    numOfIterations = 0
    start = time.clock()
    while(True):
        maxError = -50000
        oldBelief = beliefAtEachNode.copy()

        for i in edgesInBetheGraph.keys():
            node1,node2 = edgesInBetheGraph[i][0]
            separator = edgesInBetheGraph[i][1]
            typeOfFactor1 = betheMapping[node1][0]
            typeOfFactor2 = betheMapping[node2][0]
            varsInNode1 = betheMapping[node1][1]
            varsInNode2 = betheMapping[node2][1]

            msg1to2 = marginalize(beliefAtEachNode[node1], separator, varsInNode1, typeOfFactor1)
            beliefAtEachNode[node2] = updateBelief(beliefAtEachNode[node2], msg1to2, separator, varsInNode2, beliefAtEachEdge[i], typeOfFactor2)
            beliefAtEachEdge[i] = msg1to2

            msg2to1 = marginalize(beliefAtEachNode[node2], separator, varsInNode2, typeOfFactor2)
            beliefAtEachNode[node1] = updateBelief(beliefAtEachNode[node1], msg2to1, separator, varsInNode1, beliefAtEachEdge[i], typeOfFactor1)
            beliefAtEachEdge[i] = msg2to1

            error1 = max([fabs(beliefAtEachNode[node1][i] - oldBelief[node1][i]) for i in oldBelief[node1].keys()])
            error2 = max([fabs(beliefAtEachNode[node2][i] - oldBelief[node2][i]) for i in oldBelief[node2].keys()])
            error = max(error1, error2)

            #print error

            if maxError<error:
                maxError = error

        ##############################################################################################
        #Normalization
        for i in beliefAtEachNode.keys():
            z = 0
            for x in beliefAtEachNode[i].keys():
                z += exp(beliefAtEachNode[i][x])
            z = log(z)
            for x in beliefAtEachNode[i].keys():
                beliefAtEachNode[i][x] -= z

        for i in beliefAtEachEdge.keys():
            z = 0
            for x in beliefAtEachEdge[i].keys():
                z += exp(beliefAtEachEdge[i][x])
            z = log(z)
            for x in beliefAtEachEdge[i].keys():
                beliefAtEachEdge[i][x] -= z

        numOfIterations += 1
        #print 'numOfIterations',numOfIterations
        #print 'maxError', maxError

        if (maxError<0.0001) or (numOfIterations >= 50):
            end = time.clock()
            duration = end - start
            return beliefAtEachNode, betheMapping, duration
            break