Skip to content

Commit

Permalink
created a test for the LU
Browse files Browse the repository at this point in the history
  • Loading branch information
spyrchat committed Nov 29, 2023
1 parent dbd92c0 commit bb55c0a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 3 additions & 1 deletion HDC_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ def train_HDC_RFF(n_class, N_train, Y_train_init, HDC_cont_train, gamma, D_b):
omega = np.zeros(N_train, N_train)
#Fill Beta:


for i in range(N_train):
for j in range(N_train):
omega[i][j] = Y_train[i]*Y_train[j]*(np.transpose(HDC_cont_train))[i]*HDC_cont_train[j]
omega[i, j] = Y_train[i] * Y_train[j] * HDC_cont_train[i] * HDC_cont_train[j]

Beta[1:N_train+1,0] = Y_train
Beta[0,1:N_train+1] = np.transpose(Y_train)
Beta[1:N_train+1,1:N_train+1] = omega + pow(gamma,-1) * np.identity(N_train)
Expand Down
36 changes: 35 additions & 1 deletion playground.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Here the unit tests are performed
import numpy as np
import sys
from scipy.linalg import lu_factor, lu_solve
np.set_printoptions(threshold=sys.maxsize)

def lookup_generate(dim, n_keys, mode = 1):
Expand All @@ -23,4 +24,37 @@ def lookup_generate(dim, n_keys, mode = 1):
xor_result = (xor_result != 0).astype(int)
xor_result[xor_result == 0] = -1
counter = xor_result.sum(axis=0)
print(counter)

N_train =200
Y_train = np.random.choice([-1, 1], size=(N_train))
#print(Y_train)
HDC_cont_train = np.random.choice([-1,0,1], size=(N_train,1))
gamma = 2
Beta = np.zeros((N_train+1, N_train+1)) #LS-SVM regression matrix
omega = np.zeros((N_train, N_train))

for i in range(N_train):
for j in range(N_train):
omega[i, j] = Y_train[i] * Y_train[j] * HDC_cont_train[i] * HDC_cont_train[j]

Beta[1:N_train+1,0] = Y_train
Beta[0,1:N_train+1] = np.transpose(Y_train)
Beta[1:N_train+1,1:N_train+1] = omega + pow(gamma,-1) * np.identity(N_train)



#Target vector L:

L = np.zeros(N_train+1)
L[1:N_train+1] = np.ones(N_train)

#Solve the system of equations to get the vector alpha:

v = np.zeros(N_train+1)
lu, piv = lu_factor(Beta)
v = lu_solve((lu,piv),L)
alpha = v[1:N_train+1]
b = v[0]
print(v)
print(len(alpha))
print(b)

0 comments on commit bb55c0a

Please sign in to comment.