From ae700b07db509364e36c0978a0c8630c1e5ee8fb Mon Sep 17 00:00:00 2001 From: root Date: Fri, 4 Oct 2019 12:58:01 +0000 Subject: [PATCH 1/3] added function to tm.py to access all clauses of instance of MultiClassTsetlinMachine --- pyTsetlinMachine/tm.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pyTsetlinMachine/tm.py b/pyTsetlinMachine/tm.py index 0098a98..67260b8 100644 --- a/pyTsetlinMachine/tm.py +++ b/pyTsetlinMachine/tm.py @@ -230,6 +230,25 @@ def ta_state(self, mc_tm_class, clause, ta): def ta_action(self, mc_tm_class, clause, ta): return _lib.mc_tm_ta_action(self.mc_tm, mc_tm_class, clause, ta) + + #return list of all clauses + def get_all_clauses(self,list_of_classes,num_of_clauses, num_of_features): + all_clauses=[[] for i in range (num_of_clauses)] + for cur_clause in range(num_of_clauses): + for cur_cls in list_of_classes: + this_clause='' + for f in range(num_of_features*2): + action = self.ta_action(int(cur_cls), cur_clause, f) + if action==1: + if this_clause!='': + this_clause+='AND ' + if f Date: Fri, 8 Nov 2019 11:11:47 +0100 Subject: [PATCH 2/3] Update tm.py --- pyTsetlinMachine/tm.py | 58 ++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/pyTsetlinMachine/tm.py b/pyTsetlinMachine/tm.py index fb919e0..8cbe7e5 100644 --- a/pyTsetlinMachine/tm.py +++ b/pyTsetlinMachine/tm.py @@ -191,6 +191,26 @@ def set_state(self, ta_states): _lib.mc_tm_set_state(self.mc_ctm, i, ta_states[i]) return + + #save trained model and hyperparams [if indexed is added, self.indexed has to be saved in hyperparams] + def save_model(self, savefile): + save_ta_state = self.get_state() + #0:self.number_of_clauses, 1:self.T, 2:self.s, 3:self.patch_dim[0], 4:self.patch_dim[1], 5:self.boost_true_positive_feedback, 6:self.number_of_state_bits, 7:self.dim_x, 8:self.dim_y, 9:self.dim_z, 10:self.number_of_classes + hp=np.array([self.number_of_clauses, self.T, self.s, self.patch_dim[0], self.patch_dim[1], self.boost_true_positive_feedback, self.number_of_state_bits,self.dim_x,self.dim_y,self.dim_z,self.number_of_classes]) + np.savez_compressed(savefile, states=save_ta_state, hyperparams=hp) + + #load trained model and hyperparams [if indexed is added, bool(int(self.indexed)) as to be passed to __init__] + def load_model(load_filename): + ld=np.load(load_filename) + hp=ld['hyperparams'] + tm2 = MultiClassConvolutionalTsetlinMachine2D(int(hp[0]), int(hp[1]), int(hp[2]), (int(hp[3]),int(hp[4])), boost_true_positive_feedback=int(hp[5]), number_of_state_bits=int(hp[6])) + newX=np.ones((1,int(hp[7]),int(hp[8]),int(hp[9]))) + newY=np.random.randint(int(hp[10]), size=(int(hp[10])*int(hp[0]),)) + tm2.fit(newX, newY, epochs=0) + ta_state_loaded = ld['states'] + tm2.set_state(ta_state_loaded) + + return tm2 class MultiClassTsetlinMachine(): def __init__(self, number_of_clauses, T, s, boost_true_positive_feedback=1, number_of_state_bits=8, indexed=True): @@ -268,25 +288,6 @@ def ta_state(self, mc_tm_class, clause, ta): def ta_action(self, mc_tm_class, clause, ta): return _lib.mc_tm_ta_action(self.mc_tm, mc_tm_class, clause, ta) - - #return list of all clauses - def get_all_clauses(self,list_of_classes,num_of_clauses, num_of_features): - all_clauses=[[] for i in range (num_of_clauses)] - for cur_clause in range(num_of_clauses): - for cur_cls in list_of_classes: - this_clause='' - for f in range(num_of_features*2): - action = self.ta_action(int(cur_cls), cur_clause, f) - if action==1: - if this_clause!='': - this_clause+='AND ' - if f Date: Fri, 8 Nov 2019 11:18:41 +0100 Subject: [PATCH 3/3] Added save, load, access clauses MultiClassTsetlinMachine: access clauses, save, load MultiClassConvolutionalTsetlinMachine2D : save, load --- pyTsetlinMachine/tm.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pyTsetlinMachine/tm.py b/pyTsetlinMachine/tm.py index 8cbe7e5..e7c0bd5 100644 --- a/pyTsetlinMachine/tm.py +++ b/pyTsetlinMachine/tm.py @@ -303,6 +303,24 @@ def set_state(self, ta_states): return + #return list of all clauses + def get_all_clauses(self,list_of_classes,num_of_clauses, num_of_features): + all_clauses=[[] for i in range (num_of_clauses)] + for cur_clause in range(num_of_clauses): + for cur_cls in list_of_classes: + this_clause='' + for f in range(num_of_features*2): + action = self.ta_action(int(cur_cls), cur_clause, f) + if action==1: + if this_clause!='': + this_clause+='AND ' + if f