Example #1
0
    def network( self ):
        n = self.integer( )
        self.comment()
        self.newline()

        net = BNet()

        for i in xrange( 1, n+1 ):
            net.add( self.node( i ) )
            self.comment()
            self.newline()

        return net
Example #2
0
    def parse( self, inp ):
        net = BNet()

        NETWORK = Keyword( "network" ).suppress()
        VARIABLE = Keyword( "variable" ).suppress()
        PROBABILITY = Keyword( "probability" ).suppress()
        PROPERTY = Keyword( "property" ).suppress()
        VARIABLETYPE = Keyword( "type" ).suppress()
        DISCRETE = Keyword( "discrete" ).suppress()
        DEFAULTVALUE = Keyword( "default" ).suppress()
        TABLEVALUES = Keyword( "table" ).suppress()

        LBRAC = Literal( '(' ).suppress()
        RBRAC = Literal( ')' ).suppress()
        LCURL = Literal( '{' ).suppress()
        RCURL = Literal( '}' ).suppress()
        LSQUA = Literal( '[' ).suppress()
        RSQUA = Literal( ']' ).suppress()
        SEMI = Literal( ';' ).suppress()
        ORSEP = Literal( '|' ).suppress()

        identifier = Word( alphas, alphanums )
        identifier.setName( 'identifier' )

        property = PROPERTY + OneOrMore( ~( SEMI ) ) + SEMI
        property.setName( 'property' )

        number = Word( nums )
        number.setName( 'number' )
        number.setParseAction( lambda s,l,t: [ int( t[ 0 ] ) ] )

        real = Combine( number + '.' + number )
        real.setName( 'real' )
        real.setParseAction( lambda s,l,t: [ float( t[ 0 ] ) ] )

        value = identifier
        variable_values = Group( delimitedList( value ) )
        
        prEntry = LBRAC + variable_values + RBRAC + Group( delimitedList( real ) ) + SEMI
        prEntry.setName( 'prEntry' )
        prEntry.setParseAction( lambda s,l,t: [ ( tuple( t[0] ), tuple( t[1] ) ) ] )

        defaultPrEntry = Group( delimitedList( real ) ) + SEMI
        defaultPrEntry.setName( 'defaultPrEntry' )
        defaultPrEntry.setParseAction( lambda s,l,t: [ tuple( t[0] ) ] )

        prTable = TABLEVALUES + Group( delimitedList( real ) ) + SEMI
        prTable.setName( 'prTable' )
        prTable.setParseAction( lambda s,l,t: [ ((), tuple( t[0] )) ] )

        condVars = LBRAC + Group( identifier + Optional( ORSEP + delimitedList( identifier ) ) ) + RBRAC
        condVars.setName( 'condVars' )

        probability = PROBABILITY + condVars + LCURL + Group( ZeroOrMore( property ) ) + Group( ZeroOrMore( defaultPrEntry | prEntry | prTable ) ) + RCURL
        probability.setName( 'probability' )
        probability.addParseAction( lambda s,l,t: net.get( t[0][0] ).setTable( t[2] ) )
        probability.addParseAction( lambda s,l,t: ( net.get( t[ 0 ][0] ).setParents( t[0][1:] ) ) if len( t[0] ) > 1 else False )

        variable_discrete = VARIABLETYPE + DISCRETE + LSQUA + number + RSQUA + LCURL + variable_values + RCURL + SEMI
        variable_discrete.setName( 'variable_discrete' )
        variable_discrete.setParseAction( lambda s,l,t : [ t[ 1 ] ] )

        variable = VARIABLE + identifier + LCURL + ZeroOrMore( property | variable_discrete ) + RCURL
        variable.setName( 'variable' )
        variable.addParseAction( lambda s,l,t : net.add( BNode( t[ 0 ], values = map( str, t[1] ) )) ) 
        variable.addParseAction( lambda s,l,t : [] ) 

        network = NETWORK + identifier + LCURL + Group( ZeroOrMore( property ) ) + RCURL
        network.setName( 'name' )
        toplevel = network + ZeroOrMore( variable | probability )
        toplevel.setName( 'toplevel' )

        if self.debug:
            for elem in [ value, variable_values, prEntry, defaultPrEntry, prTable, condVars,
                    probability, variable_discrete, variable, network, toplevel ]:
                elem.setDebug()

        toplevel.parseString( inp )
        
        return net
Example #3
0
def Densenet124(reduce_dim=False, ncls=10, **kwargs):
    return BNet.Cls(layers=[10, 20, 30], reduce_dim=reduce_dim, ncls=ncls)
Example #4
0
def Densenet(**kwargs):
    return BNet.Cls()
Example #5
0
					message[BN.vars[var].index(vals_assignment[tgt_index])] += factor.funct(vals_assignment) * prod_value

				incoming_messages[var][edges[var].index(factor)] = np.array(copy.copy(message)) #put the message in the "queue"

		#move messages from incoming_messages to states
		for var in incoming_messages:
			states[var] = copy.copy(incoming_messages[var])
			for vect in states[var]: #normalize the vectors
				vect = (1.0/sum(vect))*vect
			
	#our final states are just a product of all of the state vectors for each var
	distributions = {}
	for var in states:
		dist = np.array([1 for _ in xrange(len(BN.vars[var]))])
		for vect in states[var]:
			dist = dist * vect
		distributions[var] = [x/float(sum(dist)) for x in dist]

	#write our results to a file
	f = open(output, "w")
	for var in BN.vars_in_order:
		f.write("{} ".format(var))
		for n in distributions[var]:
			f.write("{} ".format(n))
		f.write("\n")
	f.close()

if __name__== '__main__':
	net_name = sys.argv[1]
	net = BNet(net_name)
	sum_product(net, output = "{}-results.txt".format(net_name.replace(".bif", "")))
Example #6
0
def B(**kwargs):
    return BNet.Net()