tmu icon indicating copy to clipboard operation
tmu copied to clipboard

Request for a «set_TA_action” method

Open satunheim opened this issue 1 year ago • 5 comments

For the TMCoalescedClassifier I have used the following code to extract TA_actions per clause and weights per clause (per class).

TA_action_array= np.zeros((numberofclauses, numberofliterals), int) for k in range (numberofclauses):
for b in range (numberofliterals): if tm.get_ta_action(k,b) == True: TA_action_array[k,b]=1

Weightarray=np.zeros((numberofclasses, numberofclauses), int) for g in range (numberofclasses): for c in range (numberofclauses): Weightarray[g,c]=tm.get_weight(g,c)

I have used this just for extracting a trained model from the TMU – for use on hardware implementations of the TMCoalescedClassifier.

It would be great if a “set_TA_action” method could be included for the TMU, so it would be possible to manually set individual TA_actions. There is a “set_TA_state” method available, and this can be used. However, one then needs to know the number of TA states. A “set_TA_action” method would be simpler to use, and could potentially be implemented by using the “set_TA_state” to set the TA state either in N (exclude) or N+1 (include).

Furthermore, it would be great if a “set_weight” method could be included so it would be possible to manually explore the influence on individual weights.

This issue is also related to Issue #43.

satunheim avatar Aug 29 '23 15:08 satunheim

The “set_weight” method is already available. Sorry I hadn't noticed.

satunheim avatar Sep 06 '23 09:09 satunheim

The set_TA_action and set_weight methods work fine. However, it seems one has to perform training of the TM (in my case the TMCoalescedClassifier) before the methods can be applied. This is possibly difficult to avoid. If that is the case, one can perform only a short training phase (with only a few samples) only to get all the clause_banks etc. defined, before one "loads" an existing model to the TM.

satunheim avatar Sep 06 '23 11:09 satunheim

Currently, we need to run .fit before the model is initialized. But we might be able to do the following, if @olegranmo: Perhaps we should make a parameter: input_shape in the constructor of TM models. then we can initialize models before fit is executed.

What do you think?

perara avatar Sep 06 '23 11:09 perara

It absolutely makes sense to add shape. What is extracted from the data is simply the number of features and the number of classes, which is needed to set up the data structures

olegranmo avatar Sep 06 '23 11:09 olegranmo

The following code example seems to work well back and forth, and utilizes .csv model files. Here I do not use the TA_action_data when loading a model, just the TA_state_data. I am sure there can be more elegant ways to implement things, but this is something that is easy to understand and use.

If a model can be loaded without performing the fit method first, this can be time saving for various experiments.

#################################################### #EXTRACT a model from a trained TM (TMCoalescedClassifier): ####################################################

numberofclasses = 10
numberofclauses = 128 numberofliterals=272

TA_action_array= np.zeros((numberofclauses, numberofliterals), int) TA_state_array= np.zeros((numberofclauses, numberofliterals), int)

for k in range (numberofclauses):
for b in range (numberofliterals): TA_state_array[k,b]= tm.get_ta_state(k,b) if tm.get_ta_action(k,b) == True: TA_action_array[k,b]=1

weightarray=np.zeros((numberofclasses, numberofclauses), int) for g in range (numberofclasses): for c in range (numberofclauses): weightarray[g,c]=tm.get_weight(g,c)

#SAVE MODEL: savetxt('TA_action_data.csv', TA_action_array, delimiter=',') savetxt('TA_state_data.csv', TA_state_array, delimiter=',') savetxt('weight_data.csv', weightarray, delimiter=',')

#LOAD MODEL: weightarray = loadtxt('weight_data.csv', delimiter=',') #TA_action_array = loadtxt('TA_action_data.csv', delimiter=',') TA_state_array = loadtxt('TA_state_data.csv', delimiter=',')

for k in range (numberofclauses):
for b in range (numberofliterals): tm.set_ta_state(k, b, int(TA_state_array[k,b]))

for g in range (numberofclasses): for c in range (numberofclauses): tm.set_weight(g, c, Weightarray[g,c])

satunheim avatar Sep 06 '23 13:09 satunheim