Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for a «set_TA_action” method #46

Open
satunheim opened this issue Aug 29, 2023 · 5 comments
Open

Request for a «set_TA_action” method #46

satunheim opened this issue Aug 29, 2023 · 5 comments

Comments

@satunheim
Copy link

satunheim commented Aug 29, 2023

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 satunheim changed the title Request for «set_TA_action” and “set_weight” methods Request for a «set_TA_action” method Sep 6, 2023
@satunheim
Copy link
Author

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

@satunheim
Copy link
Author

satunheim commented Sep 6, 2023

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.

@perara
Copy link
Member

perara commented Sep 6, 2023

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?

@olegranmo
Copy link
Member

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

@satunheim
Copy link
Author

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])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants