Beispiel #1
0
 def union(self, element1, element2):
     def max_extended(m1, m2):
         if m1 is None or m2 is None:
             return None
         return max(m1, m2)
     
     if element1 is None:
         if element2 is None:
             result = None
         else:
             result = element2.copy()
     elif element2 is None:
         result = element1.copy()
     else:
         result = dbm.DBM()
         common_vars = []
         for v in self.variables:
             if v in element1.all_nodes() or v in element2.all_nodes():
                 common_vars.append(v)
         for v1 in common_vars:
             for v2 in common_vars:
                 result.set_weight(
                     v1,
                     max_extended(element1.get_weight(v1, v2),
                                  element2.get_weight(v1, v2)),
                     v2)
     return result
def test_dbm_exists_negative_cycle():

    d1 = dbm.DBM()
    d1.set_weight(1, 1, 2)
    d1.set_weight(2, -2, 1)
    assert d1.exists_negative_cycle()
    d2 = dbm.DBM()
    d2.set_weight(1, -2, 2)
    d2.set_weight(2, 3, 1)
    assert not d2.exists_negative_cycle()
    d3 = dbm.DBM()
    for i in range(0, 500):
        d3.set_weight(i, i * ((-1)**(i % 2)), i + 1)
    d3.set_weight(500, 100, 50)
    assert d3.exists_negative_cycle()
    d3.set_weight(500, 100000000, 50)
    assert not d3.exists_negative_cycle()
def test_dbm_copy():

    d = dbm.DBM()
    x = 100
    y = 200
    d.set_weight(1, x, 2)
    d.set_weight(2, y, 2)
    d2 = d.copy()
    assert d.to_string() == d2.to_string()  # == d2.to_string()
def test_dbm_set_weight_get_weight():

    d = dbm.DBM()
    d.set_weight(1, 3, 2)
    assert d.get_weight(1, 2) == 3
    d.set_weight(2, 4, 2)
    d.set_weight(1, -99, 2)
    assert d.get_weight(1, 2) == -99
    assert d.get_weight(2, 2) == 4
    d.set_weight(2, None, 2)
    assert d.get_weight(2, 2) is None
    d.set_weight(1, None, 2)
    assert d.get_weight(1, 2) is None
    assert d.get_weight("", 3) is None
    d.set_weight(1, 10, 2)
    d.set_weight(2, -10, 2)
    d.set_weight(1, 1, 3)
    d.set_weight(3, 0, 3)
    assert d.get_weight(3, 3) == 0
    assert d.get_weight(1, 1) is None
    assert d.get_weight(2, 2) == -10
Beispiel #5
0
 def widen(self, element1, element2):
     result = dbm.DBM()
     if element1 is None:
         if element2 is None:
             return None
         return element2.copy()
     elif element2 is None:
         return element1.copy()
     s1 = self._normalize(element1)
     s2 = self._normalize(element2)
     common_vars = []
     for v in self.variables:
         if v in s1.all_nodes() or v in s2.all_nodes():
             common_vars.append(v)
     for v1 in common_vars:
         for v2 in common_vars:
             val1 = element1.get_weight(v1, v2)
             val2 = element2.get_weight(v1, v2)
             if val1 is None or val2 is None or val2 > val1:
                 result.set_weight(v1, None, v2)
             else:
                 result.set_weight(v1, val2, v2)
     return result
def test_dbm_find_shortest_paths():

    # example from wikipedia, slightly extended
    d1 = dbm.DBM()
    d1.set_weight(1, -2, 3)
    d1.set_weight(3, 2, 4)
    d1.set_weight(4, -1, 2)
    d1.set_weight(2, 4, 1)
    d1.set_weight(2, 3, 3)
    d1.set_weight(5, 10, 5)
    d2 = d1.find_shortest_paths()
    assert d2.get_weight(1, 1) == 0
    assert d2.get_weight(1, 2) == -1
    assert d2.get_weight(1, 3) == -2
    assert d2.get_weight(1, 4) == 0
    assert d2.get_weight(2, 1) == 4
    assert d2.get_weight(2, 2) == 0
    assert d2.get_weight(2, 3) == 2
    assert d2.get_weight(2, 4) == 4
    assert d2.get_weight(3, 1) == 5
    assert d2.get_weight(3, 2) == 1
    assert d2.get_weight(3, 3) == 0
    assert d2.get_weight(3, 4) == 2
    assert d2.get_weight(4, 1) == 3
    assert d2.get_weight(4, 2) == -1
    assert d2.get_weight(4, 3) == 1
    assert d2.get_weight(4, 4) == 0
    assert d2.get_weight(5, 1) is None
    assert d2.get_weight(5, 2) is None
    assert d2.get_weight(5, 3) is None
    assert d2.get_weight(5, 4) is None
    assert d2.get_weight(5, 5) == 0
    assert d2.get_weight(1, 5) is None
    assert d2.get_weight(2, 5) is None
    assert d2.get_weight(3, 5) is None
    assert d2.get_weight(4, 5) is None
Beispiel #7
0
 def intersect(self, element1, element2):
     result = dbm.DBM()
     if element1 is None:
         return element2.copy()
     elif element2 is None:
         return element1.copy()
     def min_extended(m1, m2):
         if m1 is None:
             return m2
         elif m2 is None:
             return m1
         return min(m1, m2)
     common_vars = []
     for v in self.variables:
         if v in element1.all_nodes() or v in element2.all_nodes():
             common_vars.append(v)
     for v1 in common_vars:
         for v2 in common_vars:
             result.set_weight(
                 v1,
                 min_extended(element1.get_weight(v1, v2),
                              element2.get_weight(v1, v2)),
                 v2)
     return result
Beispiel #8
0
        else:
            flickr_u = np.concatenate((flickr_u, t), axis=0)

model = dbm.DBM(
    main_dir="flickr_rbm",
    do_pretrain=True,
    layers=[1024, 1024],
    models_dir=config.models_dir,
    data_dir=config.data_dir,
    summary_dir=config.summary_dir,
    learning_rate=[0.001, 0.001],
    momentum=0.9,
    num_epochs=[1, 1],
    batch_size=[64, 64],
    stddev=0.1,
    verbose=1,
    gibbs_k=[1, 1],
    model_name="flickr_dbm",
    finetune_learning_rate=0.01,
    finetune_enc_act_func=[tf.nn.sigmoid, tf.nn.sigmoid],
    finetune_dec_act_func=[tf.nn.sigmoid, tf.nn.sigmoid],
    finetune_num_epochs=5,
    finetune_batch_size=128,
    finetune_opt='momentum',
    finetune_loss_func="mean_squared",
    finetune_dropout=0.5,
    noise=["gauss", "bin"],
)

trainX = flickr_u[:3000]
testX = flickr_u[3000:3500]
Beispiel #9
0
 def dbmSession (self):
     user, pwd = self.getuser ('dbm')
     result = dbm.DBM (self.host, self.name, '', user + ',' + pwd)
     return result
Beispiel #10
0
 def getDbmSession (self):
     if self.cachedDBMSession == None:
         user, pwd = self.users ['dbm']
         self.cachedDBMSession = dbm.DBM (self.host, self.name, '', user + ',' +  pwd)
         self.keepDatabaseVersion (self.cachedDBMSession)
     return self.cachedDBMSession
Beispiel #11
0
#    Copyright (C) 2002 SAP AG
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#    ========== licence end
#

import sys
import string

import dbm

host = ''  # local connection
dbname = sys.argv[1]  # first command line argument contains database name
session = dbm.DBM(host, dbname)  # open session

stateResult = session.cmd('show state')  # execute command
lines = string.split(stateResult, '\n')  # split
print lines[1]  # second line contains state
Beispiel #12
0
 def get_top(self):
     return dbm.DBM()
Beispiel #13
0
  hidden_layer.state.add_col_vec(a)
  cm.log_1_plus_exp(hidden_layer.state)
  w_ais.add_sums(hidden_layer.state, axis=0)
  w_ais.add_dot(b.T, input_layer.state)
  offset = float(w_ais.asarray().max())
  w_ais.subtract(offset)
  cm.exp(w_ais)
  z = offset + np.log(w_ais.asarray().sum())
  return z

def Usage():
  print( '%s <model file> <number of Markov chains to run> [number of words (for Replicated Softmax models)]' )

if __name__ == '__main__':
  board = tr.LockGPU()
  model_file = sys.argv[1]
  numchains = int(sys.argv[2])
  if len(sys.argv) > 3:
    D = int(sys.argv[3]) #10 # number of words.
  m = dbm.DBM(model_file)
  m.LoadModelOnGPU(batchsize=numchains)
  plt.ion()
  log_z = AISReplicatedSoftmax(m, D, numchains, display=True)
  print( 'Log Z %.5f' % log_z )
  #log_z = AIS(m, schedule)
  #print 'Log Z %.5f' % log_z
  #log_z = ExactZ_binary_binary(m)
  #print 'Exact %.5f' % log_z
  tr.FreeGPU(board)
  raw_input('Press Enter.')
Beispiel #14
0
#
# dbup.py   starts a database
#
#
#    ========== licence begin LGPL
#    Copyright (C) 2002 SAP AG
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#    ========== licence end
#

import sys
import dbm

_dbname = sys.argv[1]
dbm.DBM('', _dbname).cmd('start')
Beispiel #15
0
#while(1):
    log=log2(n)
    #print('n = ',n) #calculates value of p
    logFrac,_= modf(log)
    #print ("log = ", log, "logfrac = ", logFrac) #returns decimal part of log
    if logFrac==0: #checks if decimal part of log is 0 which is possible only when n is a power of 2
        #Exploration Phase

        g=CalculateIndex() #update indices
        #print("Exploration at ", t)
        #print("g  = ")
        #for index in g:
           #print(index)
        #print()
        #k = dbm_with_escaling.DBM(g, prevk, M, N) #run DBM algorithm
        k = dbm.DBM(g, prevk, M, N)
        if (k != prevk): #checks if newly calculated matching is different from current matching
            #print('Matching has changed!')
            #print("k = ", k, "prevk = ", prevk)
            n = 1 #reset n=1 if matching changes
            #print("n  = ", n)
    else: #Exploitation Phase
        #print("Exploitation at ", t)
        #break
        k = prevk #keep matching as it is

    for i in range(M): #pick a user from M users sequentially
        reward[i][k[i]] = reward[i][k[i]] + pairReward[i][k[i]][t] #sums reward obtained when user i plays arm k[i] uptill time t
        playingCount[i]+=1 #increment playingCount for user i
        playingPairCount[i][k[i]]+=1 #incrment playingPairCount for the pair - user i and arm k[i]
        prevk[i]=k[i] #prevk[i] becomes k[i] since k[i] has been played ny user i 
Beispiel #16
0
# Assigning sm to all osp
for i in range(osp_N):
    tmp_sm = sms[sm_for_osp[i]]
    sm_data.append({
        "ospID": start_id_osp + i,
        "smID": tmp_sm['smID'],
        "name": tmp_sm["name"],
        "lastname": tmp_sm["lastname"],
        "function": tmp_sm["function"]
    })
    print(f'tmp_sm smID: {tmp_sm["smID"]} ')

if __name__ == "__main__":
    # path = ":memory:"
    path = "oblig1\\db\\oblig.db"
    db_obj = db.DBM(path)
    db_obj.create_table(name=osp_name, attributes=osp_attributes)
    db_obj.create_table(name=sm_name,
                        attributes=sm_attributes,
                        references=sm_references)

    # Inserting data
    #   Osp
    db_obj.insert(osp_name, osp_data)
    #   sm
    db_obj.insert(sm_name, sm_data)

    # All from osp
    q = """
     SELECT * FROM OnStagePersonnel
     """