def simple_symbgraph(): def make_data(n): X = rnd.randn(n, 5) Y = np.array([ [ x[0]>=0, x[1]>=0, x[2]>=x[3] and x[2]>=x[4] ] for x in X ], dtype=int) return { 'X': X, 'Y': Y } # Y_kwlist = [ {'RIGHT': y[0], 'TOP': y[1], 'RED': y[2] } for y in Y ] # return { 'X': X, # 'Y': Y, # 'Y_kwlist': Y_kwlist } fg = FactorGraph() feats1 = fg.add(Feats, 'feats1', nfeats = 1) feats2 = fg.add(Feats, 'feats2', nfeats = 1) feats3 = fg.add(Feats, 'feats3', nfeats = 3) RIGHT = fg.add(Variable, 'RIGHT', domain = 2) TOP = fg.add(Variable, 'TOP', domain = 2) RED = fg.add(Variable, 'RED', domain = 2) V_ = (RIGHT, TOP, RED) F_RIGHT = fg.add(FeatFactor, 'F_RIGHT', RIGHT, feats = feats1) F_TOP = fg.add(FeatFactor, 'F_TOP', TOP, feats = feats2) F_RED = fg.add(FeatFactor, 'F_RED', RED, feats = feats3) F_ = fg.add(FunFactor, 'F_', V_, nfunfeats = 1) F_.fun = lambda _values: np.array(sum(_values)) fg.make() return fg, make_data
def make_tabpgraph(vfun): fg = FactorGraph() V1, V2 = vfun(fg) F1 = fg.add(TabPFactor, 'F1', V1 ) F2 = fg.add(TabPFactor, 'F2', (V1, V2) ) fg.make() return fg
def make_tabgraph(vfun): fg = FactorGraph() V1, V2, V3 = vfun(fg) F1 = fg.add(TabFactor, 'F1', V1 ) F2 = fg.add(TabFactor, 'F2', (V1, V2) ) F3 = fg.add(TabFactor, 'F3', (V2, V3) ) # F1 prefers if V1 is 0 F1.table = [ 10, 1 ] # F2 prefers if V1 and V2 are the same F2.table = [[ 10, 1 ], [ 1, 10 ]] # F3 prefers if V2 and V3 are different F3.table = [[ 1, 10 ], [ 10, 1 ]] fg.make() return fg
def simple_featgraph(): fg = FactorGraph() V1 = fg.add(Variable, 'V1', domain=2) V2 = fg.add(Variable, 'V2', domain=2) feats = fg.add(Feats, 'feats', nfeats = 2) F1 = fg.add(FeatFactor, 'F1', (V1, V2), feats = feats) fg.make() return fg, make_data
def simple_fungraph(): def make_data(n): X = rnd.randn(n, 2) Y = np.array([ [ 'RIGHT' if x[0]>=0 else 'LEFT', 'TOP' if x[1]>=0 else 'BOTTOM', 'BL' if x[0]>=x[1] else 'TR' ] for x in X ]) # Y = np.array([ [ x[0]>=0, x[1]>=0, x[0]>=x[1] ] for x in X ], dtype=bool) # Y = np.array([ [ x[0]>=0, x[1]>=0, x[0]>=x[1] ] for x in X ], dtype=int) return { 'X': X, 'Y': Y } fg = FactorGraph() feats = fg.add(Feats, 'feats', nfeats = 2) # V1 = fg.add(Variable, 'V1', domain=2) # V2 = fg.add(Variable, 'V2', domain=2) # V3 = fg.add(Variable, 'V3', domain=2) # V1 = fg.add(Variable, 'V1', domain=[False, True]) # V2 = fg.add(Variable, 'V2', domain=[False, True]) # V3 = fg.add(Variable, 'V3', domain=[False, True]) V1 = fg.add(Variable, 'V1', domain=['RIGHT', 'LEFT']) V2 = fg.add(Variable, 'V2', domain=['TOP', 'BOTTOM']) V3 = fg.add(Variable, 'V3', domain=['TR', 'BL']) F1 = fg.add(FunFactor, 'F1', V1, feats = feats) F2 = fg.add(FunFactor, 'F2', V2, feats = feats) F3 = fg.add(FunFactor, 'F3', V3, feats = feats) F_ = fg.add(FunFactor, 'F_', (V1, V2, V3), feats = feats) F1.fun = lambda feats, V1: feats * (V1 == 'RIGHT') F2.fun = lambda feats, V2: feats * (V2 == '') F3.fun = lambda feats, V3: feats * V3 F_.fun = lambda feats, V1, V2, V3: feats * (V1+V2+V3) F1.fun = lambda feats, V1: feats * (V1 == 'RIGHT') F2.fun = lambda feats, V2: feats * (V2 == '') F3.fun = lambda feats, V3: feats * V3 F_.fun = lambda feats, V1, V2, V3: feats * (V1+V2+V3) # TODO alternatively: lambda s: s.feats * (s.V1 == 'RIGHT') fg.make() return fg, make_data
def make_featgraph(vfun): fg = FactorGraph() V1, V2, V3 = vfun(fg) feats = fg.add(Feats, 'feats', nfeats = 2) # Each factor has its own independent set of parameters F1 = fg.add(FeatFactor, 'F1', V1, feats = feats) F2 = fg.add(FeatFactor, 'F2', V2, feats = feats) F3 = fg.add(FeatFactor, 'F3', V3, feats = feats) F_ = fg.add(FeatFactor, 'F_', (V1, V2, V3), feats = feats) fg.make() return fg