Skip to content

Commit

Permalink
fixed unit_test part 2 + invert labels in accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
jitskevanpeer committed Dec 14, 2023
1 parent c85ed3a commit c1e9a6c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
20 changes: 9 additions & 11 deletions HDC_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@

def compute_accuracy(HDC_cont_test, Y_test, centroids, biases):
Acc = 0
n_class = np.max(Y_test) + 1
n_class = int(np.max(Y_test) + 1)
for i in range(Y_test.shape[0]): # Y_test.shape[0] = rows of Y_test = each patient
received_HDC_vector = (HDC_cont_test[i])
all_resp = np.zeros(n_class)
for cl in range(n_class): # classes is true or false (cancer or no cancer)
final_HDC_centroid = (centroids[cl])
#compute LS-SVM response
response = np.dot(np.transpose(final_HDC_centroid),received_HDC_vector) + biases[cl]
#print("Responce before if: ", response)
if response < 0:
response = -1
else:
response = 1
#print("Responce after if: ", response)

all_resp[cl] = response
class_idx = np.argmax(all_resp)

if class_idx == 0:
class_idx = 1
else:
class_idx = -1

if class_idx == Y_test[i]:
Acc += 1
#print(Acc/Y_test.shape[0])
Acc += 1
return Acc/Y_test.shape[0]


Expand All @@ -54,7 +49,7 @@ def lookup_generate(dim, n_keys, mode = 1):

# dim is the HDC dimensionality D
def encode_HDC_RFF(img, position_table, grayscale_table, dim):
#img containts the 30 features of the current patient
#img contains the 30 features of the current patient
img_hv = np.zeros(dim, dtype=np.int16)
container = np.zeros((len(position_table), dim))

Expand Down Expand Up @@ -106,6 +101,9 @@ def train_HDC_RFF(n_class, N_train, Y_train_init, HDC_cont_train, gamma, D_b):
#Solve the system of equations to get the vector alpha:
alpha = np.zeros(N_train+1)
alpha = np.linalg.solve(Beta,L) #alpha here is the whole v vector from the slides
print("beta =",Beta)
#print("L =",L)
print("alpha =",alpha)

# Get HDC prototype for class cla, still in floating point
final_HDC_centroid = np.zeros(100)
Expand Down Expand Up @@ -169,7 +167,7 @@ def evaluate_F_of_x(Nbr_of_trials, HDC_cont_all, LABELS, beta_, bias_, gamma, al
elif abs(cyclic_accumulation_train[row,col] - pow(2,B_cnt-1)) <= alpha_sp:
cyclic_accumulation_train[row,col] = 0

Y_train = LABELS[:N_train] - 1
Y_train = (LABELS[:N_train] - 1)*2-1
Y_train = Y_train.astype(int)

# Train the HDC system to find the prototype hypervectors, _q meqns quantized
Expand Down
Binary file modified XOR_test_data.mat
Binary file not shown.
Binary file modified __pycache__/HDC_library.cpython-36.pyc
Binary file not shown.
26 changes: 21 additions & 5 deletions playground_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ def test_matrix_probability(LUT,in_p):
in2 = mat['in2']
desired = mat['result']


# TO DO
def test_XOR(in1,in2,desired,dim):
img_hv, calculated = encode_HDC_RFF([0], in1, in2, dim)
img = np.random.randint(0, 256, size=30)
img_hv, calculated = encode_HDC_RFF(img, in1, in2, dim)
print("desired =",desired)
print("calculated =",calculated)
if (desired == calculated).all():
Expand All @@ -56,6 +57,21 @@ def test_XOR(in1,in2,desired,dim):

#test_XOR(in1,in2,desired,dim)

def test_train():
n_class = 2
N_train = 360
gamma = 0.0002
D_b = 4
Y_train_init = np.concatenate((np.ones(N_train),np.ones(N_train)*(-1)))
HDC_cont_train = np.concatenate((np.ones((N_train,100)),np.ones((N_train,100))*(-1)))
centroids, biases, centroids_q, biases_q = train_HDC_RFF(n_class, 2*N_train, Y_train_init, HDC_cont_train, gamma, D_b)
print("centroids =",centroids)
print("biases =",biases)
Acc = compute_accuracy(HDC_cont_train, Y_train_init, centroids_q, biases_q)
return Acc

#Acc = test_train()
#print("Acc =",Acc)
# If testset = trainset, we should get 100% accuracy


Expand All @@ -67,7 +83,7 @@ def test_XOR(in1,in2,desired,dim):
B_cnt = 8
maxval = 256 #The input features will be mapped from 0 to 255 (8-bit)
D_HDC = 100 #HDC hypervector dimension
portion = 0.8 #We choose 60%-40% split between train and test sets
portion = 0.6 #We choose 60%-40% split between train and test sets
Nbr_of_trials = 1 #Test accuracy averaged over Nbr_of_trials runs
N_tradeof_points = 20 #Number of tradeoff points - use 100
N_fine = int(N_tradeof_points*0.4) #Number of tradeoff points in the "fine-grain" region - use 30
Expand All @@ -83,7 +99,7 @@ def test_XOR(in1,in2,desired,dim):
DATASET = np.loadtxt(dataset_path, dtype = object, delimiter = ',', skiprows = 1)
X = DATASET[:,2:].astype(float)
LABELS = DATASET[:,1]
LABELS[LABELS == 'M'] = 0
LABELS[LABELS == 'M'] = 1
LABELS[LABELS == 'B'] = 2
LABELS = LABELS.astype(float)
X = X.T / np.max(X, axis = 1)
Expand Down Expand Up @@ -158,4 +174,4 @@ def test_XOR(in1,in2,desired,dim):
F_of_x.append(1 - np.mean(local_avg)) #Append cost F(x)
Accs.append(np.mean(local_avgre))
Sparsities.append(np.mean(local_sparse))
##################################
##################################

0 comments on commit c1e9a6c

Please sign in to comment.